Python Flashcards
Printing outputs
print (“hello world”) # This prints hello world
Variables and data types
name = “Ayush” # string (text)
age = 14 # integer (whole number)
height = 1.65 # float (decimal number)
student = True # boolean (true/false)
print(“Name:”, name)
print(“Age:”, age)
print(“Height:”, height)
print(“Student:”, student)
User input
name = input(“enter you name”)
print(“hello “ + name)
age = int(input(“Enter your age: “))
print(“Next year, you will be”, age + 1)
Arithmetic operations
a = 10
b = 3
print(a + b) # Addition → 13
print(a - b) # Subtraction → 7
print(a * b) # Multiplication → 30
print(a / b) # Division → 3.333…
print(a // b) # Floor division (no decimals) → 3
print(a % b) # Modulus (remainder) → 1
print(a ** b) # Exponentiation (power) → 10³ = 1000
Comparison operations
print(a == b) # Equal to → False
print(a != b) # Not equal to → True
print(a > b) # Greater than → True
print(a < b) # Less than → False
print(a >= b) # Greater than or equal → True
print(a <= b) # Less than or equal → False
Logical operations
x = True
y = False
print(x and y) # AND → False (both must be True)
print(x or y) # OR → True (at least one True)
print(not x) # NOT → False (opposite)
If statement
age = int(input(“Enter your age: “))
if age < 18:
print(“You are a minor.”)
elif age == 18:
print(“You just became an adult!”)
else:
print(“You are an adult.”)
While loop
count = 1
while count <= 5:
print(count)
count += 1 # Increases count by 1
For loop
for i in range(1, 6):
print(i)
fruits = [“apple”, “banana”, “cherry”]
for fruit in fruits:
print(fruit)
Creating lists
fruits = [“apple”, “banana”, “cherry”]
print(fruits) # Output: [‘apple’, ‘banana’, ‘cherry’]
Accessing elements
fruits = [“apple”, “banana”, “cherry”]
print(fruits) # Output: [‘apple’, ‘banana’, ‘cherry’]
print(fruits[0]) # Output: apple
print(fruits[1]) # Output: banana
print(fruits[-1]) # Output: cherry (last item)
Looping through a list
fruits = [“apple”, “banana”, “cherry”]
print(fruits) # Output: [‘apple’, ‘banana’, ‘cherry’]
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
Modifying a list
fruits = [“apple”, “banana”, “cherry”]
print(fruits) # Output: [‘apple’, ‘banana’, ‘cherry’]
fruits[1] = “blueberry” # Changes ‘banana’ to ‘blueberry’
print(fruits) # Output: [‘apple’, ‘blueberry’, ‘cherry’]
Adding items to a list
fruits = [“apple”, “banana”, “cherry”]
print(fruits) # Output: [‘apple’, ‘banana’, ‘cherry’]
fruits.append(“orange”) # Adds ‘orange’ to the end
print(fruits)
Removing items from a list
fruits = [“apple”, “banana”, “cherry”]
print(fruits) # Output: [‘apple’, ‘banana’, ‘cherry’]
fruits.remove(“apple”) # Removes ‘apple’
print(fruits)
fruits.pop(1) # Removes the second item (index 1)
print(fruits)