The next topic to discuss is Python Variables and Integers from our Python data types.
Python Variables
We can store objects in Python, and these objects are called variables. Variables are a way for us to store information to be re-used again in our scripts. Variables in Python are written incamel_case
and have to have the following characteristics:
- You cannot start with a number
- It must be in all lower case; all upper case can be used, but that is for a special variable called a global variable
- Do not use names that have special meanings to Python like
- len
- str
- The name can not start with any of the following characters under normal circumstances:
: ' '' < > / ? | ( ) ! @ # $ % ^ & * ~ - +
With variables, Python does not require you to say what kind of data type will be inserted into a variable; instead, it uses a concept called dynamic typing. This means that variables can be reassigned to the same or a different data type. We can now take variables and run them through math functions as we did in the previous post:
a = 10
b = 20
a+b
30
a-b
-10
a*b
200
a/b
0.5
Integers
An integer is a whole number in Pythion. Python can be used as a basic calculator, or we can use some of the below built-in functions that Python offers for integers, which are:
- bin():
- Returns the binary equivalent of the integer passed in
bin(64)
'0b1000000'
bin(12)
'0b1100'
a = 128
bin(a)
'0b10000000'
- Returns the binary equivalent of the integer passed in
- hex():
- Returns the hexadecimal equivalent of the integer passed in
hex(54)
'0x36'
hex(10)
'0xa'
hex(255)
'0xff'
a = 91
hex(a)
'0x5b'
- Returns the hexadecimal equivalent of the integer passed in
- abs()
- Returns the absolute value of the integer passed in
abs(45)
45
abs(-123)
123
abs(.42342)
0.42342
a = -1293.12
abs(a)
1293.12
- Returns the absolute value of the integer passed in
- sum()
- Returns the sum of the iterator that is passed into it
sum(range(10))
45
- Returns the sum of the iterator that is passed into it
More on Python variables and integers can be found here.