+91 81900 98289

Python Variables

This Python Variables lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

A variable is a name attached to a value. You create it by writing the name, an equals sign, and the value. Python has no separate declare step, and you do not write the type in front of the name.

The value still has a type. Text is str, a whole number is int, a decimal is float, and True or False is bool. One name can later hold a different type. The new assignment replaces the old value.

type() shows the type of the value stored right now. str(), int(), and float() build a new value of the type you ask for. The text "24" and the number 24 are not the same.

Names are case-sensitive, so city and City are two variables. Text can use single quotes or double quotes. Both produce str.

Example 1: Start here

The = sign creates the variable. Words after # are a note for you. Python skips them and still runs the code.

city = "Chennai" # text, type str seats = 24 # whole number, type int print(city) print(seats)

Example 2: The type can change

label starts as a whole number. The comment after # says int. The next assignment stores text, so the same name is now str. Python does not run the comment.

label = 24 # label is type int print(label, type(label)) # type() shows what kind of value this is label = "full" # label is now type str print(label, type(label)) # type() shows what kind of value this is

Example 3: Text, numbers, and yes or no

Each comment names the type stored on that line. course is text, seats is a whole number, fee has a decimal point, and open_batch is True or False.

course = "Python" # str, text seats = 24 # int, whole number fee = 24999.50 # float, decimal open_batch = True # bool, yes or no print(type(course)) # type() shows what kind of value this is print(type(seats)) # type() shows what kind of value this is print(type(fee)) # type() shows what kind of value this is print(type(open_batch)) # type() shows what kind of value this is

Example 4: Convert with casting

str() makes text, int() reads a whole number, and float() keeps a decimal. The # note tells you which conversion that line does.

seats_text = str(24) # int becomes text seats = int("24") # text becomes int fee = float(24) # int becomes 24.0 print(seats_text, type(seats_text)) # type() shows what kind of value this is print(seats + 1) print(fee)

Example 5: Single quotes or double quotes

Both quote styles store text. The comparison prints True because the letters match.

name_a = "Asha" # double quotes, still str name_b = 'Asha' # single quotes, still str print(name_a == name_b) # a comparison is True or False print(type(name_a), type(name_b)) # type() shows what kind of value this is

Example 6: Names are case-sensitive

city and City are not the same name. Changing City does not change city.

city = "Chennai" # lowercase name City = "Pune" # different variable print(city) print(City)

Example 7: Print a variable in a sentence

The first print mixes text and a number with commas. The second joins text only, so str() converts the number first.

course = "Python" # text seats = 24 # a whole number print(course, "has", seats, "seats") # comma accepts the number print(course + " has " + str(seats) + " seats") # plus needs text

Example 8: One name can hold many values

Square brackets make a list. Curly braces make a dictionary. Each one is still a single variable.

skills = ["Python", "SQL"] # list, ordered values scores = {"Asha": 82, "Arun": 74} # dict, name and number print(type(skills)) # type() shows what kind of value this is print(type(scores)) # type() shows what kind of value this is print(skills[0]) print(scores["Asha"])
The = sign creates the variable. Words after # are a note for you. Python skips them and still runs the code.
Call Now 81900 98289