Python variable
Python variable is used to store data like a container. It is the basic unit of storage in Python programming. There is various kind of variables available in Python. You will learn all kinds of variables in this tutorial one after another.
Objectives
-
Create variable and assign value
-
Variable creation rules
-
Variable and multiple values
-
Variable using datatype
Create Python variable and assign value
Python has no keyword for declaring a variable. A variable is created the moment you first assign a value to it.
# Variable declare & assign value
string_variable = "This is String Data"
integer_variable = 100
float_variable = 100.10
boolean_variable = True
Python variable creation rules
-
Variable names can contain only letters (A-Z, a-z), numbers (0-9), and underscores (_).
-
Variable names cannot contain spaces.
-
A variable name must start with a letter or the underscore character
-
A variable name cannot start with a number
-
Variable names cannot be the same as keywords, reserved words
Valid | Invalid |
---|---|
|
|
|
|
|
|
|
|
Python variable and multiple values
Many variables assignment in single line
# Multiple assignment
car, country, model = "Rolls-Royce", "United Kingdom", "Phantom"
print(car + " " + country + " " + model)
Output
Rolls-Royce United Kingdom Phantom
Process finished with exit code 0
Multiple variable Same value assignment
# Multiple variable Same value assignment
weekend = public_holiday = eid_day = "Monday"
print(weekend + " " + public_holiday + " " + eid_day)
Output
Monday Monday Monday
Process finished with exit code 0
List value into variable
# List value into variable
car, country, model = ["Rolls-Royce", "United Kingdom", "Phantom"]
print(car + " " + country + " " + model)
Output
Rolls-Royce United Kingdom Phantom
Process finished with exit code 0
Python variable using datatype
Specifying variable datatype in python not mandatory like other languages, but if possible datatype specifying is a good practice in programming. It helps other programmer to understand the type of variable easily
Data Type | Example |
---|---|
String |
|
Number |
|
Boolean |
|