Python Basics Practice

Printing

Print the word “cats” and the word “dogs” with a line break between them, but only use one line of code. Include an escape code “\n” in the string to make them appear on separate lines.

Print the sentence “Cats eat treats for breakfast”, except do it by concatenating variables together with some literal text.
(use “+” to concatenate strings together)

# starter code
c = "cats"
t = "treats"

# now print the sentence using these variables 
# along with string literals (quoted text)

Print some text together with a number variable. Separate the text and the variable by comma.

# starter code
x = 99
# print "the secret number is 99"
# but use x instead of 99 in your print

Now print the same thing again (“the secret number is 99”), but don’t use any commas, and still don’t literally include the number 99. Use this instead:
+ str(x)
(You are converting x to a string so it can be concatenated with another string literal)

Did you know that you can multiply a string? Use the “*” operator to print a sentence multiplied by 100.

Comments

Type a note to yourself in the code. Put a hashtag (octothorpe) in front of it so that Python will not try to read it as a line of code.

Now make a multi-line comment, which starts and ends with three quotes.

Run your code to make sure Python isn’t giving error messages on these commented lines.

Highlight a line of code and push Control + \. This should also make a line into a comment.

Push Control + \ again, and the comment should turn back into a regular line of code (the # is removed).

Math

Create three variables that contain whole numbers. Pick any two, and print their sum (make Python do the math!).

Pick another two and print their difference, then pick the last combination of two variables and print their product.

Print the type of one of your variables by doing something like this:

print(type(x))

It should tell you that your variable is an int (integer).

Now create a decimal number variable, and print the type of this variable. It should be a float (floating point decimal number).

Python has three ways to divide the numbers, which use the following three operators:
/
//
%

Create two whole number variables, and divide them in all three ways. Try to figure out what it’s doing in each case. If you chose 8 and 3, that is a set of number that will force you to see the difference. (Also see the Python math notes on this site)

Use two different ways to add two to an existing variable.
(one uses “=” and the other uses “+=”)