Data Types and Variables

Just printing Hello World! is not enough, is it? You want to do more than that - you want to take some input, manipulate it and get something out of it. We can achieve this in Python using constants and variables, and we'll learn some other concepts as well in this chapter.

Values and Types

A value is one of the basic things a program works with, like a letter or a number. Some values we have seen so far are 2, 42.0, and 'Hello, World!'.

These values belong to different types: 2 is an integer, 42.0 is a floating-point number, and 'Hello, World!' is a string, so-called because the letters it contains are strung together.

If you are not sure what type a value has, the interpreter can tell you:

Code Output
type(2) class 'int'
type(42.0) class 'float'
type('Hello, World!') class 'str'

In these results, the word "class" is used in the sense of a category; a type is a category of values.

Not surprisingly, integers belong to the type int, strings belong to str and floating-point numbers belong to float.

Each of these values –2, 42.0, and "Hello World!" are examples of literal constants.  They are called a literal because it is literal - you use its value literally. The number 2 always represents itself and nothing else - it is a constant because its value cannot be changed. Hence, all these are referred to as literal constants.

Numbers

Numbers are mainly of two types - integers and floating-point numbers.

An example of an integer is 2 which is just a whole number.  Other integers are -5, 0, and 124789.

Floating-point numbers (or floats for short) are numbers with a decimal.  Some examples include 3.23 and 52.3E-4. The E notation indicates powers of 10. In this case, 52.3E-4 means 52.3 * 10^-4^ or 0.000523.

Note for Experienced Programmers:  There is no separate long type. The int type can be an integer of any size.

When you type a large integer, you might be tempted to use commas between groups of digits, as in 1,000,000. This is not a legal integer in Python, but it is legal syntax:

Code Output
1,000,000 (1, 0, 0)

That's not what we expected at all! Python interprets 1,000,000 as a comma-separated sequence of integers. We'll learn more about this kind of sequence later.

Strings

A string is a sequence of characters. Strings are basically just a bunch of words.  In our Hello World! program, "Hello World!" was a string.  We used the print statement to display the string.

You will be using strings in almost every Python program that you write, so pay attention to the following part.

Single Quote

You can specify strings using single quotes such as 'Quote me on this'.

All white space i.e. spaces and tabs, within the quotes, are preserved as-is.

Code Output
print('Quote me on this') Quote me on this

Launch Exercise

Double Quotes

Strings in double quotes work exactly the same way as strings in single quotes. An example is "What's your name?".

Code Output
print("What's your name?") What's your name?

Launch Exercise

Triple Quotes

You can specify multi-line strings using triple quotes - (""" or '''). You can use single quotes and double quotes freely within the triple quotes. An example is:

'''This is a multi-line string. This is the second line. "What's your name?," I asked. He said "Bond, James Bond." '''
Code Output
print('''This is a multi-line string.
This is the second line.
"What's your name?," I asked.
He said "Bond, James Bond."
''')
This is a multi-line string.
This is the second line.
"What's your name?," I asked.
He said "Bond, James Bond."

Launch Exercise

Strings Are Immutable

This means that once you have created a string, you cannot change it. Although this might seem like a bad thing, it really isn't. We will see why this is not a limitation in the various programs that we see later on.

Note for C/C++ Programmers: There is no separate char data type in Python. There is no real need for it and I am sure you won't miss it.

Note for Perl/PHP Programmers: Remember that single-quoted strings and double-quoted strings are the same - they do not differ in any way.

What about values like '2' and '42.0'? They look like numbers, but they are in quotation marks like strings.

Code Output
type('2') class 'str'
type('42.0') class 'str'

They're strings.

Escape Sequences

Suppose, you want to have a string which contains a single quote ('), how will you specify this string? For example, the string is "What's your name?". You cannot specify 'What's your name?' because Python will be confused as to where the string starts and ends. So, you will have to specify that this single quote does not indicate the end of the string.

This can be done with the help of what is called an escape sequence. You specify the single quote as \' : notice the backslash. Now, you can specify the string as 'What\'s your name?'.

Code Output
print('What\'s your name?') What's your name?

Another way of specifying this specific string would be "What's your name?" i.e. using double quotes. Similarly, you have to use an escape sequence for using a double quote itself in a double quoted string. Also, you have to indicate the backslash itself using the escape sequence \\.

Code Output
print("He said, \"Hi!\"") He said, "Hi!"
print("This is a backslash: \\") This is a backslash: \

Launch Exercise

What if you wanted to specify a two-line string? One way is to use a triple-quoted string as shown previously or you can use an escape sequence for the newline character - \n to indicate the start of a new line. An example is:

'Line 1\nLine 2'
Code Output
print('Line 1\nLine 2') Line 1
Line 2

Another useful escape sequence to know is the tab: \t. This will insert a tab into your string which can be useful for formatting:

Code Output
print('First Column\tSecond Column') First Column    Second Column

There are many more escape sequences but I have mentioned only the most useful ones here.

One thing to note is that in a string, a single backslash at the end of the line indicates that the string is continued in the next line, but no newline is added. For example:

"Sentence 1. \ Sentence 2."
Code Output
print("Sentence 1. \
Sentence 2.")
Sentence 1. Sentence 2.

Launch Exercise

Raw String

If you need to specify some strings where no special processing such as escape sequences are handled, then what you need is to specify a raw string by prefixing r or R to the string. An example is:

Code Output
print(r"Newlines are indicated by \n") Newlines are indicated by \n

See how the \n is not rendered as a new line, but is displayed as-is.

Note for Regular Expression Users: Always use raw strings when dealing with regular expressions. Otherwise, a lot of backwhacking may be required. For example, backreferences can be referred to as '\\1' or r'\1'.

Launch Exercise

End Behavior

The print statement automatically ends each statement with a line break.

Code Output
print("Line 1")
print("Line 2")
Line 1
Line 2
print("Line 1\nLine 2")
print("Line 3")
Line 1
Line 2
Line 3

Later we'll learn how to control the end behavior of the print statement. For now, recognize that there will always be a line break at the end of each print statement.

Variables

Using just literal constants can soon become boring and impractical - we need some way of storing any information and manipulate them as well. This is where variables come into the picture. Variables are exactly what the name implies - their value can vary, i.e., you can store anything using a variable. Variables are just parts of your computer's memory where you store some information. Unlike literal constants, you need some method of accessing these variables and hence you give them names.

Identifier Naming

Variables are examples of identifiers. Identifiers are names given to identify something. There are some rules you have to follow for naming identifiers:

  • The first character of the identifier must be a letter of the alphabet (uppercase ASCII or lowercase ASCII or Unicode character) or an underscore (_).
  • The rest of the identifier name can consist of letters (uppercase ASCII or lowercase ASCII or Unicode character), underscores (_) or digits (0-9).
  • Identifier names are case-sensitive. For example, myname and myName are not the same. Note the lowercase n in the former and the uppercase N in the latter.

Examples of valid identifier names are:

  • i
  • name_2_3
  • _part_1

Examples of invalid identifier names are:

  • 2things
  • this is spaced out
  • my-name
  • >a1b2_c3

If you give a variable an illegal name, you get a syntax error:

Code Output
76trombones = 'big parade' SyntaxError: invalid syntax
more@ = 1000000 SyntaxError: invalid syntax
class = 'Advanced Theoretical Zymurgy' SyntaxError: invalid syntax

76trombones is illegal because it begins with a number. more@ is illegal because it contains an illegal character, @. But what's wrong with class?

It turns out that class is one of Python's keywords. The interpreter uses keywords to recognize the structure of the program, and they cannot be used as variable names.

Python 3 has these keywords:

  • and
  • as
  • assert
  • break
  • class
  • continue
  • def
  • del
  • elif
  • else
  • except
  • False
  • finally
  • for
  • from
  • global
  • if
  • import
  • in
  • is
  • lambda
  • None
  • nonlocal
  • not
  • or
  • pass
  • raise
  • return
  • True
  • try
  • while
  • with
  • yield

You don't have to memorize this list. In most development environments, keywords are displayed in a different color; if you try to use one as a variable name, you'll know.

Data Types

Variables can hold values of different data types. The basic types are integers, floats and strings, which we have already discussed. In later lessons, we will see how variables can hold other data types.

Assignment Statements

An assignment statement creates a new variable and gives it a value:

message = 'And now for something completely different' n = 17 pi = 3.141592653589793

This example makes three assignments. The first assigns a string to a new variable named message; the second gives the integer 17 to n; the third assigns the (approximate) value of π to pi.

If we were to display the variables using the print statement, we would see their values:

Code Output
message = 'And now for something completely different'
print(message)
And now for something completely different
n = 17
print(n)
17
pi = 3.141592653589793
print(pi)
3.141592653589793

Launch Exercise

You can make more than one assignment to the same variable.  In fact, in your programs, this will happen frequently.  A new assignment will make the existing variable point to a new value – and stop referring to the old value.  To make a new assignment, you simply write a new assignment statement.

Code Output
x = 5
print(x)
x = 6
print(x)

5

6

Variables can also switch data types:

Code Output
my_variable = 5
print(my_variable)
my_variable = "Good morning!"
print(my_variable)

5

Good morning!

Launch Exercise

Output with Variables and Literal Constants

There are times when we need to display one or more variables and literal constants together. The print statement has already got you covered. You simply separate the variables and literal constants using commas:

Code Output
my_name = "Heather"
print("Hi,", my_name)
Hi, Heather

Notice that I did not explicitly put a space after the comma – Python added the space itself. Later, we will learn how to control the character which separates components of the print statement.

Here are some more examples:

Code Output
fav_color = 'blue'
print("I like", fav_color, "too!")
I like blue too!
animal = "dogs"
number = 5
print('I see', number, animal, "running")
I see 5 dogs running

Launch Exercise

Launch Exercise

Launch Exercise

Example: Using Variables and Literal Constants

i = 5 print(i) i = i + 1 print(i) s = '''This is a multi-line string. This is the second line.''' print(s)

Output:

5 6 This is a multi-line string. This is the second line.

How It Works

Code Output Notes
i = 5 none

First, we assign the literal constant value 5 to the variable i using the assignment operator (=). This line is called a statement because it states that something should be done and in this case, we connect the variable name i to the value 5.

print(i) 5

Next, we print the value of i using the print statement which, unsurprisingly, just prints the value of the variable to the screen.

i = "hello" none

The we reassign i to the string hello.

print(i) hello

We then print it and expectedly, we get the value hello..

s = '''This is a multi-line string.
This is the second line.'''
none

Similarly, we assign the literal string to the variable s.

print(s) This is a multi-line string.
This is the second line.

Then we print it.