Python variables
Variables are containers for storing data values.
● Creating Variables
● Python has no command for declaring a variable.
● A variable is created the moment you first assign a value to it.
● Variables do not need to be declared with any particular type, and can even change type after they have
been set.
● Variable names are case-sensitive.
● can get the data type of a variable with the type() function
● If you want to specify the data type of a variable, this can be done with casting.
● String variables can be declared either by using single or double quotes
x=5
y=“hello world”
print(x)
print(y)
Output:
5
Hello world
x=4 # x is of type int
x = "Sally" # x is now of type str
print(x)
Output
Sally
Explanation:
It prints sally bcoz x is the recently stored value in a variable.
Casting
● If you want to specify the data type of a variable, this can be done with casting.
● Example output
● x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
Get the Type
● You can get the data type of a variable with the type() function.
● Example
● x=5
y = "John"
print(type(x))
print(type(y))
Single or Double Quotes?
● String variables can be declared either by using single or double quotes:
Example
● x = "John"
# is the same as
x = 'John'
Case-Sensitive
● Variable names are case-sensitive.
Example
● This will create two variables:
● a=4
A = "Sally"
#A will not overwrite a
How to name a Variable
● A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume).
Rules for Python variables:
● A variable name must start with a letter or the underscore character
● A variable name cannot start with a number
● A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ )
● Variable names are case-sensitive (age, Age and AGE are three different variables)
● A variable name cannot be any of the Python keywords.
valid variable names
myvar = “divya"
my_var = " divya "
_my_var = " divya "
myVar = " divya "
MYVAR = " divya "
myvar2 = " divya “
Illegal variable names:
2myvar = "John"
my-var = "John"
my var = "John"
Many Values to Multiple Variables
● Python allows you to assign values to multiple variables in one line:
● Example
● x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
One Value to Multiple Variables
● And you can assign the same value to multiple variables in one line:
● Example
● x = y = z = "Orange"
print(x)
print(y)
print(z)
Literals
● Literals in Python are fixed values written directly in the code that represent constant
data.
● They provide a way to store numbers, text, or other essential information that does not
change during program execution.
● Python supports different types of literals:
○ 10, 3.14, and 5 + 2j are numeric literals.
○ 'Hello' and "Python" are string literals.
○ True and False are Boolean literals.
DATA TYPES
Text Type: str
Numeric Types: int, float, complex
Sequence list, tuple, range
Types:
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType
● You can get the data type of any object by using the type() function:
● Print the data type of the variable x:
● x=5
print(type(x))