Python: Basic Syntax Flashcards
What is the correct syntax to print “Hello World!” in Python?
A. echo “Hello World!”
B. printf(“Hello World!”)
C. print(“Hello World!”)
D. System.out.println(“Hello World!”)
B. printf(“Hello World!”)
Explanation: The print() function is used to output text in Python. The correct syntax to print “Hello World!” is print(“Hello World!”).
What will print(a[1]) output if a = “Hello, World!”?
A. H
B. e
C. l
D. o
B. e
Explanation: In Python, string indexing starts at 0. Therefore, a[1] refers to the second character in the string “Hello, World!”, which is “e”.
What does print(len(a)) output if a = “Hello, World!”?
A. 11
B. 12
C. 13
D. 14
C. 13
Explanation: The len() function returns the length of a string. The string “Hello, World!” has 13 characters, including spaces and punctuation.
What does print(“free” in txt) output if txt = “The best things in life are free!”?
A. True
B. False
C. None
D. Error
A. True
Explanation: The in operator checks if a substring exists within a string. Since “free” is present in the string txt, the output is True.
What will print(b[2:5]) output if b = “Hello, World!”?
A. Hel
B. llo
C. lo,
D. o,
B. llo
Explanation: String slicing b[2:5] extracts characters from index 2 to 4 (inclusive of 2, exclusive of 5). Therefore, it outputs “llo”.
What does print(a.upper()) output if a = “Hello, World!”?
A. hello, world!
B. HELLO, WORLD!
C. Hello, World!
D. hELLO, wORLD!
B. HELLO, WORLD!
Explanation: The upper() method converts all characters in a string to uppercase. Therefore, a.upper() outputs “HELLO, WORLD!”.
What does print(a.replace(“H”, “J”)) output if a = “Hello, World!”?
A. Jello, World!
B. Hello, World!
C. Jello, world!
D. Jello, world!
A. Jello, World!
Explanation: The replace() method replaces all occurrences of the specified substring with another substring. Therefore, a.replace(“H”, “J”) outputs “Jello, World!”.
What does print(a.split(“,”)) output if a = “Hello, World!”?
A. [‘Hello World!’]
B. [‘Hello’, ‘ World!’]
C. [‘Hello’, ‘World!’]
D. [‘Hello’, ‘World’]
B. [‘Hello’, ‘ World!’]
Explanation: The split() method splits a string into a list using the specified delimiter. Therefore, a.split(“,”) outputs [‘Hello’, ‘ World!’].
What does print(a + s + d) output if a = “Papasa”, s = “ba”, and d = “ako?”?
A. Papasa ba ako?
B. Papasabaako?
C. Papasa baako?
D. Papasa ba ako
B. Papasabaako?
Explanation: The + operator concatenates strings without adding spaces. Therefore, a + s + d outputs “Papasabaako?”.
What does print(a + s + d) output if a = 1, s = 2, and d = 3?
A. 123
B. 6
C. 5
D. Error
B. 6
Explanation: The + operator adds numeric values. Therefore, a + s + d outputs 6.
What does print(a + s) output if a = “Okay na to” and s = 5?
A. Okay na to5
B. Okay na to 5
C. Error
D. TypeError
D. TypeError
Explanation: Concatenating a string with an integer using + results in a TypeError. Therefore, print(a + s) outputs a TypeError.
What does print(x) output if x = 3?
A. 3
B. x
C. Error
D. None
A. 3
Explanation: The variable x is assigned the value 3. Therefore, print(x) outputs 3.
What is the data type of c if c = float(5)?
A. int
B. float
C. str
D. bool
B. float
Explanation: The float() function converts the integer 5 to a floating-point number. Therefore, c is of type float.
Which of the following is a valid variable name in Python?
A. 1variable
B. variable_1
C. variable-1
D. variable 1
B. variable_1
Explanation: Variable names must start with a letter or underscore and can only contain alphanumeric characters and underscores. Therefore, variable_1 is valid.
What does print(“Python is “ + x) output if x = “awesome” and global x is set to “fantastic” in a function?
A. Python is awesome
B. Python is fantastic
C. Python is None
D. Error
B. Python is fantastic
Explanation: The global keyword allows the variable x to be modified globally within the function. Therefore, print(“Python is “ + x) outputs “Python is fantastic”.
What is the data type of z if z = 4j?
A. int
B. float
C. complex
D. str
C. complex
Explanation: The value 4j represents a complex number in Python. Therefore, z is of type complex.
What does print(random.randrange(1, 10)) output?
A. A fixed number between 1 and 10
B. A random number between 1 and 9
C. A random number between 1 and 10
D. Error
B. A random number between 1 and 9
Explanation: The randrange() function from the random module generates a random number between the specified range, inclusive of the start and exclusive of the end. Therefore, it outputs a random number between 1 and 9.
What does print(10 > 9) output?
A. True
B. False
C. None
D. Error
A. True
Explanation: The comparison operator > checks if 10 is greater than 9. Since this condition is true, the output is True.
What does print(not(x < 5 and x < 10)) output if x = 3?
A. False
B. True
C. None
D. Error
A. False
Explanation: The not operator reverses the result of the expression. Since x < 5 and x < 10 is True, not(True) is False.
What does x & y do in Python?
A. Sets each bit to 1 if both bits are 1
B. Sets each bit to 0 if both bits are 0
C. Sets each bit to 1 if one of two bits is 1
D. Inverts all the bits
A. Sets each bit to 1 if both bits are 1
Explanation: The bitwise AND operator & sets each bit to 1 if both corresponding bits are 1. Otherwise, it sets the bit to 0.
What does x | y do in Python?
A. Sets each bit to 1 if both bits are 1
B. Sets each bit to 1 if one of two bits is 1
C. Sets each bit to 0 if both bits are 0
D. Inverts all the bits
B. Sets each bit to 1 if one of two bits is 1
Explanation: The bitwise OR operator | sets each bit to 1 if one of the two corresponding bits is 1. If both bits are 0, it sets the bit to 0.
What does x ^ y do in Python?
A. Sets each bit to 1 if only one of two bits is 1
B. Sets each bit to 1 if both bits are 1
C. Sets each bit to 0 if both bits are 0
D. Inverts all the bits
A. Sets each bit to 1 if only one of two bits is 1
Explanation: The bitwise XOR operator ^ sets each bit to 1 if only one of the two corresponding bits is 1. If both bits are the same, it sets the bit to 0.
What does ~x do in Python?
A. Sets each bit to 1 if both bits are 1
B. Sets each bit to 1 if one of two bits is 1
C. Inverts all the bits
D. Sets each bit to 0 if both bits are 0
C. Inverts all the bits
Explanation: The bitwise NOT operator ~ inverts all the bits of the operand, changing 1s to 0s and 0s to 1s.
What does x «_space;2 do in Python?
A. Shifts bits to the right
B. Shifts bits to the left by pushing zeros in from the right
C. Inverts all the bits
D. Sets each bit to 1 if both bits are 1
B. Shifts bits to the left by pushing zeros in from the right
Explanation: The bitwise left shift operator «_space;shifts bits to the left by pushing zeros in from the right, letting the leftmost bits fall off.