What is Python
Python is a programming language that lets you work more quickly and integrate your systems more effectively. It is powerful, fast, runs everywhere, and easy to learn. These are some of the reasons people who use Python would rather not use anything else.
Hello World Program
We convey our commands to the computer by writing them in a text file using a programming language. These files are called programs. Running a program means telling a computer to read the text file, translate it to the set of operations that it understands, and perform those actions.
This is the Hello World program by Python:
Ironically, the first thing we’re going to do is show how to tell a computer to ignore a part of a program. Text written in a program but not run by the computer is called a comment. Python interprets anything after a # as a comment.
- Provide context for why something is written the way it is:
|# This variable will be used to count the number of times anyone tweets the word persnickety
persnickety_count = 0
- Help other people reading the code understand it faster:
|# This code will calculate the likelihood that it will rain tomorrow
- Ignore a line of code and see how a program will run without it:
|# useful_value = old_sloppy_code()
useful_value = new_clean_code()
Now what we’re going to do is teach our computer to communicate. The gift of speech is valuable: a computer can answer many questions we have about “how” or “why” or “what” it is doing. In Python, the print()function is used to tell a computer to talk. The message to be printed should be surrounded by quotes:
|# from Mary Shelley’s Frankenstein
print(“There is something at work in my soul, which I do not understand.”)
In the above example, we direct our program to print() an excerpt from a notable book. The printed words that appear as a result of the print() function are referred to as output. The output of this example program would be:
|There is something at work in my soul, which I do not understand.|
computer programmers refer to blocks of text as strings. In our last exercise, we created the string “Hello world!”. In Python a string is either surrounded by double quotes (“Hello world”) or single quotes (‘Hello world’). It doesn’t matter which kind you use, just be consistent.
Programming languages offer a method of storing data for reuse. If there is a greeting we want to present, a date we need to reuse, or a user ID we need to remember we can create a variable which can store a value. In Python, we assign variables by using the equals sign (=).
|message_string = “Hello there”
# Prints “Hello there”
In the above example, we store the message “Hello there” in a variable called message_string. Variables can’t have spaces or symbols in their names other than an underscore (_). They can’t begin with numbers but they can have numbers after the first letter (e.g., cool_variable_5 is OK).
It’s no coincidence we call these creatures “variables”. If the context of a program changes, we can update a variable but perform the same logical process on it.
message_string = “Hello there”
message_string = “Hasta la vista”
Above, we create the variable message_string, assign a welcome message, and print the greeting. After we greet the user, we want to wish them goodbye. We then update message_string to a departure message and print that out.
Humans are prone to making mistakes. Humans are also typically in charge of creating computer programs. To compensate, programming languages attempt to understand and explain mistakes made in their programs.
Python refers to these mistakes as errors and will point to the location where an error occurred with a ^ character. When programs throw errors that we didn’t expect to encounter we call those errors bugs. Programmers call the process of updating the program so that it no longer produces unexpected errors debugging.
Two common errors that we encounter while writing Python are SyntaxError and NameError.
- SyntaxError means there is something wrong with the way your program is written — punctuation that does not belong, a command where it is not expected, or a missing parenthesis can all trigger a SyntaxError.
- A NameError occurs when the Python interpreter sees a word it does not recognize. Code that contains something that looks like a variable but was never defined will throw a NameError.
Computers can understand much more than just strings of text. Python has a few numeric data types. It has multiple ways of storing numbers. Which one you use depends on your intended purpose for the number you are saving.
An integer, or int, is a whole number. It has no decimal point and contains all counting numbers (1, 2, 3, …) as well as their negative counterparts and the number 0. If you were counting the number of people in a room, the number of jellybeans in a jar, or the number of keys on a keyboard you would likely use an integer.
A floating-point number, or a float, is a decimal number. It can be used to represent fractional quantities as well as precise measurements. If you were measuring the length of your bedroom wall, calculating the average test score of a seventh-grade class, or storing a baseball player’s batting average for the 1998 season you would likely use a float.
Numbers can be assigned to variables or used literally in a program:
|an_int = 2
a_float = 2.1
print(an_int + 3)
# prints 5
Above we defined an integer and a float as the variables an_int and a_float. We printed out the sum of the variable an_intwith the number 3. We call the number 3 here a literal, meaning it’s actually the number 3 and not a variable with the number 3 assigned to it.
Floating-point numbers can behave in some unexpected ways due to how computers store them. For more information on floating-point numbers and Python, review Python’s documentation on floating-point limitations.
Computers absolutely excel at performing calculations. The “compute” in their name comes from their historical association with providing answers to mathematical questions. Python performs addition, subtraction, multiplication, and division with +, –, *, and /.
|# Prints “500”
print(573 – 74 + 1)
# Prints “50”
print(25 * 2)
# Prints “2.0”
print(10 / 5)
Notice that when we perform division, the result has a decimal place. This is because Python converts all ints to floats before performing division. In older versions of Python (2.7 and earlier) this conversion did not happen, and integer division would always round down to the nearest integer.
Division can throw its own special error: ZeroDivisionError. Python will raise this error when attempting to divide by 0.
Mathematical operations in Python follow the standard mathematical order of operations.
Variables that are assigned numeric values can be treated the same as the numbers themselves. Two variables can be added together, divided by 2, and multiplied by a third variable without Python distinguishing between the variables and literals (like the number 2 in this example). Performing arithmetic on variables does not change the variable — you can only update a variable using the = sign.
|coffee_price = 1.50
number_of_coffees = 4
# Prints “6.0”
print(coffee_price * number_of_coffees)
# Prints “1.5”
# Prints “4”
# Updating the price
coffee_price = 2.00
# Prints “8.0”
print(coffee_price * number_of_coffees)
# Prints “2.0”
# Prints “4”
We create two variables and assign numeric values to them. Then we perform a calculation on them. This doesn’t update the variables! When we update the coffee_price variable and perform the calculations again, they use the updated values for the variable!
Python can also perform exponentiation. In written math, you might see an exponent as a superscript number, but typing superscript numbers isn’t always easy on modern keyboards. Since this operation is so related to multiplication, we use the notation **.
|# 2 to the 10th power, or 1024
print(2 ** 10)
# 8 squared, or 64
print(8 ** 2)
# 9 * 9 * 9, 9 cubed, or 729
print(9 ** 3)
# We can even perform fractional exponents
# 4 to the half power, or 2
print(4 ** 0.5)
Here, we compute some simple exponents. We calculate 2 to the 10th power, 8 to the 2nd power, 9 to the 3rd power, and 4 to the 0.5th power.
Python offers a companion to the division operator called the modulo operator. The modulo operator is indicated by % and gives the remainder of a division calculation. If the number is divisible, then the result of the modulo operator will be 0.
|# Prints 4 because 29 / 5 is 5 with a remainder of 4
print(29 % 5)
# Prints 2 because 32 / 3 is 10 with a remainder of 2
print(32 % 3)
# Modulo by 2 returns 0 for even numbers and 1 for odd numbers
# Prints 0
print(44 % 2)
Here, we use the modulo operator to find the remainder of division operations. We see that 29 % 5 equals 4, 32 % 3 equals 2, and 44 % 2 equals 0.
The modulo operator is useful in programming when we want to perform an action every nth-time the code is run. Can the result of a modulo operation be larger than the divisor? Why or why not?
The + operator doesn’t just add two numbers, it can also “add” two strings! The process of combining two strings is called string concatenation. Performing string concatenation creates a brand new string comprised of the first string’s contents followed by the second string’s contents (without any added space in-between).
|greeting_text = “Hey there!”
question_text = “How are you doing?”
full_text = greeting_text + question_text
# Prints “Hey there!How are you doing?”
In this sample of code, we create two variables that hold strings and then concatenate them. But we notice that the result was missing a space between the two, let’s add the space in-between using the same concatenation operator!
|full_text = greeting_text + ” “ + question_text
# Prints “Hey there! How are you doing?”
Now the code prints the message we expected.
If you want to concatenate a string with a number you will need to make the number a string first, using the str() function. If you’re trying to print() a numeric variable you can use commas to pass it as a different argument rather than converting it to a string.
|birthday_string = “I am “
age = 10
birthday_string_2 = ” years old today!”
# Concatenating an integer with strings is possible if we turn the integer into a string first
full_birthday_string = birthday_string + str(age) + birthday_string_2
# Prints “I am 10 years old today!”
# If we just want to print an integer
# we can pass a variable as an argument to
# print() regardless of whether
# it is a string.
# This also prints “I am 10 years old today!”
print(birthday_string, age, birthday_string_2)
Using str() we can convert variables that are not strings to strings and then concatenate them. But we don’t need to convert a number to a string for it to be an argument to a print statement.
Python offers a shorthand for updating variables. When you have a number saved in a variable and want to add to the current value of the variable, you can use the +=(plus-equals) operator.
|# First we have a variable with a number saved
number_of_miles_hiked = 12
# Then we need to update that variable
# Let’s say we hike another two miles today
number_of_miles_hiked += 2
# The new value is the old value
# Plus the number after the plus-equals
# Prints 14
Above, we keep a running count of the number of miles a person has gone hiking over time. Instead of recalculating from the start, we keep a grand total and update it when we’ve gone hiking further.
The plus-equals operator also can be used for string concatenation, like so:
|hike_caption = “What an amazing time to walk through nature!”
# Almost forgot the hashtags!
hike_caption += ” #nofilter”
hike_caption += ” #blessed”
We create the social media caption for the photograph of nature we took on our hike, but then update the caption to include important social media tags we almost forgot.
Python strings are very flexible, but if we try to create a string that occupies multiple lines we find ourselves face-to-face with a SyntaxError. Python offers a solution: multi-line strings. By using three quote-marks (“””or ”’) instead of one, we tell the program that the string doesn’t end until the next triple-quote. This method is useful if the string being defined contains a lot of quotation marks and we want to be sure we don’t close it prematurely.
In the above example, we assign a famous poet’s words to a variable. Even though the quote contains multiple line breaks, the code works!
If a multi-line string isn’t assigned a variable or used in an expression it is treated as a comment.
In this lesson, we accomplished a lot of things! We instructed our computers to print messages, we stored these messages as variables, and we learned to update those messages depending on the part of the program we were in. We performed mathematical calculations and explored some of the mathematical expressions that Python offers us. We learned about errors and other valuable skills that will continue to serve us as we develop our programming skills.