Python
Learn Python data types, data structures, conditions, loops, functions, classes, data analysis, web development, GUI development, and deep learning.
name = "ibrahim" # string
age = 20 # integer
height = 5.8 # float
student = True # boolean
fruits = ["apple", "orange", "mango"] # list
fruits = ("apple", "orange", "mango") # tuple
fruits = {"apple", "orange", "mango"} # set
fruits = {0: "apple", 2: "orange", 5: "mango"} # dictionary
| Feature | List | Tuple | Set | Dictionary |
|---|---|---|---|---|
| Mutable | Yes | No | Yes | Yes |
| Allows duplicates | Yes | Yes | No | No duplicate keys |
| Indexed access | Yes | Yes | No | By key |
| Add item | append() |
Not possible | add() |
dict[key] = value |
| Remove item | remove() |
Not possible | remove() |
del dict[key] |
temp = 29
if temp > 30:
print("turn on fan")
else:
print("turn off fan")
| Type | Example | Meaning |
|---|---|---|
| Equal | 5 == 5 |
True if same |
| Not equal | 5 != 3 |
True if different |
| Greater than | 10 > 2 |
True if left is bigger |
| Less than | 2 < 10 |
True if left is smaller |
| Greater or equal | 5 >= 5 |
True if bigger or same |
| Less or equal | 4 <= 6 |
True if smaller or same |
age > 18 and age < 60
city == "Paris" or city == "London"
not is_raining
if score > 90:
print("A")
elif score > 80:
print("B")
else:
print("C")
if age > 18:
if age < 25:
print("Young Adult")
for i in range(10):
print(i) # prints from 0 to 9
i = 0
while i < 10:
print(i) # prints from 0 to 9
i += 1
def add(a, b):
print(a + b)
add(10, 20)
add(2, 5)
| Argument Type | Definition | Example |
|---|---|---|
| Positional | def func(a, b) |
func(10, 20) |
| Keyword | def func(a, b) |
func(a=10, b=20) |
| Default | def func(a=5, b=10) |
func() |
| Extra positional | def func(*args) |
func(1, 2, 3, 4) |
| Extra keyword | def func(**kwargs) |
func(name="Ali", age=28) |
| Positional-only | def func(a, b, /) |
func(10, 20) |
| Keyword-only | def func(*, x, y) |
func(x=5, y=10) |
| Lambda | lambda x, y: x + y |
add(3, 5) |
| Multiple return | return a, b |
x, y = func(3, 5) |
| Docstring | """Explain here""" |
help(func) |
| Empty function | pass |
def func(): pass |
class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
def what_is_name(self):
print("name is", self.name)