This page was generated from notebooks/L1/1_variables.ipynb.
Binder badge
You can directly download the pdf-version of this page using the link below.
download

Variables and types#

Symbol names#

Variable names in Python can contain alphanumerical characters a-z, A-Z, 0-9 and some special characters such as _. Normal variable names must start with a letter.

By convention, variable names start with a lower-case letter, and Class names start with a capital letter.

Note: Reserved keywords

There are a number of Python keywords that cannot be used as variable names. These keywords are:

and, as, assert, break, class, continue, def, del, elif, else, except,
exec, finally, for, from, global, if, import, in, is, lambda, not, or,
pass, print, raise, return, try, while, with, yield

Note: Be aware of the keyword lambda, which could easily be a natural variable name in a scientific program. But being a keyword, it cannot be used as a variable name.

Variable Assignment#

The assignment operator in Python is =. Python is a dynamically typed language, so we do not need to specify the type of a variable when we create one.

Assigning a value to a new variable creates the variable:

[37]:
  # variable assignments
x = 1.0
my_favorite_variable = 12.2
x
[37]:
1.0

Although not explicitly specified, a variable does have a type associated with it. The type is derived from the value that was assigned to it.

[36]:
type(x)
[36]:
int

If we assign a new value to a variable, its type can change.

[38]:
x = 1
[39]:
type(x)
[39]:
int

If we try to use a variable that has not yet been defined we get an NameError:

[44]:
print(g)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[44], line 1
----> 1 print(g)

NameError: name 'g' is not defined

Number types#

Python allows as any programming language different types of variables. The type of a variable can be always accessed with the help of the type() command. The variable can store different data types. A list of different data types can be found in the notebook on data types. Here we treat numbers or the numeric data type a bit special.

Numbers can be represented in different number systems by adding a special character in the number

Number System

Prefix

Binary

0b or 0B

Octal

0o or 0O

Hexadecimal

00x or 0X

Integers#

Python treats numbers without a decimal point automatically as an integer. In Python 2 there has been a maximum integer possible, which does not exist in Python 3 anymore.

[45]:
# integer number
x = 1
type(x)
[45]:
int

a binary number

[107]:
0b1010111110
[107]:
702

a hexadecimal number

[102]:
0x0F
[102]:
15

Floating Point#

Floating point values are values with a decimal point. There is a maximum float in Python 3, which is 1.7976931348623157e+308. If you multiply this maximum by 2 for example, Python will threat the number as infinity.

[54]:
# float variable
x= 3.141
[55]:
type(x)
[55]:
float
[57]:
1.7976931348623157e+308*2
[57]:
inf

Complex Numbers#

[69]:
j=5
c=2+4j
[70]:
type(c)
[70]:
complex
[75]:
c.imag
[75]:
4.0

Complex numbers have built in accessors. These accessors give for example access to the real and imaginary part of the complex number.

[76]:
r=c.real
print(r)
2.0
[77]:
i=c.imag
print(i)
4.0

On the other side, one my also evaluate the complex conjugate of a complex number by one of those accessors. Note that this is provided by a function here, while the above real and imaginary part are values. The are some basic functions available, which act on complex numbers. More complex calculations are possible with functions built in to modules such as cmath or numpy.

[83]:
c=c.conjugate()
print(c)
(2+4j)

Type conversion#

Different number types can be converted into each other in python. There is either an

  1. implicit

  2. explicit

way of type conversion.

The implicit conversion can be very useful but also dangerous. Python automatically may convers one data type to another, which is known as implicit type conversion.

[85]:
integer_number = 123
float_number = 1.23

type(integer_number),type(float_number)
[85]:
(int, float)
[86]:
new_number=integer_number+float_number
[87]:
type(new_number)
[87]:
float

In explicit type conversion, users convert the data type of an object to required data type using the built-in functions like int(), float(), str(), etc to perform explicit type conversion.

[88]:
num_string = '12'
num_integer = 23
[90]:
type(num_string)
[90]:
str
[92]:
num_string = int(num_string)
[93]:
type(num_string)
[93]:
int
[109]:
x = 5/2

print(x, type(x))
2.5 <class 'float'>
[110]:
x = int(x)

print(x, type(x))
2 <class 'int'>
[111]:
z = complex(x)

print(z, type(z))
(2+0j) <class 'complex'>
[97]:
type(z.imag)
[97]:
float

Complex variables cannot be cast to floats or integers. We need to use z.real or z.imag to extract the part of the complex number we want:

[98]:
y = bool(z.real)

print(z.real, " -> ", y, type(y))

y = bool(z.imag)

print(z.imag, " -> ", y, type(y))
2.0  ->  True <class 'bool'>
0.0  ->  False <class 'bool'>