Skip to content
elevatedevco

Data & MLOps

Python basics

Learn Python basics: 40 practice questions and answers with an interactive quiz — from the offline AI Learning app.

Intermediate40 questions40 quiz questions

Questions & Answers

Tap a question to reveal its answer. Written for self-study and interview preparation.

What is a variable in Python?

A variable is a name that stores a value so you can use it later in your program. For example, age = 12 stores the number 12 in a variable called age. You can change what a variable holds at any time.

age = 12
print(age)
Which Python function is used to display text on the screen?

The print() function displays text or values on the screen. You put what you want to show inside the parentheses, like print("Hello"). It is one of the first functions beginners learn.

print("Hello")
What data type is the value 7 in Python?

The value 7 is an integer, which is a whole number without a decimal point. In Python, integers are written without quotes. You can do math with them, like adding or subtracting.

x = 7
print(type(x))
What is a string in Python?

A string is text made of characters, like letters, numbers, or symbols, written inside quotation marks. For example, "cat" and 'Python3' are strings. Strings are used to store words and sentences.

name = "Maya"
How do you write a comment in Python?

You write a comment by starting a line with the # symbol. Python ignores everything after # on that line, so comments are notes for humans, not instructions for the computer. They help explain what your code does.

# This is a comment
print("Hi")
What does the input() function do in Python?

The input() function lets the program pause and wait for the user to type something, then returns what they typed as a string. You can store that text in a variable. For example, name = input("Your name? ") saves the user's reply.

name = input("Your name? ")
What will 10 / 3 give in Python 3?

In Python 3, the / operator does normal division and gives a float (decimal) result. So 10 / 3 gives about 3.3333333333333335. If you want only the whole number part, you use // instead.

print(10 / 3)
What does the // operator do in Python?

The // operator does floor division, which divides and then drops the decimal part, keeping only the whole number. For example, 7 // 2 gives 3, not 3.5. It is useful when you only need whole results.

print(7 // 2)
What does the % (modulo) operator return?

The % operator returns the remainder after dividing one number by another. For example, 10 % 3 gives 1 because 10 divided by 3 leaves a remainder of 1. It is often used to check if a number is even or odd.

print(10 % 3)
What is a list in Python?

A list is an ordered collection that can hold many values in one variable, written inside square brackets. For example, fruits = ["apple", "banana"] stores two items. You can add, remove, or change items in a list.

fruits = ["apple", "banana"]
What is the index of the first item in a Python list?

In Python, list indexing starts at 0, so the first item has index 0. For a list like colors = ["red", "blue"], colors[0] gives "red". This is called zero-based indexing.

colors = ["red", "blue"]
print(colors[0])
What keyword starts an if statement in Python?

The keyword if starts a conditional statement that runs code only when a condition is True. For example, if score > 50: runs the next indented lines when score is greater than 50. It lets programs make decisions.

if score > 50:
    print("Pass")
Why is indentation important in Python?

Python uses indentation (spaces at the start of a line) to group code into blocks, instead of curly braces. Lines indented the same amount belong to the same block, such as the body of a loop or if statement. Wrong indentation causes an error.

if True:
    print("Indented")
What does a for loop do in Python?

A for loop repeats a block of code once for each item in a sequence, like a list or a range of numbers. For example, for i in range(3): runs three times. It saves you from writing the same code over and over.

for i in range(3):
    print(i)
What does range(5) produce in a for loop?

range(5) produces the numbers 0, 1, 2, 3, and 4, which is five numbers starting at 0. It does not include 5, because the count stops just before it. It is commonly used to repeat a loop a set number of times.

for i in range(5):
    print(i)
What is a while loop?

A while loop repeats a block of code as long as a condition stays True. For example, while count < 5: keeps running until count is no longer less than 5. You must change the condition inside the loop, or it will run forever.

count = 0
while count < 5:
    count = count + 1
How do you define a function in Python?

You define a function using the def keyword, followed by a name and parentheses. The code inside the function is indented, and it runs only when you call the function by its name. Functions let you reuse code easily.

def greet():
    print("Hello")
What does the return statement do in a function?

The return statement sends a value back from a function to the place that called it. After return runs, the function stops. For example, return a + b gives the sum back so it can be stored or printed.

def add(a, b):
    return a + b
What data type is the value True in Python?

True is a Boolean value, which can only be True or False. Booleans are used to represent yes/no or on/off conditions in a program. They often come from comparisons like 5 > 3.

x = True
print(type(x))
What does the len() function do?

The len() function returns the number of items in something, like the number of characters in a string or items in a list. For example, len("cat") gives 3. It is handy for counting.

print(len("cat"))
What is the result of "Hello" + " World" in Python?

Using + on two strings joins them together, which is called concatenation. So "Hello" + " World" gives "Hello World". The strings are combined exactly as written, including the space.

print("Hello" + " World")
What is the difference between = and == in Python?

A single = assigns a value to a variable, like x = 5. A double == compares two values to check if they are equal, returning True or False. Mixing them up is a common beginner mistake.

x = 5
print(x == 5)
What does the float data type represent?

A float represents a number with a decimal point, like 3.14 or 0.5. Floats are used when you need fractions or precise measurements. They are different from integers, which are whole numbers.

pi = 3.14
print(type(pi))
How can you convert the string "5" into the number 5?

You use the int() function to convert a string of digits into an integer, like int("5") which gives 5. This is useful because input() always returns text, even when the user types numbers. Without converting, you cannot do math with it.

num = int("5")
print(num + 1)
What does the elif keyword mean in Python?

elif stands for 'else if' and lets you check another condition when the previous if was False. You can have several elif branches to test many possibilities in order. The first matching condition runs its block.

if x > 0:
    print("pos")
elif x == 0:
    print("zero")
How do you add an item to the end of a Python list?

You use the append() method to add an item to the end of a list, like fruits.append("mango"). This makes the list one item longer. It is one of the most common list operations.

fruits = ["apple"]
fruits.append("mango")
What symbol is used for exponents (powers) in Python?

Python uses the ** operator for exponents, so 2 ** 3 means 2 raised to the power 3, which equals 8. This is different from the ^ symbol used in some other contexts. It is handy for squaring or cubing numbers.

print(2 ** 3)
What is a dictionary in Python?

A dictionary stores data as key-value pairs, written inside curly braces. For example, ages = {"Sam": 12} links the key "Sam" to the value 12. You look up values using their keys, like ages["Sam"].

ages = {"Sam": 12}
print(ages["Sam"])
What does the bool value of an empty list [] equal in Python?

An empty list is treated as False when used in a condition, because Python considers empty collections to be 'falsy'. A list with at least one item is treated as True. This lets you check if a list has anything in it.

items = []
if not items:
    print("empty")
What does the break statement do inside a loop?

The break statement immediately stops the loop and jumps to the code after it, even if the loop has not finished. It is useful when you find what you are looking for and want to stop early. Only the nearest enclosing loop is stopped.

for i in range(10):
    if i == 3:
        break
What does the continue statement do in a loop?

The continue statement skips the rest of the current loop step and moves on to the next one. Unlike break, it does not stop the whole loop. It is useful for ignoring certain values.

for i in range(5):
    if i == 2:
        continue
    print(i)
Which function tells you the data type of a value in Python?

You can check a value's type with the type() function, which tells you what kind of data it is. For example, type(3.5) shows it is a float. This helps you understand and debug your code.

print(type(3.5))
In machine learning, why might you store training examples in a Python list?

A list is a simple way to keep many data examples together in order, so a program can loop through them one by one. Computers learn from examples by processing each item, and lists make that easy. For larger datasets, people often use specialized libraries, but lists are a great starting point.

examples = [1, 2, 3, 4]
for x in examples:
    print(x)
What is the output of print(3 > 5) in Python?

The expression 3 > 5 is a comparison that checks if 3 is greater than 5, which is not true. So Python prints False. Comparisons always result in a Boolean value, either True or False.

print(3 > 5)
What is the and keyword used for in Python?

The and keyword combines two conditions and is True only when both conditions are True. For example, age > 10 and age < 20 is True only for ages between 11 and 19. It helps make more specific decisions.

age = 15
print(age > 10 and age < 20)
What does the str() function do?

The str() function converts a value into a string (text). For example, str(25) gives "25", which you can then join with other text. This is useful when you want to print a number next to words.

age = 12
print("Age: " + str(age))
Why should you give variables clear, meaningful names?

Clear names like student_score make code easy to read and understand, both for you and others. Vague names like x or a make it hard to remember what the value means. Good names reduce mistakes and make programs easier to fix.

student_score = 95
When a program uses people's data, why is privacy important to think about?

Data can include personal information, so collecting or sharing it without permission can harm people's privacy. Responsible programmers only use data they are allowed to use and keep it safe. Thinking about ethics helps build technology people can trust.

What is the result of int(7.9) in Python?

The int() function converts a float to an integer by removing the decimal part, not rounding it. So int(7.9) gives 7, not 8. This is called truncation toward zero.

print(int(7.9))
What does the in keyword check when used like "a" in "cat"?

The in keyword checks whether something is found inside another value, such as a character in a string or an item in a list. "a" in "cat" returns True because the letter a is in the word cat. It is a quick way to search.

print("a" in "cat")

Put it into practice with AI Learning

This topic — and hundreds more — comes from our free, offline AI Learning app. Practice Q&A, quizzes and cheat sheets anywhere, no account needed.

  • Free
  • No account
  • Works offline
Coming soon toGoogle Play