Python Programming
Python Programming
▪ Variables are used to store data, they take memory space based on the type of value
we assigning to them. Creating variables in Python is simple, you just have write
the variable name on the left side of = and the value on the right side, as shown
below. You do not have to explicitly mention the type of the variable, python infer
the type based on the value we are assigning.
a, b, c = 5, 6, 7
print(a)
print(b)
print(c)
Plus and concatenation operation on the variables
x = 10 y = 20
print(x + y)
p = "Hello"
q = "World"
print(p + " " + q)
Data Types
A data type defines the type of data, for example 123 is an integer data while “hello” is a
String type of data. The data types in Python are divided in two categories:
1. Immutable data types – Values cannot be changed.
2. Mutable data types – Values can be changed
A python keyword is a reserved word which you can’t use as a name of your
variable, class, function etc. These keywords have a special meaning and they are
used for special purposes in Python programming language. For example – Python
keyword “while” is used for while loop thus you can’t name a variable with the
Variable name is known as identifier. There are few rules that you have to follow
For example here the variable is of integer type that holds the value 10. The name of
num = 10
Python Identifiers
1. The name of the variable must always start with either a letter or an underscore
(_). For example: _str, str, num, _num are all valid name for the variables.
2. The name of the variable cannot start with a number. For example: 9num is not
a valid variable name.
3. The name of the variable cannot have special characters such as %, $, # etc, they
can only have alphanumeric characters and underscore (A to Z, a to z, 0-9 or _ ).
4. Variable name is case sensitive in Python which means num and NUM are two
different variables in Python.
Python Identifiers Example
In the following example we have three variables. The name of the variables num, _x
and a_b are the identifiers.
Float – Values with decimal points are the float values, there is no need to specify the data
type in Python. It is automatically inferred based on the value we are assigning to a
variable. For example here fnum is a float data type.
# float number
fnum = 34.45
print(fnum)
print("Data Type of variable fnum is", type(fnum))
Python Data Type – String
String is a sequence of characters in Python. The data type of String in Python is called
“str”.
Strings in Python are either enclosed with single quotes or double quotes. In the following
example we have demonstrated two strings one with the double quotes and other string s2
with the single quotes.
# Python program to print strings and type
s = "This is a String"
s2 = 'This is also a String'
# displaying string s and its type
print(s)
print(type(s))
# displaying string s2 and its type
print(s2)
print(type(s2))
Python Data Type – Tuple
Tuple is immutable data type in Python which means it cannot be changed. It is an ordered
collection of elements enclosed in round brackets and separated by commas
# tuple of integers
t1 = (1, 2, 3, 4, 5)
# prints entire tuple
print(t1)
# tuple of strings
t2 = ("hi", "hello", "bye")
# loop through tuple elements
for s in t2:
print (s)
# tuple of mixed type elements t3 = (2, "Lucy", 45, "Steve")
''' Print a specific element indexes start with zero ''‘
print(t3[2])
Python Data Type – List
List is similar to tuple, it is also an ordered collection of elements, however list is a mutable
data type which means it can be changed unlike tuple which is an immutable data type.
A list is enclosed with square brackets and elements are separated by commas.
# list of integers
lis1 = (1, 2, 3, 4, 5)
# prints entire list
print(lis1)
# list of strings
lis2 = ("Apple", "Orange", "Banana")
# loop through tuple elements
for x in lis2:
print (x)
# List of mixed type elements
lis3 = (20, "Chaitanya", 15, "BeginnersBook")
''' Print a specific element in list indexes start with zero '''
print("Element at index 3 is:",lis3[3])
Python Data Type – Dictionary
Dictionary is a collection of key and value pairs. A dictionary doesn’t allow duplicate keys
but the values can be duplicate. It is an ordered, indexed and mutable collection of elements.
The keys in a dictionary doesn’t necessarily to be a single data type, as you can see in the
following example that we have 1 integer key and two string keys.
# Dictionary example
dict = {1:"Chaitanya","lastname":"Singh", "age":31}
# prints the value where key value is 1
print(dict[1])
# prints the value where key value is "lastname"
print(dict["lastname"])
# prints the value where key value is "age"
print(dict["age"])
Python Data Type – Set
A set is an unordered and unindexed collection of items. This means when we print the
elements of a set they will appear in the random order and we cannot access the elements of
set based on indexes because it is unindexed.
Elements of set are separated by commas and enclosed in curly braces.
# Set Example
myset = {"hi", 2, "bye", "Hello World"}
# loop through set
for a in myset:
print(a)
# checking whether 2 exists in myset
print(2 in myset)
# adding new element
myset.add(99)
print(myset)