Let's write some code!
To start playing with Python, we need to open up a command line on your computer. You should already know how to do that – you learned it in the Introduction to the Command-line Interface chapter.
Once you're ready, follow the instructions below.
We want to open up a Python console, so type in python on Windows or python3 on Mac OS/Linux and hit enter.
$ python3Python 3.6.1 (...)Type "help", "copyright", "credits" or "license" for more information.>>>
After running the Python command, the prompt changed to >>>. For us this means that for now we may only use commands in the Python language. You don't have to type in >>> – Python will do that for you.
If you want to exit the Python console at any point, just type exit() or use the shortcut Ctrl + Z for Windows and Ctrl + D for Mac/Linux. Then you won't see >>> any longer.
For now, we don't want to exit the Python console. We want to learn more about it. Let's start by typing some math, like 2 + 3 and hitting enter.
>>> 2 + 35
Nice! See how the answer popped out? Python knows math! You could try other commands like:
- 4 * 5
- 5 - 1
- 40 / 2
To perform exponential calculation, say 2 to the power 3, we type:
>>> 2 ** 38
Have fun with this for a little while and then get back here. :)
There are some more operators in Python, but we won't go over them right now. At the end of the article, there is a table with a list of all arithmetic operators in Python.
Each of the lines above that you typed into the Python console is called an expression. Expressions can be much longer too. For example, try this:
>>> (5 + 2) * (7 - 1) / 314.0
In Python (and other programming languages as well), all expressions evaluate to a single value. You saw this above for some simple expressions such as 2 + 3 which evaluates to 5.
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. If you're wondering what else you can do…
How about your name? Type your first name in quotes like this:
You've now created your first string! It's a sequence of characters that can be processed by a computer. The string must always begin and end with the same character. This may be single (') or double (") quotes (there is no difference!) The quotes tell Python that what's inside of them is a string.
Strings can be strung together. Try this:
>>> "Hi there " + "Commonlounge"'Hi there Commonlounge'
Notice how we had to end the first string ("Hi there ") with a space (" "). If we did not, then our result would be 'Hi thereCommonlounge', which isn't what we want.
You can also multiply strings with a number:
>>> "Commonlounge" * 3'CommonloungeCommonloungeCommonlounge'
And you can combine these things into a more complicated expression as well:
>>> "Hi there " + ("Commonlounge" * 3) + "!"'Hi there CommonloungeCommonloungeCommonlounge!'>>> "Hi there Commonlounge" + "!" * 10'Hi there Commonlounge!!!!!!!!!!'
If you need to put an apostrophe inside your string, you have two ways to do it.
Using double quotes:
>>> "Runnin' down the hill""Runnin' down the hill"
or escaping the apostrophe with a backslash (\):
>>> 'Runnin\' down the hill'"Runnin' down the hill"
Nice, huh? To see your name in uppercase letters, simply type:
You just used the upper method on your string! A method (like upper()) is a sequence of instructions that Python has to perform on a given object ("Commonlounge") once you call it.
If you want to know the number of letters contained in your name, there is a function for that too!
Wonder why sometimes you call functions with a . at the end of a string (like "Commonlounge".upper()) and sometimes you first call a function and place the string in parentheses? Well, in some cases, functions belong to objects, like upper(), which can only be performed on strings. In this case, we call the function a method. Other times, functions don't belong to anything specific and can be used on different types of objects, just like len(). That's why we're giving "Commonlounge" as a parameter to the len function.
There's more to strings and functions in Python, and we'll be learning about it later in the course in the following tutorials - Python3 Strings and Python3 Functions.
Let's try something new. Can we get the length of a number the same way we could find out the length of our name? Type in len(304023) and hit enter:
>>> len(304023)Traceback (most recent call last):File "", line 1, inTypeError: object of type 'int' has no len()
We got our first error! 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.”
It says that objects of type "int" (integers, whole numbers) have no length. So what can we do now? Maybe we can write our number as a string? Strings have a length, right?
It worked! We used the str function inside of the len function. In particular, the step-by-step evaluation of the above is:
len(str(304023))=> len("304023")=> 6
You can use the str() function to convert anything to a string.
- The str function converts things into strings
- The int function converts things into integers
Important: we can convert numbers into text, but we can't necessarily convert text into numbers – what would int('hello') be anyway?
OK, enough of strings. So far you've learned about:
- the prompt – typing commands (code) into the Python prompt results in answers in Python
- numbers and strings – in Python numbers are used for math and strings for text objects
- operators – like + and *, combine values to produce a new one
- functions – like upper() and len(), perform actions on objects.
- errors – you now know how to read and understand errors that show up if Python doesn't understand a command you've given it
These are the basics of every programming language you learn. Ready for something harder? We bet you are!
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
Based on content from https://tutorial.djangogirls.org/en/python_introduction/