Python Variables¶
Python variables are like containers that store data values, allowing you to label and reference that data throughout your code. They let you assign and reassign values, making it easier to manage and manipulate information as your program runs.
Naming Variable¶
The simplest way to describe variables is that they are names that are assigned to values. These names can start with either a letter or an underscore, but not a number.
# correct way to name a variable
variableName = 1
_variable = 1
# incorrect way to name a variable
1variable = 1
Cell In[4], line 2 1variable = 1 ^ SyntaxError: invalid decimal literal
Case Sensitive Variables¶
Variable names are case sensitive, meaning two variables with the same name but one has an uppercase letter will be treated differently than the variable with the lowercase letter.
var = 1
print("var :", var)
VAR = 2
print("VAR :", VAR)
var : 1 VAR : 2
Assign Multiple Variables
You can assign mulitple variables in one line. It's not good practice to do this, but it is possible.
variable1 = variable2 = variable3 = 4
print("vairable1: ", variable1)
print("vairable2: ", variable2)
print("vairable3: ", variable3)
vairable1: 4 vairable2: 4 vairable3: 4
Python Data Types¶
In Python, data is dynamically typed, which means that Python automatically determines the type of a variable (such as integer, float, string, etc.) based on the data you assign to it, without requiring explicit specification. The type of variable can dynaimcally change as you assign different types.
variable = 1
print("Vairable is of type :", type(variable))
variable = "hello"
print("Now, it is of type :", type(variable))
Vairable is of type : <class 'int'> Now, it is of type : <class 'str'>
Basic Data Types in Python
To get the type fo a variable or data type, use type()
var = 10
print("integer: ", type(var))
var = 1.5
print("float: ", type(var))
var = 10 + 5j
print("complex numbers: ", type(var))
var = True
print("boolean: ", type(var))
var = "Hello"
print("string: ", type(var))
integer: <class 'int'> float: <class 'float'> complex numbers: <class 'complex'> boolean: <class 'bool'> string: <class 'str'>
Casting Data Types¶
Casting allows you want to change a variable's data type to a different type. For example, if you want to change the type of a variable, which is a float, into an integer, you would use int()
.
float_variable = 3.14
print("Original variable type: ", type(float_variable))
print("Casting to an integer: ", type(int(float_variable)))
Original variable type: <class 'float'> Casting to an integer: <class 'int'>
You can try casting other types such as string, char and ordinal.
# convert a number into a string
print("Casting to a string: ", type(str(32)))
# convert a number into a character
print("Casting to a character: ", type(chr(32)))
# convert an individual character into an integer representation (ASCII)
print("Casting to an ordinal: ", type(ord('d')))
Casting to a string: <class 'str'> Casting to a character: <class 'str'> Casting to an ordinal: <class 'int'>