Python Variables and Data Types
Before starting with variables in python, let's start with the print function first.
To print something to the terminal in python, we use
print("Print something here")
This is what we will often use to see the outputs on the screen.
There are many faces of ways to print to the terminal using the print function in python. The above is the most basic one. You pass an object to the print function inside the pair of parenthesis.
The object can be anything from a simple string to a series of data.
For example, we can do this to print multiple strings and values.
print("Hello", "Alex.", 36, True)
and that would print out Hello Alex. 35 True
.
Declaring Variables
To declare a variable in python, we have to basically assign it a value. Unlike other languages where you can declare a variable and not initialize it.
The following are sample variable declarations in python.
Numeric Types Numeric values in python are dividied into 3 types.
- Integer
# Declare an integer value called with a variable name 'age'. age = 36 print(age) # Prints out 36
- Float
# Declare a floating point value with a variable name 'gravity'. gravity = 9.81 print(gravity)
Sequence Types
- String
# Declare a string variable website = "https://syncster.dev" print(website) # This prints out https://syncster.dev
- List
# Declare a list of strings fruits = ['apple', 'orange', 'grapes', 'banana']
You can declare a list of any type in python and even combination of data types
some_data = ['apple', 24, True, 1.23]
- Tuple
Tuple is similar to list but are declared in this way.
fruits = ('apple', 'orange', 'grapes', 'banana')
So what's the difference aside from square brackets and parentheses?
Well in python, lists are mutable while tuples are immutable. Mutable means they can be updated.
Mapping Type
- Dictionary
person = { "name": "Sidney Sheldon", "male": True, "profession": "Writer" }
Boolean Type
# Declare a true value is_male = True # Declare a false value dead = False
Take note of the capital letter 'T' and 'F'.
None Type
None types are used for null values.
deleted = None