-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python_Basics_Refresher.py
60 lines (47 loc) · 1.12 KB
/
Python_Basics_Refresher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Python Basics Cheat Sheet
# 1. Printing
print("Hello, World!") # Outputs: Hello, World!
# 2. Variables
x = 10 # Integer
y = 3.14 # Float
name = "Alice" # String
is_active = True # Boolean
# 3. Indentation (Python uses spaces to define code blocks)
if x > 5:
print("x is greater than 5")
# 4. Conditionals
if x > 5:
print("x is large")
elif x == 5:
print("x is equal to 5")
else:
print("x is small")
# 5. Loops
# For loop
for i in range(5): # 0 to 4
print(i)
# While loop
count = 0
while count < 5:
print(count)
count += 1
# 6. Functions
def greet(name):
return f"Hello, {name}!"
print(greet("Alice")) # Outputs: Hello, Alice!
# 7. Lists
numbers = [1, 2, 3, 4, 5]
numbers.append(6) # Add to list
print(numbers) # Outputs: [1, 2, 3, 4, 5, 6]
# 8. Dictionaries
person = {"name": "Alice", "age": 30}
print(person["name"]) # Outputs: Alice
# 9. Classes
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hi, I'm {self.name}"
p = Person("Alice", 30)
print(p.greet()) # Outputs: Hi, I'm Alice