# Python Operators Examples

The python operators are used to evaluate expressions. An expression consists of values and operators which always evaluates to a single value.

The python operators are classified in following types.

### Program 1: Write a program to demonstrate the working of python arithmetic operator.

#### Solution:

#Filename : Arithmetic_Demo.py
#Write a program to demonstrate the working of python arithmetic operator.

#plus

num1 = 23
num2 = 56
result = num1 + num2

#minus

num1 = 321
num2 = 100
result = num1 - num2
print("Subtraction",result)

#multiplication

num1 = 24
num2 = 10
result = num1 * num2
print("Multiplication",result)

#division

num1 = 59
num2 = 3
result = num1 / num2
print("Division",result)

#floor division

num1 = 100
num2 = 31
result = num1 // num2
print("Floor Division",result)

#exponential

num1 = 3
num2 = 2
result = num1 ** num2
print("Exponential",result)

#### Solution:

Addition 79
Subtraction 221
Multiplication 240
Division 19.666666666666668
Floor Division 3
Exponential 9

### Program 2: Write a program to demonstrate the working of python assignment operators.

#### Solution:

File name: Assignment_demo.py

# simple assignment

x = 10
y = 20
z = 0

x += 10



# Python Mutable And Immutable Type Examples

Some data types in python are mutable type which means you can make changes to them without changing the data type memory location, which immutable types do not allow changes to existing values in memory. Any change in variable value will be stored in a different location in memory.

Here you will work on examples that demonstrate how mutable and immutable types work in python programming language. We recommend you to try the problem on your own first, then compare the results.

### Program 1: Write a program that demonstrates working of immutable types.

#### Solution:

# Filename : Immutable_types.py
# Write a program that demonstrates working of immutable types

myInteger = 35
print("Before Integer Update",id(myInteger))

#update the integer
myInteger = myInteger + 100

#print the new value and its memory address
print(myInteger)
print("After Integer Update",id(myInteger))

myFloat = 15.45
print("Before Float Update",id(myFloat))

#update the floating number
myFloat = myFloat + 5.42

#print the new value and its memory address
print(myFloat)
print("After Float Update",id(myFloat))

myString = "Hello"
print("Before String Update",id(myString))

#update the string

#print the new value and its memory address
print(myString)
print("After String Update",id(myString))

myComplex = 2 + 5j
myComplexTwo = 4 + 2j

print("Before Complex Update",id(myComplex))

#update the complex numbers
myComplex = myComplex + myComplexTwo

#print the new value and its memory address
print(myComplex)
print("After Complex Update",id(myComplex))

myTuple = (34,66,77)
print("Before Tuple Update",id(myTuple))

#update the tuple
#Tuples cannot be changed , but if you convert a tuple to list then you can update it.
myChangeList = list(myTuple)
myChangeList ="Superman"
myTuple = tuple(myChangeList)

#print the value and the memory address
print(myTuple)
print("After Tuple Update",id(myTuple))

#### Output:

Before Integer Update 1747543680
135
After Integer Update 1747545280
Before Float Update 43183456
20.869999999999997
After Float Update 42192624
Before String Update 43542976
After String Update 43617040
Before Complex Update 42811048
(6+7j)
After Complex Update 42056088
Before Tuple Update 42121816
(34, 'Superman', 77)
After Tuple Update 42423680

### Program 2: Write a program to demonstrate the working of mutable data types.

#### Solution:

# Filename : Mutable_type.py
# Write a program to demonstrate the working of mutable data types.

#sets

mySet = {23, 5, 66}
print("Before Set Update",id(mySet))

#update the sets using function update()
mySet.update("B")

#print the updated value
print(mySet)
print("After Set Update",id(mySet))

#lists

myList = [3, 6, 98, 22]
print("Before List Update",id(myList))

#update list by accessing the individual items
myList = "Krishna"

#print the updated list
print(myList)
print("After List Update",id(myList))

#dictionary

myDictionary = {
"ID": 234,
"Name": "Mary",
"Subject": "Mathematics",
}

print("Before Dictionary Update",id(myDictionary))

#update the dictionary by accessing individual key
myDictionary.update({"Course":"BSc"})

#print the updated dictionary
print(myDictionary)
print("After Dictionary Update",id(myDictionary))

#### Output:

Before Set Update 44014208
{66, 5, 'B', 23}
After Set Update 44014208
Before List Update 42321400
[3, 'Krishna', 98, 22]
After List Update 42321400
Before Dictionary Update 43772976
{'ID': 234, 'Name': 'Mary', 'Subject': 'Mathematics', 'Course': 'BSc'}
After Dictionary Update 43772976

Observations: The mutable types do not have a different memory address after the update like the immutable types.

# Python Data Types Examples

Data types are fundamental to programming languages and they define the type of values get stored in a python variable. Here you will practice python data types which are similar to other programming languages, yet python don’t have strict type checking like C++, Java, etc. Strict type checking means the you need to declare the type of data before storing value in a variable. For example, int Num; Here ‘int’ means integer type.

It is recommended that you try these problems on your own and then compare the results. May be you may come up with a better way to solve the problem.

### Program 1: Write a program that store number data types , output the type and the data.

#### Solution:

# Filename : Number_type.py
# write a program that store number types, output the values and type

# number types are - integer, float , complex , and boolean

integer_type = 23
float_type = 344.66
complex_type = 3 + 2j
bool_type1 = True
bool_type2 = False

# Print values and their types

print(integer_type)
print(type(integer_type))

print(float_type)
print(type(float_type))

print(complex_type)
print(type(complex_type))

print(bool_type1)
print(type(bool_type1))

print(bool_type2)
print(type(bool_type2))

#### Output:

23
<class 'int'>
344.66
<class 'float'>
(3+2j)
<class 'complex'>
True
<class 'bool'>
False
<class 'bool'>

### Program 2: Writing a program to store ordered data types such as strings, lists, and tuples and display their value. Also, output their type information.

#### Solution:

# Filename : ordered_data_type.py
# Writing a program to display values of ordered data type with type information

# python ordered types are string, list, and tuple

myString = "Himalayas"

myList = ["Milk","Banana", "Bread", "Chocolate", 400]

myTuple = (100, "Notesformsc", "ComputerScience")

# print the values and the type

print(myString)
print(type(myString))

print(myList)
print(type(myList))

print(myTuple)
print(type(myTuple))

#### Output:

Himalayas
<class 'str'>
<class 'list'>
(100, 'Notesformsc', 'ComputerScience')
<class 'tuple'>

### Program 3: Write a program to display third letter of the string “MOUNTAIN”.

#### Solution:

# Filename : Display_letter.py
# Writing a program to display third letter of the string "MOUNTAIN"

myString = "MOUNTAIN"

# index start at 0; therefore, third letter is index is 2

myLetter = myString

# print the output

print("Third letter is",myLetter)

#### Output:

Third letter is U

### Program 4: Write a program in Python to store a None type and display the results with its type information.

#### Solution:

# Filename : None_type.py
# Write a program that store None type and display the results with its type information.

# the None type store nothing, its not zero, not null, etc

myValue = None

print(myValue)
print(id(myValue))

#### Output:

None
1786346564

### Program 5: Write a program that demonstrate that python sets cannot store duplicate values.

#### Solution:

# Filename : Sets_duplicate_value.py
# Write a program to demonstrate that python sets cannot store duplicate values.

mySet = {230, 500,100, 500, 670}

print(mySet)
print(type(mySet))

#### Output:

{100, 670, 500, 230}
<class 'set'>

### Program 6: Write a program using dictionary to store records of 5 students and display them as output.

#### Solution:

# Filename : Student_record_dictionary.py
# Write a program using dictionary to store records of 5 students and display them as output.

firstStudent = {
"ID": 35,
"Name": "Ram",
"Subject":"Math",
"Class": 10,
}
secondStudent = {
"ID": 36,
"Name": "Megha",
"Subject":"Chem",
"Class": 10,
}
thirdStudent = {
"ID": 37,
"Name": "John",
"Subject":"Hist",
"Class": 11,
}
fourthStudent = {
"ID": 38,
"Name": "Lee",
"Subject":"Engl",
"Class": 12,
}
fifthStudent = {
"ID": 39,
"Name": "Geeta",
"Subject":"Math",
"Class": 12,
}
# Display student data

print("ID","Name","Section","Class")
print(firstStudent["ID"],firstStudent["Name"],firstStudent["Subject"],firstStudent["Class"])
print(secondStudent["ID"],secondStudent["Name"],secondStudent["Subject"],secondStudent["Class"])
print(thirdStudent["ID"],thirdStudent["Name"],thirdStudent["Subject"],thirdStudent["Class"])
print(fourthStudent["ID"],fourthStudent["Name"],fourthStudent["Subject"],fourthStudent["Class"])
print(fifthStudent["ID"],fifthStudent["Name"],fifthStudent["Subject"],fifthStudent["Class"])

#### Output:

ID Name Section Class
35 Ram Math 10
36 Megha Chem 10
37 John Hist 11
38 Lee Engl 12
39 Geeta Math 12

# Python Examples

Python is a high level language and you as a learner must practice a lot to master this programming language. Here we provide you examples to practice with solutions.

You must install the python 3x compiler suitable for your computer and practice programming as you learn python. To learn the python language visit our Python tutorial page.

### Prerequisites

There are few prerequisites before you start practicing the examples,

• You must install a Python 3.x.x compiler on you computer. Make sure there are no error while working with the compiler.
• Keep a note book or use computer notepad to manually work out the problem before writing the programs.
• Keep a separate folder to keep your programs so that you can work on them later.

### Python Example Programs

The python examples are organized based on programming concepts. If you are learning a particular concept, you can check you understanding by working through these examples.

# Python Variable Examples

This post contains python variable examples. You can start with first example and work your way through rest of the programs. For each program the solution is provided after the source code.

You must try to write the program yourself and compare the results later.

### Program 1: Write a program to display the value of variables

#### Solution:

# filename: greetings.py
# program to display values of variables

greeting = "Happy Diwali"
print(greeting)

memberID = 343
print("MemberID is",memberID)

#### Output:

Happy Diwali
MemberID is 343

### Program 2: Writing a program to find the area of a rectangle where length of the rectangle is 120 units and breadth is 40 units.

#### Solution:

# filename : rectangle_area.py
# write a program to find the area of a rectangle with length = 120 units and breadth = 40 units.

length = 120
print("Area =",area_rectangle)

#### Output:

Area= 4800

### Program 3: Write a program to display total marks in 3 subjects – Math, English, and Science and include comments to explain your code.

#### Solution:

# filename : total_marks.py
# write a program to display total marks in 3 subjects - Math, English, and Science and include comments to explain your code.

# marks in three subjects for a student
Math = 77
English = 100
Science = 233

# compute total marks by adding all subject marks

Total_Marks = 77 + 100 + 233

# print total marks

print("Total Marks","=",Total_Marks)

#### Output:

Total Marks = 410

### Program 4: Python treat every value as an object by assigning a unique ID number. Write a program using function “id()” to display the object unique id.

#### Solution:

# filename : find_object_id.py
# Write a program to display object unique id using function id()

num = 30
print(id(num))

name = "Ram"
print(id(name))

#### Output:

1802659376
43805184