# command Python to print the string "Hello World"
print("Hello World")
Hello World
Python is a general-purpose, versatile and popular programming language. It’s great as a first language because it is concise and easy to read, and it is very valuable because it can be used for everything from web development to software development and scientific applications. In fact, python is the world’s fastest growing and most popular programming language used by software engineers, analysts, data scientists, and machine learning engineers alike!
Below we will write our first line of code! We will make Python “come alive” by sending a greeting to the world on the screen. To do this, we will use a command called print
. This command tells Python to print the input text to the screen. Let’s begin!
Amazing! You just wrote your first line of Python code. Pretty easy right?
Python can also be used as a calculator. Let’s use Python to do some simple addition and subtraction below.
We can also do multiplication and division with Python. The symbol for multiplication is *
and the symbol for division is /
. Let’s use Python to do some multiplication and division below!
Easy right?
Now let’s say we wanted to add the result of 6 x 13 to the result of 98 / 4. Python follows the standard order of operations: PEMDAS, so we could do it this way:
But what if we wanted to save ourselves some time? We already typed those equations earlier, so instead of typing the same equations over and over, we can store the results in variables.
Variables are words or symbols that are used to represent some value. They’re kind of like boxes that you can store things in. Variables in Python are used very similarly to variables that you have probably seen in math class. For example, if you assign x = 5 and compute 9 + x = _ ?
Exactly - 14! Let’s see some examples of how this works in code.
You might notice that after running the above cell, nothing was printed to the screen. This is because our code commanded Python only to assign the values to the variable names, not to print to the screen. This is a good reminder that Python is very literal, and will only do exactly what we command it to do.
If we want to see the values assigned to each variable, we can use the print
command we learned above to print them out.
# print the value stored in the variable result1
print(result1)
# print the value stored in the variable result2
print(result2)
78
24.5
Cool! Now we’ve seen that the print
function in Python can accept different inputs, such as a string (as in "Hello World"
), or a variable name (as in result1
). We see that when we use the variable name result1
, it prints the value of that variable, not the variable name itself. We will talk more about how we differentiate strings from variable names in the next lesson.
Now, let’s revisit our initial objective. We wanted to add the result of 6 x 13 to the result of 98 / 4, but this time let’s use our variables that contain each of our values.
# command Python to add the result of 6 x 13 to the result of 98 / 4 using our variable names
result1 + result2
102.5
Fantastic! You’ve just solved your first set of problems using Python code! You may not realize it, but in just a few minutes, you’ve already learned how to: * Use built-in Python functions (print
) * Use mathematical operators to perform calculations (+ - * /
) * Assign values to variables * Use variables in mathematical equations
Now let’s continue to practice these skills with your partners!