Python Notes
1. WHAT IS PYTHON?
Python is a simple, powerful programming language used for web, software, data, and
automation.
2. PYTHON SYNTAX
- Uses new lines
- Uses indentation
Examples:
print("Hello World")
if 5 > 2:
print("Correct indentation")
3. VARIABLES
Store data values.
Examples:
x=5
y = "Hello"
x = 10
x = "Now a string"
4. DATA TYPES
int, float, str, list, tuple, set, dict, bool
EXAMPLES:
x=5
y = 3.14
z = "Hi"
5. PRINT FUNCTION
Displays output.
Examples:
print("Hello World")
print(10)
print("Age:", 20)
6. COMMENTS
# Single line
print("Hello") # Inline
'''
Multiline comment
'''
7. STRINGS
Text in quotes.
Examples:
text = "Hello"
print(text[0])
print("Hello World"[0:5])
8. STRING METHODS
.upper(), .lower(), .strip()
EXAMPLES:
print("hello".upper())
print("HELLO".lower())
print(" Hello ".strip())
9. BOOLEANS
True or False
EXAMPLES:
print(10 > 5)
print(10 == 5)
print(bool("Hello"))
10. OPERATORS
+, -, *, /, %, ==, >, <, and, or
EXAMPLES:
print(5 + 3)
print(10 > 5)
print(5 > 2 and 10 > 3)
11. LISTS
Store multiple items.
EXAMPLES:
fruits = ["apple", "banana", "cherry"]
print(fruits[0])
[Link]("orange")
12. TYPE CASTING
int(), float(), str()
Examples:
x = int(3.5)
y = float(5)
z = str(10)
13. PYTHON NUMBERS
int, float, complex
Examples:
x = 10
y = 3.14
z = 2 + 3j
Additional Python Topics (Beginner Summary)
--- LISTS ---
ADD ITEMS:
[Link](), [Link]()
Examples:
fruits = ["apple", "banana"]
[Link]("cherry")
[Link](1, "orange")
REMOVE ITEMS:
remove(), pop(), del
EXAMPLES:
[Link]("banana")
[Link]()
del fruits[0]
LOOP LISTS:
for item in list
Examples:
for fruit in fruits:
print(fruit)
LIST COMPREHENSION:
Short way to create lists
EXAMPLES:
nums = [x for x in range(5)]
even = [x for x in range(10) if x % 2 == 0]
SORT LISTS:
sort(), reverse()
EXAMPLES:
nums = [3,1,2]
[Link]()
[Link](reverse=True)
COPY LISTS:
copy(), list()
EXAMPLES:
new = [Link]()
new2 = list(fruits)
JOIN LISTS:
+ or extend()
EXAMPLES:
a = [1,2]
b = [3,4]
c=a+b
[Link](b)
List Methods:
append(), remove(), pop(), sort(), reverse()
--- TUPLES ---
ACCESS:
tuple[index]
Examples:
t = ("a","b","c")
print(t[0])
UPDATE:
Convert to list
Examples:
x = list(t)
x[1] = "z"
t = tuple(x)
UNPACK:
Assign to variables
EXAMPLES:
a,b,c = t
LOOP:
for item in tuple
Join:
t3 = t1 + t2
Methods:
count(), index()
--- SETS ---
Access:
Loop only
Examples:
for item in myset:
print(item)
Add/Update:
add(), update()
Examples:
[Link]("new")
[Link](["a","b"])
Remove:
remove(), discard()
Examples:
[Link]("a")
[Link]("b")
Join:
union(), update()
Frozen Set:
immutable set
Examples:
f = frozenset([1,2,3])
Methods:
add(), remove(), union()
--- DICTIONARIES ---
Access:
dict[key]
Examples:
d = {"name":"John"}
print(d["name"])
Update/Add:
dict[key] = value
Examples:
d["age"] = 20
Remove:
pop(), del
Examples:
[Link]("age")
del d["name"]
Loop:
for key,value in [Link]()
Examples:
for k,v in [Link]():
print(k,v)
Join:
update()
Examples:
[Link](d2)
Frozen:
Not directly supported (use frozenset for keys)
Methods:
get(), keys(), values(), items()
Definitions (Quick Beginner Guide)
List: An ordered, changeable collection that allows duplicate values.
Tuple: An ordered, unchangeable collection that allows duplicate values.
Set: An unordered collection that does not allow duplicate values.
Frozen Set: An immutable (unchangeable) version of a set.
Dictionary: A collection of key-value pairs used to store data.
List Comprehension: A short and simple way to create lists using a single line of code.
Loop: A way to repeat a block of code multiple times.
Method: A built-in function that performs an action on an object (like list or string).
Join: Combining two or more collections into one.
Copy: Creating a duplicate of a collection without changing the original.