Data types, variables, and output
Let’s now consider the below Python components, which you will then explore in more detail below.
Objectives
- Variables and basic data types.
- Changing data type (not always possible).
- Some built-in functions e.g., we use - type() to tell us what data type a variable is, and print() to print the output on the screen.
- Operators like + and - to do calculations with variables.
Variables
As programmers, we work with data. Data comes in different types, like text and numbers. The data a program works on will be in the computer’s memory, and we use variables to give data a name so that we can work with it.
If our data is the number 10, I can make a variable to refer to it like this:
myvar = 10
Figure 1. After running the code myvar = 10, the variable name myvar refers to a place in the computer’s memory where the value 10 is stored.
That sets up a small part of memory with 10 stored there, and we can use the name we gave it in calculations, like myvar + 6 or myvar / 2
Because myvar holds a whole number, it has the whole number data type. In Python, this is known as int, short for integer.
We can also check this using Python’s built-in type() function.
Typing type(myvar) at the Python interactive prompt, after setting it up as above, will print <class int>.
So, variables are references to data values.
- Data used by a program is stored somewhere in the computer’s memory
- In programs we use variables to refer to our data
- In Python a variable is created when you assign it a value, for example myvar = 10
- The variable name is the programmer’s choice
Apart from whole numbers, we often work with text and with numbers like 3.5 which have fractional parts.
Data type float
A number like 3.5 has the Python data type float.
Setting up a variable with the name ‘price’ and the value 3.5:
price = 3.5
Figure 2. After running the code price = 3.5, the variable name price refers to a place in the computer’s memory where the value 3.5 is stored.
type(price) will print <class float>
Data type str
Text data in Python has the data type str (short for string); it is like a string of characters.
You set up a string variable using quotes round the text. Either double or single quotes will do. To make a variable called lastname containing the text “Jones” you use:
lastname = 'Jones'
Figure 3. After running the code lastname = “Jones”, the variable name lastname refers to a place in the computer’s memory where the value ‘Jones’ is stored.
Example:
You can try out any code examples in IDLE or any Python IDE.
x = 10
y = 77.77
z = "Monday"
Here, we up three variables, x is 10, y is 77.77 and z is the word Monday.
Printing to the screen in Python
We can output to the screen using the built-in function print().
Examples:
"""
Setting up variables
and printing them
"""
Copy code
1
my_number = 3
Copy code
1
my_string = "Jane Austen"
Copy code
1
second_number = 111.983
Copy code
1
print(my_number, my_string, second_number)
Copy code
1
print(my_number + second_number)
Data types
Variables can store different kinds of data. In the last section, we saw variables containing text and numbers. There are many more data types and we will use some in later sections of the module. Here are some of the built-in data types in Python:
Numbers
Data type: int, float | Example: 100, 3.6
Text
Data type: str | Example: "Sarah", ’Nonlinear Models’
Boolean
Data type: bool | Example: True, False
Sequences
Data type: list, tuple, range, (also str) | Example: [1,2,3], range(10)
Set types
Data type: set | Example: {1,2,3}
Mappings
Data type: dict | Example: {’Paris’:’France’, ’London’:’England’}
You can check the data type of a Python variable using the built-in function type().
Numbers
The data type for whole numbers like 3 or -90 is int (meaning integer)
The data type for numbers with decimal points like 0.00001 or 3.7 is float
Variables: conclusion
Python variables are references for data values and these variables store data of different types. By using the print() function, we can print output to the screen.
Next up, we’ll take this subject further and look at using data types and variables in Python.
Examples:
"""
Using the built in function type to see what data type we have
"""
Copy code
1
myvar = "some text"
Copy code
1
number1 = 25
Copy code
1
number2 = 11.4
Copy code
1
print(myvar, type(myvar))
Copy code
1
print(number1, type(number1))
Copy code
1
print(number2, type(number2))
"""
This will print
"some text" <class 'str'>
25 <class int>
11.4 <class float>
because the Python data type for text is called str,
the Python data type for whole numbers is called int,
and the Python class for numbers with decimal points is called float.
"""
Strings
The Python data type for text is str (meaning string).
To make a string variable, surround your text with quotes, eg. x = "Joe".
Double quotes " and single quotes ’ are the same in Python.
For multiline text, use triple quotes """ around the text.
Examples:
"""
Using quotes to make strings of text
We make three variables, called
my_name, my_friend, and some_friends
(we can choose any name for a variable
with some restrictions that we will cover later)
All three will be of type str, we could check this using the type() built in function
Single quotes are the same as double quotes in Python.
Triple quotes allow multiline text.
"""
Copy code
1
my_name = "Sarah"
Copy code
1
my_friend = 'Sophie'
Copy code
1
some_friends = """
Copy code
1
Anne,
Copy code
1
Joe,
Copy code
1
Marcus
Data type bool
Variables of this type can only be True or False. We will see more on working with booleans and checking conditions in the next sections, where we cover making choices in Python and checking whether conditions are true or false.
Casting - changing data type
When you create a variable by assigning a value, like x = 3, Python decides the data type automatically, in this case, the data type is int.
But what if you want a different type?
You can often change the data type of a variable. If the change is not possible, you will get a Python error.
Make a variable called x containing 9
x = 9
Change it to a float – you can always change it into to a float.
x = float(x)
Now type(x) will be float, and printing x will give 9.0.
Or change it to a string. You can always change an int or a float to a string.
x = str(x)
Now type(x) will say str and printing x will give ‘9.0’.
You can also change a string to a number – but not always.
Examples:
"""
Type conversions
# Data types can be changed, but only when it makes sense to do so
"""
Copy code
1
x = 3
Copy code
1
print(x, type(x))
Copy code
1
x = float(x)
Copy code
1
print(x, type(x))
Copy code
1
x = str(x)
Copy code
1
print(x, type(x))
Copy code
1
y = '3'
Copy code
1
print(y, type(y))
Copy code
1
y = int(y)
Copy code
1
print(y, type(y))
Copy code
1
z = "Fred"
Copy code
1
int(z)
Copy code
1
# The last one will give an error! Python doesn't know how to make a number out of the string "Fred"
Operators – doing calculations in Python
- We can make variables that refer to numbers
- We have the usual mathematical operators to do calculations: + and - for addition and subtraction, / is divide, and * is multiply
- We have the usual rules of precedence (BODMAS - first brackets, then exponentiation, then multiplication and division, then addition and subtraction)
- We can use brackets to affect the order of operations
Operator: +-
Meaning: addition and subtraction | Example: 1 + 7 = 8
Operator: * /
Meaning: multiplication and division | Example: 1 * 7, x / y
Operator: %
Meaning: modulo/remainder | Example: 7 % 2
Operator: **
Meaning: exponentiation | Example: x ** y
Operator: //
Meaning: integer (floor) division | Example: x // y
Examples of mathematical operators:
"""
Some calculations in Python
"""
Copy code
1
x = 7
Copy code
1
y = 100
Copy code
1
z = 12.3
Copy code
1
r2 = x*y - z
Copy code
1
print(r2)
Copy code
1
num1 = 1
Copy code
1
num2 = 3
Copy code
1
ave = (num1 + num2) / 2
Copy code
1
print(ave)
Summary - variables and data types
- Variables are references to data values stored in computer memory
- Numbers have data type int or float, text has datatype str (string)
- You can use the built-in function type() to find out what data type a variable is
- Mathematical operators like + - * / can be used for calculations
- The Python interactive shell allows you to run single Python commands
- The function print writes to the screen
Next, you will watch Sarah, a software developer, demonstrating how to set up variables and looking at the four basic data types in Python. This will then be followed by another demonstration of writing a very simple program in Python.
In this section, you will explore data types, variables, input and output functions, and discover more about calculations and mathematical operators in Python.
A world-leading tech and digital skills organization, we help many of the world’s leading companies to build their tech and digital capabilities via our range of world-class training courses, reskilling bootcamps, work-based learning programs, and apprenticeships. We also create bespoke solutions, blending elements to meet specific client needs.