CommonLounge Archive

Python Basics: Numbers, Strings and Functions

August 04, 2018

Let’s write some code!

Python prompt

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 your Terminal and hit enter.

$ python
Python 2.7.1 (...)
Type "help", "copyright", "credits" or "license" for more information.
>>>

Your first Python command!

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.

Integers (int)

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 + 3
5

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

  • 4 * 5
  • 5 - 1

Have fun with this for a little while and then get back here. :)

Floating-point values (float)

Division is a bit more interesting. By default, when you divide two integers, Python does integer division, i.e. it ignores the fractional part of the result. For example:

>>> 5 / 2 
2 

As you can see, the fractional part is missing (the result is 2 instead of 2.5). To get the correct result, you can do either of the following:

>>> 5.0 / 2
2.5
>>> 5.0 / 2.0
2.5

In Python, 5 and 2 are ints (integers), whereas 5.0 and 2.0 are floats (floating-point values, or real numbers). When you divide an integer by an integer, Python the result is an integer. But if either of the numbers (or both) is a float, then Python converts the other one to float before doing the math.

This is also true for other operations. For example,

>>> 2.0 + 3.0
5.0
>>> 2 + 3.0
5.0
>>> 2.1 + 3.2
5.3

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

>>> 2 ** 3
8

Again, feel free to play around for a bit and then get back here.

As you can see, Python is a great calculator. If you’re wondering what else you can do…

Strings

How about your name? Type your first name in quotes like this:

>>> "Commonlounge"
'Commonlounge'

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'

You can also multiply strings with a number:

>>> "Commonlounge" * 3
'CommonloungeCommonloungeCommonlounge'

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:

>>> "Commonlounge".upper()
'COMMONLOUNGE'

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!

>>> len("Commonlounge")
12

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.

Errors

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, in 
TypeError: object of type 'int' has no len()

We got our first error! Making mistakes (even intentional ones) are an important part of learning!

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?

>>> len(str(304023))
6

It worked! We used the str function inside of the len function. str() converts everything to strings.

  • 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?

Summary

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!

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


© 2016-2022. All rights reserved.