Python Exercises
Comments are written with a special character, which one?
#
Print "Hello World" if "a" is not equal to "b"
a = 50 b = 10 if a != b: print("Hello World")
Print "1" if "a" is equal to "b", print "2" if "a" is greater than "b", otherwise print "3"
a = 50 b = 10 if a == b: print("1") elif a > b: print("2") else: print("3")
Print "Yes" if "a" is equal to "b", otherwise print "No"
a = 50 b = 10 if a == b: print("Yes") else: print("No")
Print "Hello World" if "a" is greater than "b"
a = 50 b = 10 if a > b: print("Hello World")
Use the clear() method to empty the car dictionary car = { "brand": "Ford", "model": "Mustang", "year": 1964 }
car.clear()
Use the pop() method to remove "model" from the car dictionary car = { "brand": "Ford", "model": "Mustang", "year": 1964 }
car.pop("model")
Add the key/value pair "color": "red to the car dictionary car = { "brand": "Ford", "model": "Mustang", "year": 1964 }
car["color"] = "red"
Change the "year" value from 1964 to 2018 car = { "brand": "Ford", "model": "Mustang", "year": 1964 }
car["year"] = 2018
Enter code to create a variable named carname and assign the value Volvo to it
carname = "Volvo"
Use the range function to loop through a code set 6 times
for x in range(6): print(x)
In the loop, when the item value is "banana", jump directly to the next item
fruits = ["apple", "banana", "cherry"] for x in fruits: if x == "banana": continue print(x)
Loop through the items in the fruits list
fruits = ["apple", "banana", "cherry"] for x in fruits: print(x)
Use the correct membership operator to check if "apple" is present in the fruits object
fruits = ["apple", "banana"] if "apple" in fruits: print("Yes, apple is a fruit!")
Use the add() method to add "orange" to the fruits set fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
Use the append method to add "orange" to the fruits list. fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
Use the discard() method to remove "banana" from the fruits set fruits = {"apple", "banana", "cherry"}
fruits.discard("banana")
Use the insert method to add "lemon" as the second item in the fruits list. fruits = ["apple", "banana", "cherry"]
fruits.insert(1, "lemon")
Use the remove method to remove "banana" from the fruits list. fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
Use the remove() method to remove "banana" from the fruits set fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
Change the value from "apple" to "kiwi", in the fruits list. fruits = ["apple", "banana", "cherry"]
fruits[0] = "kiwi"
Stop the loop if i is 3
i = 1 while i < 6: if i == 3: break i += 1
Print i as long as i is less than 6
i = 1 while i < 6: print(i) i += 1
Check if "apple" is present in the fruits set fruits = {"apple", "banana", "cherry"}
if ("apple" in fruits): print("Yes, apple is a fruit!")
Use the correct comparison operator to check if 5 is not equal to 10
if 5 != 10: print("5 and 10 is not equal")
Use the correct logical operator to check if at least one of two statements is True
if 5 == 10 or 4 == 4: print("At least one of the statements is true")
Print "Hello" if "a" is equal to "b", AND "c" is equal to "d"
if a == b and c == d: print("Hello")
Print "Hello" if either "a" is equal to "b", or "c" is equal to "d"
if a == b or c == d: print("Hello")
Use the correct method to add multiple items (more_fruits) to the fruits set fruits = {"apple", "banana", "cherry"}
more_fruits = ["orange", "mango", "grapes"] fruits.update(more_fruits)
Enter code to output "Hello World!": blank("Hello World!")
Multiply 10 with 5, and print the result
print(10 * 5)
Divide 10 by 2, and print the result
print(10 / 2)
Use the get() method to print the value of the "model" key of the car dictionary car = { "brand": "Ford", "model": "Mustang", "year": 1964 }
print(car.get("model"))
Print the first item in the fruits tuple. fruits = ("apple", "banana", "cherry")
print(fruits[0])
Print the second item in the fruits list. fruits = ["apple", "banana", "cherry"]
print(fruits[1])
Print the number of items in the fruits tuple. fruits = ("apple", "banana", "cherry")
print(len(fruits))
Return the string without any whitespace at the beginning or the end
txt = " Hello World! " x = txt.strip()
Convert the value of txt to lower case
txt = "Hello World!" txt = txt.lower()
Replace the character H with a J
txt = "Hello World!" txt = txt.replace("H", "J")
Convert the value of txt to upper case
txt = "Hello World!" txt = txt.upper()
Get the first character of the string txt
txt = "Hello World!" x = txt[0]
Get the characters from position 2 to 5 (not included)
txt = "Hello World!" x = txt[2:5]
Use the len method to print the length of the string
x = "Hello World!" print(len(x))
Enter code to display the sum of 5 + 10, using the two variables x and y
x = 5 y = 10 print(x + y)
Enter code to create a variable called z, assign x + y to it, and display the result
x = 5 y = 10 z = x + y print(z)
Enter code to create a variable named x and assign the value 50 to it
x = 50