Python CONDITIONALS Exercise for Beginners and Intermediate

Enable the edit mode with double click on the editor.
For answers, please select the invisible text right after Answer:


Write code that prints out True! if x has 30 characters or more.

x = "Python is actually one of the best programming languages for beginners."
Output: True!

Answer:

if len(x) >= 30:
   print("True!")

Write code that prints out True! if x is a string and the first character in the string is P.

x = "Python has a very simple and elegant syntax."
Output: True!

Answer:

if type(x) is str and x.startswith("P"):
   print("True!")

Write code that prints out True! if at least one of the following conditions occurs:
– the string contains the character g
– the string contains the character s at least three times

x = "Python has a very simple and elegant syntax."
Output: True!

Answer:

if "g" in x or x.count("s") >= 3:
   print("True!")

Write code that prints out True! If the last 3 characters of the string are all digits, and prints out False! otherwise.

Hint! Use the appropriate method to check if the requested string slice contains digits only.

x = "Python 3 has an easier syntax compared to Python 2."
Output: False!

Answer:

if x[-3:].isdigit():
   print("True!")
else:
   print("False!")

Write code that prints out True! if x has at least 8 elements and the element positioned at index 6 is a floating-point number and prints out False! otherwise.

x =  [115, 115.9, 116.01, ["length", "width", "height"], 109, 115, 119.5, ["length", "width", "height"]]
Output: True!

Answer:

if len(x) >= 8 and type(x[6]) is float:
   print("True!")
else:
   print("False!")

Write code that prints out True! if one of the following two conditions are satisfied and prints out False! otherwise.

• the third string of the first list in x ends with the letter h
• the second string of the second list in x also ends with the letter h

x = [115, 115.9, 116.01, ["length", "width", "height"], 109, 115, 119.5, ["length", "width", "height"]]
Output: True!

Answer:

if x[3][2].endswith("h") or x[7][1].endswith("h")
   print("True!")
else:
   print("False!")

Write code that prints out True! if the largest value among the first 3 elements of the list is less than or equal to the smallest value among the next 3 elements of the list. Otherwise, print out False!

Hint! Use the appropriate slices to make the comparison.

x = [115, 115.9, 116.01, 109, 115, 119.5, ["length", "width", "height"]]
Output: False!

Answer:

if max(x[:3]) <= min(x[3:6]):
   print("True!")
else:
   print("False!")

Write code that prints out True! if 115 appears at least once inside the list or if it is the first element in the list. Otherwise, print out False!

Hint! Use the appropriate method to check if 115 is the first element in the list.

x = [115, 115.9, 116.01, 109, 115, 119.5, ["length", "width", "height"]]
Output: True!

Answer:

if x.count(115) >= 1 or x.index(115) == 0:
   print("True!")
else:
   print("False!")

Write code that prints out True! if the value associated with key number 5 is Perl or the number of key-value pairs in the dictionary divided by 5 returns a remainder less than 2. Otherwise, print out False!

x = {1: "Python", 2: "Java", 3: "Javascript", 4: "Ruby", 5: "Perl", 6: "C#", 7: "C++"}
Output: True!

Answer:

if x[5] == "Perl" or len(x) % 5 < 2:
   print("True!")
else:
   print("False!")

Write code that prints out True! if the sum of all the keys in the dictionary is less than the number of characters of the string obtained by concatenating the values associated with the first 5 keys in the dictionary. Otherwise, print out False!

x = {1: "Python", 2: "Java", 3: "Javascript", 4: "Ruby", 5: "Perl", 6: "C#", 7: "C++"}
Output: True!

Answer:

if sum(x) < len(x[1] + x[2] + x[3] + x[4] + x[5]):
   print("True!")
else:
   print("False!")

Write code that prints out True! if the 3rd element of the first range is less than 2, prints out False! if the 5th element of the first range is 5, and prints out None! otherwise.

x = [list(range(5)), list(range(5,9)), list(range(1,10,3))]
Output: None!

Answer:

if x[0][2] < 2:
   print("True!")
elif x[0][4] == 5:
   print("False!")
else:
   print("None!")

Write code that prints out True! if the sum of all the elements of the first range is greater than the sum of all the elements of the third range, prints out False! if the largest element of the second range is greater than the largest element of the third range, and prints out None! otherwise.

x = [list(range(5)), list(range(5,9)), list(range(1,10,3))]
Output: False!

Answer:

if sum(x[0]) > sum(x[2]):
   print("True!")
elif max(x[1]) > max(x[2]):
   print("False!")
else:
   print("None!")

Write code that prints out True! if the largest element of the first range minus the second element of the 3rd range is equal to the first element of the first range, prints out False! if the length of the first range minus the length of the 2nd range is equal to the first element of the 3rd range, prints out Maybe! if the sum of all the elements of the 3rd range divided by 2 returns a remainder of 0, and prints out None! otherwise.

x = [list(range(5)), list(range(5,9)), list(range(1,10,3))]
Output: True!

Answer:

if max(x[0]) - x[2][1] == x[0][0]:
   print("True!")
elif len(x[0]) - len(x[1]) == x[2][0]:
   print("False!")
elif sum(x[2]) % 2 == 0:
   print("Maybe!")
else:
   print("None!")

Write code that prints out True! if the number of characters of the smallest value in the dictionary is equal to the number of occurrences of letter a in the value at key 3, and prints out False! otherwise.

x = {1: "Python", 2: "Java", 3: "Javascript", 4: "Ruby", 5: "Perl", 6: "C#", 7: "C++"}
Output: True!

Answer:

if len(min(x.values())) == x[3].count("a"):
   print("True!")
else:
   print("None!")

Leave a Reply

Prev
Python DICTIONARIES Exercise for Beginners

Python DICTIONARIES Exercise for Beginners

Enable the edit mode with double click on the editor

Next
How to Create GUI Application with Python and Tkinter
How to Create GUI Application with Python and Tkinter

How to Create GUI Application with Python and Tkinter

Tkinter is python’s de-facto standard GUI package

You May Also Like