Learn Python in 1 Hour - Quick Notes
1. Basics
Print something:
print("Hello, World!")
Variables:
x=5
name = "Alice"
Data Types:
- int (5), float (5.5), str ("hello"), bool (True/False), list, dict, tuple, set
Input from user:
name = input("Enter your name: ")
2. Conditions
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
3. Loops
For loop:
for i in range(5):
print(i)
While loop:
while x > 0:
Learn Python in 1 Hour - Quick Notes
print(x)
x -= 1
4. Functions
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
5. Lists
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits[0]) # first item
Loop through list:
for fruit in fruits:
print(fruit)
6. Dictionaries
person = {"name": "Alice", "age": 25}
print(person["name"])
Add new key-value:
person["city"] = "New York"
7. Importing Modules
import math
print(math.sqrt(16))
Learn Python in 1 Hour - Quick Notes
8. Classes (OOP Basics)
class Person:
def __init__(self, name):
self.name = name
def greet(self):
print(f"Hi, I'm {self.name}")
p = Person("Alice")
p.greet()
9. Error Handling
try:
x = int(input("Enter a number: "))
except ValueError:
print("That's not a number!")
10. File Handling
Writing to file:
with open("file.txt", "w") as f:
f.write("Hello, file!")
Reading file:
with open("file.txt", "r") as f:
content = f.read()
print(content)
Bonus Tips
Learn Python in 1 Hour - Quick Notes
- Indentation matters (use 4 spaces).
- Comments: # this is a comment
- Pip: pip install package-name
- Popular libraries: numpy, pandas, flask, django