Sample Exercises for Python

 Sample Exercises in Python:

# Numbers 

# integers 1 2 3 4 5 

# float 1.2 5.6 9.5 10.50 

# complex 5+2j 

# Sample Operators  "-, *,  /,  +,  **,  %, //" 

print(3 // 2) 


# Variables , a-z , _ , watch , Watch (Case Sensitive)

Watch_price = 500 

Customer_Name = "mohi" 

Watch1 = Name

1 = 750 

print(Watch_price) 

print(Customer_Name) 

Watch1, Watch2 = 500, 750

Watch1 = Watch2 = 500


# Strings (Values are storing as an Array)

word = 'Hi' 

word2 = " my age is 24 , I can vote " 

para = """ this is my para """ 

word3 = "hello, world" 

print(word[1])


# slicing , length , strip() 




# Operators 

# +, -, *, /, //, %  // Arithmetic Operator

# = // Assignment Operator

# > < >= <= // Comparison Operator

# and or not // Logical Operator

# is is not // Identity Operator

# in not in // Membership Operator

# & | ^ ~ << >> // Bitwise Operator

number1 = 10 

number1 /= 10 

number2 = number1 + 20 


Number1 = "10"

Number1 += "10"

print(Number1)

Number2 = 10

Number2 += 10  // -=, *=, /=

print(Number2)

Output:

1010

20


# Casting

x = 10

y = 10.10

z = "mohi"

print(type(x))

print(type(y))

print(type(z))

Output:

<class 'int'>

<class 'float'>

<class 'str'>


a = float(10) 

b = int(10.10) 

c = str(120) 

print(a, b, c) 

Output:

10.0 10 120


# Type

d = "h3"

print(type(d)) 

Output:

<class 'str'>


# list [ ] // Can contains duplicate values


# Tuples ( ) // Python tuples are a data structure that store an ordered sequence of values. 
Tuples are immutable. This means you cannot change the values in a tuple.

Name = ['mohi', 'mano', 'anand', 'uday']
Friends = ('mano', 'gowtham', 'anand', 'arun')
Mates = ('anitha', 'geetha', 'kalpana', 'selvi')
print(Friends)
Add1 = Friends + Mates
print(Add1)
Add = Friends + Name
print(Add)
Output:
TypeError: can only concatenate tuple (not "list") to tuple // Friends is tuple and Name is List so we can not concatenate
('mano', 'gowtham', 'anand', 'arun')
('mano', 'gowtham', 'anand', 'arun', 'anitha', 'geetha', 'kalpana', 'selvi')


# set { } // No Index values, order gets modify automatically while run a program repeatedly, can be used in games, like open an enemy.
# Set
city = {'chennai', 'madurai', 'trichy'}
print(city)
city.add('kovai')
print(city)

Output:
{'madurai', 'trichy', 'chennai'}
{'madurai', 'trichy', 'chennai', 'kovai'}

{'chennai', 'madurai', 'trichy'}
{'kovai', 'chennai', 'madurai', 'trichy'}


# Dictionary , get() // Store the data "Key, Values" and reuse, like "set" pattern.
# Dictionary
my_data = {
    "name": "mohi",
    "age": "40"
}
print(my_data)
print(my_data.get("name"))
my_data["age"] = "41"   // Updating "value" of "key" (age).
print(my_data)

Output:
{'name': 'mohi', 'age': '40'}
mohi
{'name': 'mohi', 'age': '41'}


# if, elif, else statements

# if statements

your_age = 40

if your_age > 18:

    print("You can vote")

else:

    print("You can not vote")


Output:

You can vote

____________________

your_age = 18

if your_age > 18:

    print("You can vote")

elif your_age == 18:

    print("Check your voterID")

else:

    print("You can not vote")


Output:

Check your voterID

_________________

a, b = 100, 200

if a == 100 and b == 100:

    print("a is equals to b")

else:

    print("a is not equals to b")


Output:

a is not equals to b

________________


if a == 100 or b == 100:

    print("either a or b is 100")

else:

    print("both a and b are not 100")


Output:

either a or b is 100

_______________

# nested if

if a == 100 or b == 100:

    print("either a or b is 100")

    if a == 100:

        print("a is 100")

else:


Output:

either a or b is 100

a is 100

_____________________

# functions Keyword: def, function must call by it's name in the program.

# Function without parameter

def addition():

    a = 10

    b = 20

    print(a+b)

addition()

addition()

Output:

30

30

------------------


# Function with parameter

def addition1(a, b):

    print(a + b)


addition1(60, 20)

addition1(500, 1000)


Output:

80

1500

_____________________

def subtract(x, y):

    print(x - y)


subtract(30, 15)


Output:

15

_________________

def hi(name):

    print("hi," + name)


hi("mohi")   // Calling the function


Output:

hi,mohi

_________________


def ret():

    return 100


print(ret())


def ret1(a):

    return a*100


print(ret1(5))

Output:

100

500

_________________


def addition(a, b): 

    print(a + b) 

def subtraction(a, b): 

    print(a - b) 

def hi(name): 

    print("Hi," + name) 

def fun(a): 

    return a*100 


addition(12, 10) 

addition(100, 300) 

subtraction(50, 25) 

hi("balu") 

print(fun(5)) 

___________________

# loop, range, for, break

# Loop

# for loop

name = "mohi"

for letters in name:

    print(letters)

Output:

m

o

h

i

__________________

names = ["mohi", "anand", "uday", "mano"]

for name in names:

    print(name)

Output:

mohi

anand

uday

mano

__________________

for name in "mohi", "anand", "uday", "mano":

    print(name)

Output:

mohi

anand

uday

mano

__________________

for color in "Grey", "Black", "Blue", "Pink":

    if color == "Black":

        print("Black color is there in the list")

    else:

        print("Other than Black is there in the list")

Output:

Other than Black is there in the list

Black color is there in the list

Other than Black is there in the list

Other than Black is there in the list

_____________________________

# break // keyword

# Break the loop once the condition met

for color in "Grey", "Black", "Blue", "Pink":

    if color == "Black":

        print("Black color is there in the list")

        break

    else:

        print("Other than Black is there in the list")

Output:

Other than Black is there in the list

Black color is there in the list

__________________

# Continue the loop once the condition met

for color in "Grey", "Black", "Blue", "Pink":

    if color == "Black":

        continue

    else:

        print("Other than Black is there in the list")

Output:

Other than Black is there in the list

Other than Black is there in the list

Other than Black is there in the list

____________________________

# Range

for numbers in range(6):

    print(numbers)

Output:

0

1

2

3

4

5

_________

for numbers1 in range(3, 6):

    print(numbers1)

Output:

3

4

5

_________


for numbers2 in range(100, 200, 20):   // Start from 100 to 200 and 20 as break.

    print(numbers2)

Output:

100

120

140

160

180

__________

# for else loop

for numbers3 in range(6):

    print(numbers3)

else:

    print("Numbers from 0 to 5 are printed")

Output:

0

1

2

3

4

5

Numbers from 0 to 5 are printed

______________

# Nested for loop

# Nested for loop

for numbers4 in range(6):

    print(numbers4)

    for i in range(2):

        print(i)

else:

    print("Numbers from 0 to 5 are printed")

Output:

0

0

1

1

0

1

2

0

1

3

0

1

4

0

1

5

0

1

Numbers from 0 to 5 are printed

_________________


# while loop

number_list = 1

while number_list < 6:

    print(number_list)

    number_list += 1

Output:

1

2

3

4

5

__________


# while else loop

number_list1 = 1

while number_list1 < 6:

    print(number_list1)

    number_list1 += 1

else:

    print("Number's printing done")

Output:

1

2

3

4

5

Number's printing done

_____________


# Lambda // A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression. 

add_l = lambda number: number + 100

print(add_l(10))

print(add_l(100))

Output:

110

200

______________


Input User Data:

# User Input

Your_Name = input ()

print("My Name is " +Your_Name)


mohi

My Name is mohi

_______________


# User Input

Your_Name = input ("Enter Your Name: ")

print("My Name is " +Your_Name)


Enter Your Namemohi

My Name is mohi

_________________


# User Input

number1 = input("Enter number1: ")

number2 = input("Enter number2: ")

print(number1 + number2)


Enter number1: 100

Enter number2: 200

100200   // Since the values are considered as a "string" it will do the concatenation and didnot perform addition.

so we need to apply "casting" here,

# User Input

number1 = int(input("Enter number1: "))

number2 = int(input("Enter number2: "))

print(number1 + number2)


Enter number1: 100

Enter number2: 200

300

_______________


# Simple Calculator Program

# Define functions

def add(a, b):

    return a+b

def sub(a, b):

    return a-b

def mul(a, b):

    return a*b

def div(a, b):

    return a//b


# Get input of Choice

print(""" Choose Your Operation: 

Type 1 for Addition

Type 2 for Subtraction

Type 3 for Multiplication

Type 4 for Division""")


# Define Variables

Choice = int(input("Enter Your Choice: "))

a = int(input("Enter Your First Number: "))

b = int(input("Enter Your Second Number: "))


# Conditions

if Choice == 1:

    print(add(a, b))

elif Choice == 2:

    print(sub(a, b))

elif Choice == 3:

    print(mul(a, b))

elif Choice == 4:

    print(div(a, b))

else:

    print("Enter Valid Choice")


Output:

Choose Your Operation: 

Type 1 for Addition

Type 2 for Subtraction

Type 3 for Multiplication

Type 4 for Division

Enter Your Choice: 1

Enter Your First Number: 34

Enter Your Second Number: 23

57

___________________













Popular posts from this blog

Python Libraries

Python Intro