1.
Variables and Data Types
Code:
a = 10 # Integer
b = 5.5 # Float
name = "John" # String
is_valid = True # Boolean
print(a, b, name, is_valid)
Output: 10 5.5 John True
2. Input and Output
Code:
name = input("Enter your name: ")
print("Hello", name)
Input: John
Output: Hello John
3. Operators
Code:
x = 10
y=3
print(x + y, x - y, x * y, x / y, x % y, x ** y)
Output: 13 7 30 3.3333333333333335 1 1000
4. Conditional Statements
Code:
num = int(input("Enter number: "))
if num > 0:
print("Positive")
elif num == 0:
print("Zero")
else:
print("Negative")
Input: 5
Output: Positive
5. Loops (While and For)
Code:
# While loop
i=1
while i <= 5:
print(i)
i += 1
# For loop
for i in range(1, 6):
print(i)
Output: 1 2 3 4 5 (Repeated)
6. Lists
Code:
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)
Output: ['apple', 'banana', 'cherry', 'orange']
7. Tuples
Code:
t = (1, 2, 3)
print(t[0])
Output: 1
8. Dictionaries
Code:
student = {"name": "Alice", "age": 20}
print(student["name"])
Output: Alice
9. Sets
Code:
s = {1, 2, 3, 3}
s.add(4)
print(s)
Output: {1, 2, 3, 4}
10. Functions
Code:
def add(x, y):
return x + y
print(add(5, 3))
Output: 8