CodingNic

Getting Started with Python

Boolean Logic Exercises

Getting Started with Python 20 min read

Boolean Logic Exercises

Objectives

This chapter introduces no new concepts. It’s a chance to put Module 1 together: variables, data types, strings, and boolean logic.

Exercises

Write Python code to do each of the following:

  1. Declare a variable called first and assign it the value "Hello World".
  2. Write a comment that says This is a comment.
  3. Print the message I AM A COMPUTER! to the terminal.
  4. Write an if statement that checks whether 1 is less than 2 and 4 is greater than 2. If both are true, print Math is fun.
  5. Assign a variable called nope to an absence of value.
  6. Use and to combine True and False.
  7. Calculate the length of the string "What's my length?".
  8. Convert the string "i am shouting" to uppercase.
  9. Convert the string "1000" to the number 1000.
  10. Combine the number 4 with the string "real" to produce "4real".
  11. Predict, then check, the output of 3 * "cool".
  12. Predict, then check, the output of 1 / 0.
  13. Determine the type of [].
  14. Ask the user for their name with input(), and store it in a variable called name.
  15. Ask the user for a number. Print That number is less than 0! if negative, That number is greater than 0! if positive, or You picked 0! otherwise.
  16. Find the index of "l" in "apple".
  17. Check whether "y" appears anywhere in "xylophone".
  18. Check whether a string called my_string is entirely lowercase.

Recap

You can now assign variables, work with Python’s core data types, manipulate strings, and write conditional logic. That’s the toolkit Module 1 set out to build.

Next module: Python’s first real data structure, lists.