Python Basic
Python Basic
Variable:
Difference:
Summary of Key Differences
Feature List Tuple Dictionary Set
Ordered Yes Yes No No
Mutable Yes No Yes Yes
Keys: No,
Duplicates Allowed Allowed Not allowed
Values: Yes
Syntax [] () {key: value} {} or set()
Indexing By index By index By key No indexing
Ordered collection Ordered collection that Key-value Unique items collection
Use Case
that can change should not change pairs and set operations
Print:
Instructor : Musfiqul Hamid Showmik
STEM HERO BD
Input :
if grade:
print(f"Your grade is: {grade}")
else:
Instructor : Musfiqul Hamid Showmik
STEM HERO BD
print("Invalid score.")
Loop :
# Looping Through a Sequence # Looping with Range
print("Looping Through a Sequence:") print("\nLooping with Range:")
fruits = ["apple", "banana", "cherry"] # Range from 0 to 4
for fruit in fruits: for i in range(5):
print(fruit) print(i)
# Range from 1 to 5
# Looping With Index print("\nRange from 1 to 5:")
print("\nLooping With Index:") for i in range(1, 6):
for index, fruit in enumerate(fruits): print(i)
print(f"Index: {index}, Fruit: {fruit}")
# Range with a step of 2
print("\nRange with a step of 2:")
for i in range(0, 10, 2):
print(i)