Grade IX Python Variables
Grade IX Python Variables
print("Sum:", x + y)
print("Real part of z:", z.real)
print("Imaginary part of z:", z.imag)
✅ 2. Sequence Types
▶ str
name = "Python"
print(name.upper()) # Output: PYTHON
print(name[0]) # Output: P
▶ list
fruits = ["apple", "banana", "cherry"]
fruits.append("mango")
print(fruits) # ['apple', 'banana', 'cherry', 'mango']
▶ tuple
colors = ("red", "green", "blue")
print(colors[1]) # green
💡 Try this:
names = ["John", "Jane", "Doe"]
names[1] = "Janet"
print("Updated list:", names)
✅ 4. Set Types
▶ set
nums = {1, 2, 3, 2, 1}
print(nums) # Output: {1, 2, 3} (no duplicates)
▶ frozenset
frozen = frozenset([1, 2, 3])
print(frozen)
💡 Try this:
a = {1, 2, 3}
b = {3, 4, 5}
print("Union:", a | b)
print("Intersection:", a & b)
print(bool(x)) # True
print(bool(y)) # False
💡 Try this:
a=5
b=7
print("Is a < b?", a < b)
✅ 6. Binary Types
▶ bytes, bytearray, memoryview
b = bytes("hello", "utf-8")
print(b) # b'hello'
ba = bytearray(b)
ba[0] = 72 # 'h' → 'H'
print(ba) # bytearray(b'Hello')
✅ Practice Set
📝 Questions:
1. Create a list of 3 of your favorite movies.
2. Convert your name into uppercase using a string method.
3. Create a dictionary with your name, age, and city.
4. Create a set with some duplicate numbers and print it.
5. Print whether 25 is greater than 50 using Boolean.