Text Type: str
Numeric Types: int, float, complex
Sequence list, tuple, range
Types:
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType
x=5
print(type(x))
a = "Hello"
print(a)
age = 36
txt = "My name is John, I am " + age
print(txt)
age = 36
txt = f"My name is John, I am {age}"
print(txt)
price = 59
txt = f"The price is {price} dollars"
print(txt)
price = 59
txt = f"The price is {price:.2f} dollars"
print(txt)
txt = f"The price is {20 * 59} dollars"
print(txt)
x = "Hello World"
x = 20
x = 20.5
x = 1j
x = ["apple", "banana", "cherry"]
x = ("apple", "banana", "cherry")
x = range(6)
x = {"name" : "John", "age" : 36}
x = {"apple", "banana", "cherry"}
x = frozenset({"apple", "banana", "cherry"})
Python Casting
● int() - constructs an integer number from an integer literal, a float
literal (by removing all decimals), or a string literal (providing the
string represents a whole number)
● float() - constructs a float number from an integer literal, a float literal
or a string literal (providing the string represents a float or an integer)
● str() - constructs a string from a wide variety of data types, including
strings, integer literals and float literals
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2
Assignment Operators
Assignment operators are used to assign values to variables:
Operator Example Same As Tr
y
it
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
:= print(x := 3) x=3
print(x)
Python Logical Operators
Operator Description Example
and Returns True if both statements are true x < 5 and x
< 10
or Returns True if one of the statements is x < 5 or x <
true 4
not Reverse the result, returns False if the not(x < 5 and
result is true x < 10)
Python Membership Operators
Membership operators are used to test if a sequence is presented in an
object:
Operat Description Examp
or le
in Returns True if a sequence with the specified value x in y
is present in the object
not in Returns True if a sequence with the specified value x not in
is not present in the object y
Bitwise Operators
Bitwise operators are used to compare (binary) numbers:
Operat Name Description Example
or
& AND Sets each bit to 1 if both bits are 1 x&y
| OR Sets each bit to 1 if one of two bits is 1 x|y
^ XOR Sets each bit to 1 if only one of two bits x^y
is 1
~ NOT Inverts all the bits ~x
<< Zero fill left Shift left by pushing zeros in from the x << 2
shift right and let the leftmost bits fall off
>> Signed right Shift right by pushing copies of the x >> 2
shift leftmost bit in from the left, and let the
rightmost bits fall off
Operator Precedence
Parentheses has the highest precedence, meaning that expressions inside
parentheses must be evaluated first:
print((6 + 3) - (6 + 3))
Multiplication * has higher precedence than addition +, and therefore
multiplications are evaluated before additions:
print(100 + 5 * 3)
Operator Description
() Parentheses
** Exponentiation
+x -x ~x Unary plus, unary minus, and bitwise
NOT
* / // % Multiplication, division, floor division,
and modulus
+ - Addition and subtraction
<< >> Bitwise left and right shifts
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
== != > >= < < Comparisons, identity, and
= is is not in not membership operators
in
not Logical NOT
and AND
or OR
Boolean Values
print(10 > 9)
print(10 == 9)
print(10 < 9)