Python Notes
Python Notes
H
examplesto illustrate each concept.
print("Hello, World!")
Python is dynamically typed, meaning you don’t need to declare data types.
ame = "Alice"
n # String
age = 25 # Integer
height = 5.6 # Float
is_student = True # Boolean
3. Comments
" ""
This is a
multi-line comment
"""
6. Operators
+ - * / % // **
Arithmetic:
== != > < >= <=
Comparison:
and or not
Logical:
= 10
x
y = 3
print(x + y) # 13
print(x ** y) # 1000 (10^3)
ifStatements:
ge = 18
a
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
elif
:
core = 85
s
if score >= 90:
print("Grade A")
elif score >= 80:
print("Grade B")
else:
print("Grade C")
8. Loops
forLoop:
for i in range(5):
print(i) # 0 to 4
whileLoop:
i = 0
while i < 5:
print(i)
i += 1
9. Functions
defkeyword.
Defined using the
def greet(name):
print("Hello", name)
greet("Alice")
10. Lists
11. Dictionaries
Key-value pairs.
person = {
"name": "Alice",
"age": 25
}
print(person["name"])
12. Tuples
Immutable sequences.
13. Sets
Sure! Here's an additional note aboutappending toa list, to be added under theListssection:
📌 Appending to a List
append()method.
You can add items to the end of a list using the
🔹 Syntax:
list_name.append(item)
🔹 Example:
fruits.append("cherry")
print(fruits)
Output:
append()call adds one item to theendof thelist. To add multiple items, use
Each extend()
+=instead.
or
🔹 Example with
extend()
:
fruits.extend(["date", "elderberry"])
print(fruits)
Output:
Here are detailed notes and aPython program exampleto help you understand how to:
✅
Calculate thevolumeandsurface areaof a fishpond
✅
Add or remove water based on user input
✅
Keep track of the current water level
length = 10 # in meters
width = 5 # in meters
depth = 2 # in meters
def add_water(amount):
global current_water
if current_water + amount <= volume:
current_water += amount
else:
def remove_water(amount):
global current_water
current_water -= amount
else:
def menu():
while True:
print("4. Exit")
choice = input("Choose an option (1-4): ")
add_water(amount)
remove_water(amount)
print("Exiting program.")
break
else:
menu()
📝 Notes:
volume =
● You can modify dimensions or extend to other shapes (like cylindrical using
π × r² × h
).
● G
lobal variables are used here for simplicity; you can encapsulate in a class for better
structure.