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:
- Declare a variable called
firstand assign it the value"Hello World". - Write a comment that says
This is a comment. - Print the message
I AM A COMPUTER!to the terminal. - Write an
ifstatement that checks whether1is less than2and4is greater than2. If both are true, printMath is fun. - Assign a variable called
nopeto an absence of value. - Use
andto combineTrueandFalse. - Calculate the length of the string
"What's my length?". - Convert the string
"i am shouting"to uppercase. - Convert the string
"1000"to the number1000. - Combine the number
4with the string"real"to produce"4real". - Predict, then check, the output of
3 * "cool". - Predict, then check, the output of
1 / 0. - Determine the type of
[]. - Ask the user for their name with
input(), and store it in a variable calledname. - Ask the user for a number. Print
That number is less than 0!if negative,That number is greater than 0!if positive, orYou picked 0!otherwise. - Find the index of
"l"in"apple". - Check whether
"y"appears anywhere in"xylophone". - Check whether a string called
my_stringis 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.