In our first week of class, we started a refresher on the very basics of Python. We covered how to print things from our program, how to do some basic math in Python, what strings are, and how to use strings. This post will go over everything we talked about in class and have some problems for you to solve too!
Read over the code given as examples, then, answer the questions marked with “???” in your own Python program.
Printing in Your Program
The first big thing to remember in Python is how to print something out to the shell. To do this, use the
print() function. You can print out almost anything with this function. For example, the following line will write “Hello World!” to the shell.
Sometimes, you’ll want to print the value of variables and some words at the same time. You can print out multiple things by separating them with a comma, as in the example below.
x = 10 print("The value of x is", x)
Once you understand the above code, solve the following problem.
# Given the below variable, v, print out "v is" and what is stored in v v = 123 # ???
One of the biggest components of computer science is math, especially with Python. Python is a language designed to make doing math a lot easier, so we will be using math a lot. Let’s first look at the syntax for how to write some basic math operations.
x = 1 + 3 # add y = 3 - 4 # subtract z = 6 * 3 # multiply a = 4 / 2 # divide b = 4 ** 10 # take things to power c = 7 % 2 # remainder of 7 / 3
After understanding the examples above, write code to answer the following question.
# Set the variable 'd' equal to the remainder of 21 squared, divided by 4 d = # ???
All of the above examples have only used ints, or whole numbers. Next, let’s take a look at how Python represents floats. Floats are how Python stores decimals. Some examples of storing floats in a variable are below.
x = 10.5 y = 2 / 3 # Stores 0.666666... in y
Sometimes, when you’re doing calculations, you’ll have to make a float into an int by rounding. In Python, the round() function rounds down if the decimal is less than or equal to 0.5, and rounds up if it is greater than 0.5.
round_up = round(0.51) # Stores 1 in z round_down = round(2.5) # Stores 2 in a
Once you understand how floats work, answer the following question.
# What's 176 divided by 3 to the nearest integer? b = # ???
The next data type we are going to be looking at is strings, which is how Python stores letters. You can also put numbers into strings, but once you make a number a string, you can’t do any more math with the string.
The following is an example of a string. Note that there are quotes around the letters I want to be the string. Quotes are how Python can tell what is a string and what isn’t.
string_ex = "Hello"
Once you have your string, you’ll often want to know how long the string is. You can figure this out with the
len() function. The
len() function can be used to find the length of almost any data type, but for now we’ll just show examples using it with strings.
len_string = len(string_ex) # Evaluates to 5
Now that you have a string, you’ll want to be able to change it by adding another string to it to make a longer string, or concatenate the two strings together. You can do this by simply using a + between the two strings you want to put together. This works with both pure strings (in quotes) and variables (no quotes).
concatenate = string_ex + " there"
If you want to say the same thing many times in one string, you can use * to repeat the same thing over and over in one string.
mult_string = "Hi!\n" * 50 # This writes Hi! 50 times
In the above code, we use a special character
\n. This character is used to write a new line. It is the same as hitting enter on your keyboard. To use it, just type
\n inside a string.
Once you understand what the above examples are doing, solve the following question.
# print "General Kenobi" 12 times with the following variables x = "General" y = "Kenobi" print() # ???
# Store the length of the string you just made into the variable length length = # ???
This covers the very basics of Python! You now know how to print things out, how to do some basic math, and how to work with strings. Next week, we will continue with our review of Python and dive into some more complex topics.