Python Notes
Python Notes
Print(x)
Print(X, Y, Z)
Print(1 + 2)
Define variable X=5
Y = “abc”
Define variable with X = str()
data type & length Y = int()
Z = float()
Define multi X, Y, Z = “a”, “b”, “c”
variables X = Y = Z = “a”
Add comment - #abc
- “””string
String”””
Get & print data Print(type(x))
type
Global variable 1. Define global variable normally
X = “abc” [Outside the def]
Variable
Data Type
Type Example
Str X = “Hello”
Int X = 20
Float X = 20.4
List X = [“a”, “b”, “c”]
Bool X = True
Random int Import random
Print(random.randrange(start, end))
Change data type X = int(2.6)
X = float(1)
X = str(2)
Data type - String
Return character by A = “Hello World”
index Print(a[4])
**o
Print character in For n in “apple”
variable one by one Print(x)
Check length of Print(len(x))
string
Check the variable Txt = “The best thing in life”
contains string Print(“life” in txt)
**True
Return strings by A = “Hello World”
index Print(a[1:4])
**ell
Print(a[:5])
**Hello
Print(a[2:])
**llo, World
Change to upper Print(a.upper())
case
Change to lower Print(a.lower())
case
Remove space Print(a.strip())
before & after string
Replace a character Print(a.replace)”a”, “b”)
with special case
Split string Print(a.split(“,”))
Formatting Print(f(“abc {int:. 2f} abc”))
Escape formatting \n
\t
Calculation Operator
Operator Assignment operator Meaning
+ += Addition
- -= Subtraction
* *= Multiplication
/ /= Division
% %= Get modulus
// //= Floor division
Comparison Operation
Operator Meaning
== Equal
!= Not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Logical Operator
Operator Short form Meaning
And & Both are reached
Or | Reach either one of it
Not ~ Inverts
print(myList[1])
**orange
Print(myList[-1])
**mango
Print(myList[1:4])
**[“orange”, “banana”, “cherry”]
Print(myList[:3])
**[“apple”, “orange”, “banana”]
Print(myList[2:])
**[“banana”, “cherry”, “mango”]
Check a string in the list If “apple” in myList:
Print(“Yes”)
Change list item with myList[2] = “apple”
index (1-to-1)
Change list item (add) myList = [“apple”, “banana”, “cherry”]