CommonLounge Archive

Python3 Basics: Numbers and Expressions

July 26, 2018

Python is a programming language. It was created by Guido van Rossum and was first released in 1991. Python has a design philosophy that makes it very easy to read the code. As you will see throughout the course, Python is one of the easiest languages to learn because it almost looks like English, i.e. it’s highly readable.

However, don’t be fooled by Python’s simplicity — major tech companies including Dropbox, Instagram, YouTube etc. use Python to power a number of their apps and websites. As we will discover over the course, Python is a very powerful language with easy-to-use libraries (other people’s code that you can use, almost always for free!).

Excited? Let’s get started!

Your first Python program!

Below is your first Python program. If you run the program (click Try It Now! and then Run), you’ll see that it simply produces the output 5

print(5)

When you say print(something), Python will print the contents and give it to you as output. In this case, we wanted to print the number 5, so we just said print(5) and Python understood that we wanted to print the number 5.

Note: This course is structured so that you can learn quite a bit of Python before having to install things on your computer. However, if you prefer to run the Python programs on your computer, it shouldn’t take more than 5 minutes to set things up. You can see Set-up and installation instructions for Python but don’t worry, we will let you know when it’s the right time to install.

Python and numbers

How about some numbers? Let’s see what Python can do.

print(2 + 3)

Output:

5

Again, click Try It Now! and then Run on the code above to actually run it. You can also play around. Try changing the numbers and make sure you’re getting the correct answers.

Nice! See how the answer popped out? Python knows math! You could try other commands like:

print(5 - 1)
print(4 * 5)
print(40 / 3)

Along the course, we recommend you to run all the programs, and also try changing things to see what happens. Programming is best learnt by doing.

To perform exponential calculation, say 2 to the power 3, we type:

print(2 ** 3)

Output:

8

Have fun with this for a little while and continue here. 🙂

Exercise: Use Python to find out the value of 99 multiplied by 99. As in the last exercise, change the ... part of the code below after hitting the Try it Now button.

print(...)

There are some more operators in Python, but we won’t go over them right now. At the end of this article, there is a table with a list of all arithmetic operators Python supports.

Errors

Let’s try something wild. What if we forget to close the parenthesis properly?

print(3 + 4

We get our first error:

SyntaxError: unexpected EOF while parsing 
Command exited with non-zero status 1

This error message is trying to tell us that it’s a Syntax error. This is the first hint for us to make sure that the syntax is 100% correct. Next, the error message says that Python encountered End of File (EOF) of the program file, but couldn’t find something that it was expecting. Most programs, when successful, exit with status 0. Thus, the last bit is informing you that due to the error, the program exited with a non-zero status.

Making mistakes (even intentional ones) are an important part of learning. In particular, an error doesn’t mean something bad is going to happen with the computer. It’s just Python’s way of saying, “Something’s wrong.” or “I don’t understand.”

In the example above, we made an error and Python told us so — this is an excellent thing. Whenever you get an error, always take a moment to read the full error message. The language creators usually try to be as helpful as they can to make sure you can correct the error quickly.

Expressions

Each of the lines that you typed earlier (inside print()) is called an expression. So, when we do print(expression), Python actually prints the value of the expression evaluates to.

For example, in the very first print example we saw earlier, 5 is an expression. The value is 5. Similarly, 2 + 3 is also an expression. It’s value is 5.

Expressions can be much longer too. For example, try this:

print((5 + 2) * (7 - 1) / 3)

Output:

14.0

In Python (and other programming languages as well), all expressions evaluate to a single value. Here’s a step-by-step breakdown of how the above expression is evaluated in Python:

(5 + 2) * (7 - 1) / 3
=> (7) * (7 - 1) / 3
=> 7 * (7 - 1) / 3
=> 7 * (6) / 3
=> 7 * 6 / 3
=> 42 / 3
=> 14.0

Python does the above behind the scenes, and tells us what the final result of evaluating the expression is.

As you can see, Python is a great calculator.

Exercise: Assume that farmer John wants to build a fence around his farm. His farm is rectangular with 500 metres length and 100 metres breadth. One metre of fence wire costs $1.50. Use Python to calculate the cost of fence wire. As usual, hit Try it now button and replace the ... below to find out the cost.

print(...)

Once you’re done, click the spoiler below to verify your answer.

The correct answer is $1800. Reveal the block below to reveal the code:

print(2 * (500 + 100) * 1.5)

Wow! You just used a complicated Python expression to solve a real world problem. Congrats :)

List of Arithmetic Operations

The following is a list of all arithmetic operations supported by Python.

Operator | Example | Result | Description 
---------|---------|--------|------------------------------------------------
  +      | 4 + 5   | 9      | addition 
  -      | 4 - 5   | -1     | subtraction 
  *      | 4 * 5   | 20     | multiplication
  /      | 8 / 3   | 2.666  | division 
  //     | 14 // 3 | 4      | integer division (fractional part is ignored)
  %      | 14 % 3  | 2      | modulo (remainder left after division)
  **     | 5 ** 2  | 25     | exponent 

Summary

OK, so far you’ve learned about:

  • print – used to make Python “print” the contents you give it
  • numbers – used for math
  • operators – like + and *, combine values to produce a new one
  • errors – you now know how to read and understand errors that show up if Python doesn’t understand a command you’ve given it
  • expressions — evaluate to a single value

Based on content from https://tutorial.djangogirls.org/en/python_introduction/


© 2016-2022. All rights reserved.