Python Exercise questions
What will be the result of the following code: x = 'Welcome' print(x[3:]) -lcome -come -com -co
-come
x = {"name" : "John", "age" : 36} print(type(x)) What is the data type? -complex -dict -set -list
-dict
x = 20.5 print(type(x)) what is the data type? -complex -dict -int -float
-float
txt = 'The best things in life are free!' if 'free' _______ txt: print('Yes, free is present in the text.') Fill in the blank. -contains -present -in -match
-in
x = 5 print(type(x)) This is a data type int. -float -int -complex -bytes
-int
x = ["apple", "banana", "cherry"] print(type(x)) What is the data type? -Frozenset -memoryview -str -list
-list
Which is NOT a legal numeric data type in Python: -int -long -float
-long
Which is NOT a legal variable name? -my-var = 20 -my_var = 20 -Myvar = 20 -_myvar = 20
-my-var = 20
a = 'Join' b = 'the' c = 'party' What is a correct syntax to print 'Join the party'? -print(a + b + c) -print(a + ' ' + b + ' ' + c) -print(a b c)
-print(a + ' ' + b + ' ' + c)
If x = 9, what is a correct syntax to print 'The price is 9.00 dollars'? -print(f'The price is {x:.2f} dollars') -print(f'The price is {x:2} dollars') -print(f'The price is {x:format(2)} dollars')
-print(f'The price is {x:.2f} dollars')
What is a correct syntax to merge variable x and y into variable z? -z = x, y -z = x = y -z = x + y
-z = x + y
What is the correct file extension for Python files? - .pp - .pt - .py
- .py
What is the missing part of the code below to output "Hello World". ________("Hello World") - Print -py -# -python
What is a correct syntax to exit the Python command line interface? - exit() - stop() - end()
- exit()
What is the correct keyword to make the variable x belong to the global scope? def myfunc(): ______ x x = "fantastic" - global - print - type - var
- global
Complete the code block, print "YES" if 5 is larger than 2. if 5 > 2: ____________ - print ("yes") - Print ("yes") -print ("YES") - print("YES")
- print("YES")
What is a correct command line syntax for checking if python is installed on your computer? (And also to check the Python version) - python --version - python ##version - python version
- python --version
What is the correct way to declare a Python variable? -var x = 5 -#x = 5 -$x = 5 -x = 5
- x = 5
What is a correct syntax to print a string in upper case letters? -'Welcome'.upper() -'Welcome'.toUpper() -'Welcome'.toUpperCase()
-'Welcome'.upper()
What will be the result of the following code: print(int(35.88)) -35 -35.88 -36
-35
What will be the result of the following code: print(float(35)) -35 -35.0 -0.35
-35.0
What will be the result of the following code: print(str(35.82)) -35 -35.8 -35.82
-35.82
Consider the following code: a = 4 b = 5 print(a + b) What will be the printed result? -45 -9 -4 + 5
-9
Indentation in Python is for readability only. -True -False
-False
Variable names are not case-sensitive. a = 5 # is the same as A = 5 -True -False
-False
txt = "Hello World" x = txt[1] This function will get the first character of the string. -True -False
-False, 0 is the first character not 1
This is a proper variable with an assigned value: carname = Volvo -True -False
-False, no quotations on the value "Volvo"
What will be the result of the following code: x = 'Welcome' print(x[3:5]) -lcome -come -com -co
-co
txt = "Hello World" txt = txt.replace(h, j) The character H will be replaced with J. -True -False
-False, txt = txt.replace("H", "J") is the correct answer
txt = "Hello World" txt = print(txt.upper()) This will convert the value of txt to upper case. -True -False
-False, txt = txt.upper() is the correct answer
txt = " Hello World " x = txt.whitespace() This will return the string without any whitespace at the beginning or the end. -True -False
-False, x = txt.strip() is the correct answer to remove whitespace
Consider the following code: print('Hello', 'World') What will be the printed result? -Hello, World -Hello World -HelloWorld
-Hello World, No commas in the quotations so the comma in the code is only used to seperate the words hello and woorld when printed
Consider the following code: a = 'Hello' b = 'World' print(a + b) What will be the printed result? -a + b -Hello World -HelloWorld
-HelloWorld, Combines the variables when plus is used
x = 'awesome' def myfunc(): x = 'fantastic' myfunc() print('Python is ' + x) What will be the printed result? -Python is awesome -Python is fantastic
-Python is awesome, Because there is no Global x used in the Def myfunc(). without global x being used, the global scope is used instead.
x = 'awesome' def myfunc(): global x x = 'fantastic' myfunc() print('Python is ' + x) What will be the printed result? -Python is awesome -Python is fantastic
-Python is fantastic, Global x takes control over the global scope since the myfunc() was used properly
print(f'The price is {2 + 3} dollars') What will be the result of the following code? -The price is 23 dollars -The price is 5 dollars -The price is {2 + 3} dollars -The price is 6 dollars
-The price is 5 dollars
You can declare string variables with single or double quotes. x = "John" # is the same as x = 'John' -True -False
-True
age = 36 txt = f"My name is John, and I am {age}" print(txt) This will add a placeholder for the age parameter. -True -False
-True
txt = "Hello World" txt = txt.lower() This will convert the value of txt to lower case. -True -False
-True
txt = "Hello World" x = txt[2:5] This will get the characters from index 2 to index 4 (llo). -True -False
-True
x = "Hello World" print(len(x)) This function will print the length of the string. -True -False
-True
x = "Hello World" print(type(x)) what is the data type? -str -dict -range -bool
-True
x = 5 x = complex(x) This is the correct syntax to convert x into a complex number. -True -False
-True
x = 5 x = float(x) This would convert x into a floating point number. -True -False
-True
x = 5.5 x = int(x) This is the correct syntax to convert x into a integer. -True -False
-True
This is a proper variable with an assigned value: x = 50 -True -False
-True, Numbers dont require quotations
This is the correct syntax to assign values to multiple variables in one line: x,y,z = "Orange", "Banana", "Cherry" -True -False
-True, The commas seperate the multiple variables just like the values
x = ("apple", "banana", "cherry") print(type(x)) What is the data type? -set -float -tuple -bytes
-Tuple
x = 'Welcome' y = 'Coders' print(x + y) What will be the result of the following code? -Welcome Coders -WelcomeCoders -WelcomeCoders
-WelcomeCoders
fruits = ['apple', 'banana', 'cherry'] a, b, c = fruits print(a) What will be the result of this code? -apple -banana -cherry
-apple, It goes in order of the values and variables
x = True print(type(x)) What is the data type? -bool -dict -list -int
-bool
What will be the result of the following code: x = 'Welcome' print(x[3]) -Wel -c -Welcome Welcome Welcome
-c
If x = 5, what is a correct syntax for printing the data type of the variable x? -print(dtype(x)) -print(type(x)) -print(x.dtype())
-print(type(x))
What is a correct syntax to add the value 'Hello World', to 3 variables in one statement? -x, y, z = 'Hello World' -x = y = z = 'Hello World' -x|y|z = 'Hello World'
-x = y = z = 'Hello World'
