-
1.1 Creating Variables
In Python, variables are created when you assign a value to it:
Examples:
x = 5
y = 10.2
z = “David”You can check the variable data types using the print command
print(x)
1.2 Data types
Python has the following data types built-in by default
Text Type: str
Numeric Types: int
,float
,complex
Sequence Types: list
,tuple
Dictionary Type: dict
Boolean Type: bool Set Types: set,frozenset Let’s look at some examples below
Text Type:
x = “Population”, here, x is a variable of type strNumeric Types:
x = 10, here x is a variable of type int
x = 8.2, here x is a variable of type float
x = 3+ 4j , here x is a variable of type complexSequence Type – List:
Names = [“John”, “David”, “Michael”]Sequence Type – Tuple:
Signal = (“red”, “yellow”, “green”)Dictionary Type – dict:
marks = {“Maths”:”90″, “Physics”:”85″}Boolean:
rained = TrueSet Type – set
subjects = {“Maths”,”English”,”Science”}Set Type – frozenset
subjects = ({“Maths”,”English”,”Science”})Know the type of a variable
x = 5
y = “John”print(type(x))
print(type(y))1.3 Data type casting
You can explicitly specify the data type of a variable using a method known as casting.
x = str(5) # x will be ‘5’
y = int(5) # y will be 5
z = float(5) # z will be 5.0