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.
Python Collection(Arrays)
There are four collection data types in the Python programming language:
• List
• Tuple
• Set
• Dictionary
List
List is a collection which is ordered and changeable. Allows duplicate members. Access the list items by
referring to the index number. Python has a set of built-in methods that you can use on lists.
Method Description
append(value) Adds an element at the end of the list
len() To determine how many items have in list
insert(position, value) Adds an element at the specified position
count(value) Returns the number of elements with the specified value
index(value) Returns the index of the first element with the specified value
remove(value) Removes the element with the specified value
pop(position) Removes the element at the specified position
reverse() Reverse the order of the list
sort() Sorts the list
Example (1)
lst=[10,20,30,40,50,10]
print(lst)
print("Length=",len(lst))
print("Count 10=",lst.count(10))
print("Index 10=",lst.index(10))
lst.append(100)
lst.insert(2,1000)
print("After Append ",lst)
Example (2)
lst2=list(range(1,11))
print(lst2)
print("Sum=",sum(lst2))
lst2.insert(1,200)
print("Now Lst2=",lst2)
Example (3)
extend() -This function adds the elements of one list to the end of another list. It returns the extended list.
lstOne=[1,2,3]
lstTwo=[100,200]
lstOne.extend(lstTwo)
print("lstOne=",lstOne)
lstOne.remove(3)
print("After Remove lstOne=",lstOne)
print("lstOne pop",lstOne.pop())
print("lstOne pop",lstOne.pop())
print("lstOne pop",lstOne.pop(0))
print("lstOne=",lstOne)
Example (4)
lst=[10,-2,40,100,12]
lst.reverse()
print("Reverse lst=",lst)
lst.sort()
print(lst)
lst.sort(reverse=True)
print(lst)
Example (5)
The id() function in Python is used to return a unique id for an object. The id() function returns an identity of an
object. The id() function is useful to determine the type of an object and also to compare two objects.
lst2=lst[:]
lst.append(10245)
print(lst2)
print("ID=",id(lst))
print("ID=",id(lst2))
Tuple
A tuple is a collection which is ordered and unchangeable. Cannot add items after create the tuple and
remove items in a tuple. So, assign the items in a tuple while the tuple creation. In Python tuples are written
with round brackets.
Method Description
len() To determine how many items have in tuple
Example (6)
print("Tuple")
myTuple="John",
print("type=",type(myTuple))
print(myTuple)
print(myTuple[0])
student=(("John",20),("Su",15))
print(student)
print(student[0])
Example (7)
myTuple="John",20
print(myTuple)
print("Name=",myTuple[0])
print("Age=",myTuple[1])
Example (8)
students=(("Rose",16),("Susan",15))
print(students)
print(students[0])
print(students[1])
Set
A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.Once a
set is created, cannot change its items, but can add and remove items. Note: Sets are unordered, so the items
will appear in a random order.
Method Description
add(value) To add one item to a set
del() Delete the set
remove(value) To remove an item in a set (Note : If the item to remove does not exist, will raise an
error)
Example (9)
centre={"psd","mng","tgz","hld1","hld2"}
print(centre)
print(sorted(centre),len(centre))
del(centre)
print(centre)
Example (10)
numbers = {21, 34, 54, 12}
print('Initial Set:',numbers)
numbers.add(32)
print('Updated Set:', numbers)
Example (11)
programming = {'PHP', 'Java', 'Python'}
print('Initial Set:', programming)
removedValue = programming.remove('Java')
print('Set after remove():', programming)
programming.update(['VB','C','Ruby'])
print('Set after update():', programming)
Dictionary
A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written
with curly brackets, and they have keys and values. Adding an item to the dictionary is done by using a new
index key and assigning a value to it. Can access the items of a dictionary by referring to its key name.
Example (12)
mark1={"Myan":40,
"Eng":60,
"Math":72}
mark2={"Myan":80,
"Eng":70,
"Math":62}
print(mark1)
print(mark2)
Exercise(1)
Write a Python program to display the first and last colors from the following list.
#left to right -> start 0
#right to left -> start -1
Declare & Assign
variableName=value
Eg. a=1000.00 ,a=”pikachu” // = assignment operator(right to left assign)
a=100 , a=”Hello”
Python has several built-in data types.
int (integer)
float (floating-point number)
str(string)
bool (Boolean) …. Etc.
Example (1)
name="Mg Mg"
age=15
weight=100
height=5.8
marriage=False
print(name,"\t",type(name))
print(age,"\t",type(age))
print(weight,"\t",type(weight))
print(height,"\t",type(height))
print(marriage,"\t",type(marriage))
Example (2)
name=input("Enter your name:")
age=input("Enter your age:")
print ("Student Name=",name)
print ("Name Data Type=",type(name))
print ("Age="+str(age),"\nAge Data Type=",type(age))
Arithmetic Operator
Eg. 1+2*3=7
Eg. (1+2)*3=9
Operator Precedence
Bracket, Of, Division, Multiplication, Addition, Subtraction // () of / * + -
Example (3)
num1=int(input("Enter First Number:"))
num2=int(input("Enter Second Number:"))
print ("Sum=",num1+num2)
Example (4)
name= input("Enter Employee Name:")
salary = input("Enter salary:")
company = input ("Enter Company name:")
print("Printing Employee Details")
print ("Name", "Salary", "Company")
print (name, salary, company)
Example (5)
myan=int(input("Enter Myanamr Mark:"))
eng=int(input("Enter English Mark:"))
math=int(input("Enter Mathematics Mark:"))
sum=myan+eng+math
avg=sum/3
print("Total Mark=",sum)
print("Avg Mark"+str(avg))
The Python math module is a built-in library that provides access to mathematical functions. It includes
functions for basic operations like addition, subtraction, multiplication, division, and exponentiation. The math
module can help with tasks like calculating the area of a circle, calculating the sine of an angle, or finding the
square root of a number. It also provides constants such as pi, etc.
To use the math module, you need to import it using the "import" keyword.
Exercise (1)
Exercise (2)
Write a Python program that accepts the user's first and last name and prints them in reverse order with a
space between them.
Exercise (3)
Write a Python program that will accept the base and height of a triangle and compute its area.
Formula (b*h/2)