Numeric and textual types, and their operations#
Numerical data types#
Python has two built-in numerical data types: integers (int) and floating point numbers (float). In essence, a number that does not have a decimal point is treated as an integer and a number that has a decimal point is treated as a floating point number:
# a floating point number
3.14
3.14
# an integer
5
5
To enter big and small numbers, use the engineering notation with e indicating exponent (notice that engineering notation always create floating point values). Note that the use of enginnering notation automatically imply that the number is a float
# a floating point number declared using engineering notation
5e2
500.0
The usual arithmetic operators + -, *, and / carry the usual meaning in python:
1 + 3
4
7 - 12
-5
5 * 2.5
12.5
4 / 3
1.3333333333333333
In addition, python also has ** for exponentiation, // for floor division, and % for remainder
2 ** 3
8
7 // 3
2
7 % 3
1
Note that different operators have different precedence. In particular, ** is carried out before * and /, which are in turns carried out before + and -
5 + 4 * 2 ** 3
37
To force certain operations to execute first, surround the corresponding expression with parentheses ()
((5 + 4) * 2) ** 3
5832
In addition, python also has a small number of built-in mathematical functions. Notably, the collection includes abs() for absolute value, round() for rounding, max() for maximum, and min() for minimum
# the maximum among the values supplied
max(7, 10, -1)
10
# round to 2 decimal points
round(3.1415926, 2)
3.14
Assigning values to variables#
In python, the = sign is used as the assignment operator. In essence, in a statement with =, the right hand side (RHS) of the = is evaluated, and the resulting value is assigned to the symbol on the left hand side (LHS)
# assign the result of the calculation to variable x
x = 3 + 12
# Use the variable x in subsequent calculations
x + 5
20
Note that because the right-hand side is evaluated before the assignment, a statement like x = y does not establish a symbolic relationship between the variables x and y. Instead, it merely assign x to the value of y.
In particular, if we assign the value of y to something else later, it will not affect the value of x:
y = 1.5
x = y
y = 3
print(x)
1.5
In python, a variable name can consists of letters, numbers, and the underscore (_) symbol, but it cannot start with a number. Here are some examples of allowed variable names:
this_is_a_long_name = 1
thisValue = 2
_x = 3
t2 = 12
Some variable names that are not allowed are:
2y = 13
Cell In[19], line 1
2y = 13
^
SyntaxError: invalid decimal literal
this-value = 15
Cell In[20], line 1
this-value = 15
^
SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
Textual data types#
The textual data type in python is called string (str). It is defined by surrounding the literal text with a pair of " or ':
string1 = "this is a string"
string2 = "this is also a string"
The + operator can be used to concatenate strings, and the * operator can be used to repeat a string multiple times:
string1 + ". " + string2
'this is a string. this is also a string'
string1 * 3
'this is a stringthis is a stringthis is a string'
Note that the + operator is not defined between a string and a numerical value
string1 + 3
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[24], line 1
----> 1 string1 + 3
TypeError: can only concatenate str (not "int") to str
Type checking and type conversion#
You can check the type of an object using the type() function, and you can convert between different types (if the conversion is possible) using int() (to integer), float() (to floating point), and str() (to string):
# check the type of a variable
x = 1.537
type(x)
float
# convert a floating point number to integer, with truncation
int(7.31)
7
# convert a string to a number
float("6.21")
6.21
# convert a number to a string
str(6.21)
'6.21'