Variables and Data Types¶
Variable Assignment and naming restrictions¶
A variable is a named symbol that holds a value.
Variable containers --> name and value
Create variables by assigning a value to a name (just like using variables in math).
Variable names should be meaningful, i.e. not just
a
,b
,c
Variables are always assigned with the variable name on the left and the value on the right of the *equals* sign. For instance:
a_variable = 100
- assigned to other variables:
another_variable = a_variable
- reassigned at any time:
a_variable = 435
- assigning several variables at the same time:
all, at, once = 1, 130, 43
Variables must be assigned before they can be used.
# Create a variable x by assigning a value to x
x = 100
print(x)
# What is the container and what are the data here?
100
In Python, you can name your variables whatever you want, with some restrictions:
Variables must start with a letter or underscore:
_yes
: Valid name.2no
: Not valid!
The rest of the name must consist of letters and numbers (i.e. alpanumeric). If you need to use a multi_word variable name, underscores can be used:
yes2
: Valid name.hey@no
: Not valid!hey_no
: Valid name.
Names are case-sensitive
Yes
andyes
are two different variables.
Each variable's name must be unique. No other variable can have its name.
# Try it out
_2x = 100
print(_2x)
# What can be the alternatives to make this variable naming work?
100
Data Types¶
In any assignment, the assigned value must always be a valid data type.
Python data types include (among others):
- numbers:
int
: an integer, e.g.1
,2
,3
float
: a floating point number with a decimal point, e.g.1.2
,2999.197
,-160.8
str
: (string) a sequence of Unicode characters, e.g. "Kate" or "程序设计"bool
: True or False
You can check a variable's type with the type()
function.
# Try it out
var = -1
print(var)
print(type(var))
-1 <class 'int'>
var2 = -2.099874565
print(var2)
print(type(var2))
-2.099874565 <class 'float'>
my_string = "Hello world!"
print(my_string)
print(type(my_string))
Hello world! <class 'str'>
bool_var = True
print(bool_var)
print(type(bool_var))
True <class 'bool'>
Numbers and Operators¶
Strings¶
Strings are containers of characters.
There are different encodings for characters. The default in Python 3 is the Unicode encoding which includes characters from European and Asian languages.
Declaring strings¶
String literals in Python can be declared with either single or double quotes.
my_other_str = 'a hat'
my_str = "a cat"
Either one is perfectly fine; but make sure you stick to the same convention throughout the same file.
Reading error messages¶
Let's talk about error messages! They look red and scary, but they're actually just here to help you. Error messages tell you that you are trying to do something that is either not allowed, not possible, ambiguous, not meaningful or written using the wrong syntax.
You will encounter error messages ALL THE TIME; especially when learning to program, but also as an advanced programmer. That's why it's worth knowing how the read them.
Look at the error message below. You can find the most important piece of information on the last line: this is the actual error and this is where you should always look first. In the part above the last line, you can see in which line of your program the error occurs in.
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-10-bc757c3fda29> in <module>
----> 1 1 / 0 # the arrow points towards the line where the error occurs
ZeroDivisionError: division by zero # this is the actual type of error
# let's see what types of error messages there are
i
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-9-397d543883c5> in <module>() ----> 1 i NameError: name 'i' is not defined
1 / 0
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) <ipython-input-10-bc757c3fda29> in <module>() ----> 1 1 / 0 ZeroDivisionError: division by zero
1 %% 2
File "<ipython-input-11-0165dc641888>", line 1 1 %% 2 ^ SyntaxError: invalid syntax
my_string = "Hello world!"
my_string + 1
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-12-9aa644ec6cb4> in <module>() 1 my_string = "Hello world!" ----> 2 my_string + 1 TypeError: can only concatenate str (not "int") to str
Exercise¶
~ 15 minutes
a. Is the following statement correct? Variables must be assigned before they can be used.
b. Is 24hrs
a valid variable name?
# Your code goes here
c. Is my_1st_variable
a valid variable name?
# Your code goes here
d. Please complete the four steps below.
- Use a number of your choice and store it in a variable. Multiply that variable by
2
and print this new variable. - Use a second number of your choice and multiply it with the initial variable used in (1).
- Find out if the result is even (divisible by 2) or odd. Hint: Use the modulo operator.
- What is the type of the final variable?
# Your code goes here