Python Flashcards

1
Q

Printing outputs

A

print (“hello world”) # This prints hello world

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Variables and data types

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

User input

A

name = input(“enter you name”)
print(“hello “ + name)
age = int(input(“Enter your age: “))
print(“Next year, you will be”, age + 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Arithmetic operations

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Comparison operations

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Logical operations

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If statement

A

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.”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

While loop

A

count = 1
while count <= 5:
print(count)
count += 1 # Increases count by 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

For loop

A

for i in range(1, 6):
print(i)

fruits = [“apple”, “banana”, “cherry”]
for fruit in fruits:
print(fruit)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Creating lists

A

fruits = [“apple”, “banana”, “cherry”]
print(fruits) # Output: [‘apple’, ‘banana’, ‘cherry’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Accessing elements

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Looping through a list

A

fruits = [“apple”, “banana”, “cherry”]
print(fruits) # Output: [‘apple’, ‘banana’, ‘cherry’]

for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Modifying a list

A

fruits = [“apple”, “banana”, “cherry”]
print(fruits) # Output: [‘apple’, ‘banana’, ‘cherry’]

fruits[1] = “blueberry” # Changes ‘banana’ to ‘blueberry’
print(fruits) # Output: [‘apple’, ‘blueberry’, ‘cherry’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Adding items to a list

A

fruits = [“apple”, “banana”, “cherry”]
print(fruits) # Output: [‘apple’, ‘banana’, ‘cherry’]

fruits.append(“orange”) # Adds ‘orange’ to the end
print(fruits)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Removing items from a list

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Nested lists

A

students = [
[“Alice”, 85],
[“Bob”, 92],
[“Charlie”, 78],
[“David”, 88]
]
In this example, each inner list contains a student’s name and their score.

17
Q

Accessing nested list items

A

students = [
[“Alice”, 85],
[“Bob”, 92],
[“Charlie”, 78],
[“David”, 88]
]
print(students[0]) # Prints: [‘Alice’, 85] (first student’s name and score)
print(students[0][0]) # Prints: Alice (first student’s name)
print(students[0][1]) # Prints: 85 (first student’s score)

18
Q

Modifying nested list items

A

students = [
[“Alice”, 85],
[“Bob”, 92],
[“Charlie”, 78],
[“David”, 88]
]
students[1][1] = 95 # Update Bob’s score to 95
print(students) # Output: [[‘Alice’, 85], [‘Bob’, 95], [‘Charlie’, 78], [‘David’, 88]]

19
Q

Looping through nested lists

A

students = [
[“Alice”, 85],
[“Bob”, 95],
[“Charlie”, 78],
[“David”, 88]
]
for student in students:
print(f”Name: {student[0]}, Score: {student[1]}”)

Output:
Name: Alice, Score: 85
Name: Bob, Score: 95
Name: Charlie, Score: 78
Name: David, Score: 88

20
Q

Creating a dictionary

A

student_scores = {
“Ayush”: 97,
“Ansha”: 96,
“Sneha”: 95,
“Kiran”: 94,
“Prema”: 93
}

21
Q

Accessing values in a dictionary

A

student_scores = {
“Ayush”: 97,
“Ansha”: 96,
“Sneha”: 95,
“Kiran”: 94,
“Prema”: 93
}
print(student_scores[“Ayush”]) # Output: 97

22
Q

Adding or updating values in a dictionary

A

student_scores = {
“Ayush”: 97,
“Ansha”: 96,
“Sneha”: 95,
“Kiran”: 94,
“Prema”: 93
}
student_scores[“Alice”] = 90 # Adds a new student
student_scores[“Ayush”] = 100 # Updates Ayush’s score

23
Q

Removing items in a dictionary

A

student_scores = {
“Ayush”: 97,
“Ansha”: 96,
“Sneha”: 95,
“Kiran”: 94,
“Prema”: 93
}
del student_scores[“Prema”] # Removes Prema from the dictionary
print(student_scores)

Or using pop:
removed_score = student_scores.pop(“Kiran”) # Removes Kiran and returns the value
print(removed_score) # Output: 94

24
Q

Looping through a dictionary (keys)

A

student_scores = {
“Ayush”: 97,
“Ansha”: 96,
“Sneha”: 95,
“Kiran”: 94,
“Prema”: 93
}
for student in student_scores:
print(student) # Prints each student’s name (key)

25
Q

Looping through a dictionary (values)

A

student_scores = {
“Ayush”: 97,
“Ansha”: 96,
“Sneha”: 95,
“Kiran”: 94,
“Prema”: 93
}
for score in student_scores.values():
print(score) # Prints each student’s score (value)