Language:
Contact Us

Search

Notes Dhruv : Coding with Python

  • Share this:

15 Sep 2022:

Class work: Turtle Graphics 

Assignment :

1) Draw 

 

 

 

18.8.2022:

Class Work:

To find key exists or not

dict1 = {1:10, 2:20, 3:40, 5:100}

if 4 in dict1:
    print("Key is present")
else:
    print("Key not present")

 

2 June 2022:

Assignment:

1) Given a two Python list. Write a program to iterate both lists simultaneously and display items as shown in output.
input:
rollNo = [1, 2, 3, 4]
list2 = ["Dhruv", "John", "Ryan", "Albert"]
output :
1. Dhruv
2. John
3. Ryan
4. Albert

2) Write a function to remove empty strings from the list of strings
input : names = ["Mike", "", "Emma", "Kelly", "", "Brad"]
output:
["Mike", "Emma", "Kelly", "Brad"]

3. Write a function to print following pattern using for loop for n = 5 :
+
+ +
+ + +
+ + + +

+ + + + +

Classwork:

Computer Gussing using Binary Search 

low = 1 
high = 10000
noGusses = 0
mid = int((high+low)/2)

while (low < high):
    noGusses += 1
    
    reply = input(f"My guess {noGusses} is {mid} [{low} {high}]. M for Match. H for Higer. L for Lower: ")
    reply = reply.upper()
    if reply == "M":
        print(f"I got it correct in {noGusses} tries.")
        quit()
    if reply == "L":
        high = mid
        mid = int((high+low)/2 + 0.5)
    if reply == "H":
        low = mid
        mid = int((high+low)/2 + 0.5)
        
print("Error")
    

26 May 2022:

Assignment :

1) Gussing game (Human) to include
a) no. of tries, b) give message eg. enter number between 50 and 75.

2) Guessing game: Computer will guess the correct number.

Class work:

Random ineteger generator, Gussing game (human)
while loop, random.randint(start, end);

10 May 2022:

Assignment :

1) Write the program to give fibonacci series. If negative number is entered give proper message.

2) Write a powerFibonacci series programme. [1,1,1, 3, 5, 9, 17, 31, .....
(Next number is total of 3 previous numbers)

3) Take 3 numbers from user. start, end, div. Print all the numbres between start and end (both inclsive) divisible by div.

 

Class work: Range function to create list directly.

Fabonacci series  generator program.

num = int(input("How many numbers to generate: "))
fib = []
if num == 0:
    fib = []
elif num == 1:
    fib = [1]
elif num == 2:
    fib = [1,1]
elif num > 2:
    fib = [1,1]
    i = 0
    while i < num-2:
        fib.append(fib[i] + fib[i+1])
        i += 1 
print(fib)

27 Apr 2022::

Assignment : Rock, Paper, Scissors game : Computer 1 vs Computer 2.
Input : No of games

Class Work:

Developed Rock, Paper, Scissors game

20 Aprl 2022: Class work

Assignment:

1. Write a function to print following pattern using for loop for n = 4 :
D D D D
D D D
D D
D

2. Write a function to print following pattern using for loop for n = 5 :
#
# #
# # #

# # # #
# # # # #

3. Write a function to print following pattern (n = 5)
[All even numbers are printed as 0]
1
2
3
4
5

4. Print the following pattern using for loop (n = 5)

5 5 5 5 5

4 4 4 4 4

3 3 3 3 3

2 2 2 2 2

1 1 1 1 1

5. Print the following pattern using for loop (n = 5)

5 5 5 5 5

4 4 4 4

3 3 3

2 2

1

6. Print the following pattern using for loop (n = 5)

0
1 1
2 2 2 
3 3 3 3
4 4 4 4 4

def pattern1(n): #prints 1 to n number
    for x in range(1, n+1):
        print(x)

def pattern2(n):
    for x in range(1,n+1):
        for z in range(n+1-x):
            print(x, end = " ")
        print()    

def pattern3(n):
    for x in range(1, n+1):
        print((str(x) + " ") * (n+1))

print("Welcome to Dhruvs Pattern Generator v1.01")
n = int(input("Enter a number between 1 and 9: "))
if n < 1 or n > 9:
    print("Sorry: Not number between 1 and 9")
else:
    pattern1(n)
    pattern2(n)
    pattern3(n)

Sample Prog Run:

Welcome to Dhruvs Pattern Generator v1.01
Enter a number between 1 and 9: 4
1
2
3
4
1 1 1 1 
2 2 2 
3 3 

1 1 1 1 1 
2 2 2 2 2 
3 3 3 3 3 
4 4 4 4 4 

   

23 Mar2022: Assignment

1. Write a function to print following pattern using for loop for n = 4 :
x x x x
x x x
x x
x

2. Write a function to print following pattern using for loop for n = 5 :
+
+ +
+ + +
+ + + +

+ + + + +

3. Write a function to print following pattern (n = 5)
[All even numbers are printed as 0]
1
1 0
1 0 3 
1 0 3 0
1 0 3 0 5

4. Print the following pattern using for loop

5 5 5 5 5

4 4 4 4 4

3 3 3 3 3

2 2 2 2 2

1 1 1 1 1

16 Mar 2022: Assignment

1) Write a function which accepts a list and returns list of squres.
print(get_squres([1, 2, 3, 4, 5, 6, 7]))
output : [1, 4, 9, 16, 25, 36, 49]

Solution:

def get_squres(list1):
    list3 = []
    for x in range(len(list1)):
         list3.append(list1[x] + list2[x])
     return list3
print(get_squres([1,2,3,4,5])
 

2) Write a function to Join two list as below:
input: 
list1 = ["Hello ", "take "]
list2 = ["Dear", "Sir"]
output : ['Hello Dear', 'Hello Sir', 'take Dear', 'take Sir']

Solution:

list1 = ["Hello ", "take "]
list2 = ["Dear", "Sir", "Madam"]
list3 = []
for x in list1:
    for y in list2:
        list3.append(x + y)
print(list3)
 

3) Given a two Python list. Write a program to iterate both lists simultaneously and display items as shown in output.
input:
list1 = [10, 20, 30, 40]
list2 = [100, 200, 300, 400]
output :
10 100
20 200
30 300
40 400

Solution:

for x in range(len(list1)):
    print(list1[x])
list1 = [10,20,30,40]
list2 = [100,200,300,400]
for x in range(len(list1)):
    print(str(list1[x]) + " " + str(list2[x]))
 

4) Write a function to remove empty strings from the list of strings
input : list1 = ["Mike", "", "Emma", "Kelly", "", "Brad"]
output:
["Mike", "Emma", "Kelly", "Brad"]

list1 = ["Mike", "", "Emma", "Kelly", "", "Brad"]
list2 = []
for x in list1:
    if x != "":
        list2.append(x)
print(list2)
 

5) You have given a Python list. Write a program to find value 20 in the list, and if it is present, replace it with 200. Only update the first occurrence of an item.
Input : list1 = [5, 10, 15, 20, 25, 50, 20]
output: [5, 10, 15, 200, 25, 50, 20]

6. Given a Python list, write a program to remove all occurrences of item 20.
Input : list1 = [5, 20, 15, 20, 25, 50, 20]
output: [5, 15, 25, 50]

Tick Tack Toe

turn = 1  
list1 = [0,0,0,0,0,0,0,0,0]

def print_board():
    print(list1[0:3])
    print(list1[3:6])
    print(list1[6:9])

def get_winner():
    global turn
    if list1[0] == list1[1] and list1[1] == list1[2]  and list1[0] == 1:

def get_input():
    global turn
    x = int(input(f"Turn Player {turn}: Number from 1 to 9 : "))
    x = x-1
    if list1[x] == 0:
        list1[x] = turn
    else:
        print("Already occuppied")
        get_input()
    if turn == 1:
        turn = 2
    else:
        turn = 1

print_board()
for x in range(9):
    get_input()
    print_board()

9 Mar 2022: Assignment

1) Write a function which accepts a list and returns list of squres.
print(get_squres([1, 2, 3, 4, 5, 6, 7]))
output : [1, 4, 9, 16, 25, 36, 49]

2) Write a function to Join two list as below:
input: 
list1 = ["Hello ", "take "]
list2 = ["Dear", "Sir"]
output : ['Hello Dear', 'Hello Sir', 'take Dear', 'take Sir']

3) Given a two Python list. Write a program to iterate both lists simultaneously and display items as shown in output.
input:
list1 = [10, 20, 30, 40]
list2 = [100, 200, 300, 400]
output :
10 100
20 200
30 300
40 400

4) Write a function to remove empty strings from the list of strings
input : list1 = ["Mike", "", "Emma", "Kelly", "", "Brad"]
output:
["Mike", "Emma", "Kelly", "Brad"]

5) You have given a Python list. Write a program to find value 20 in the list, and if it is present, replace it with 200. Only update the first occurrence of an item.
Input : list1 = [5, 10, 15, 20, 25, 50, 20]
output: [5, 10, 15, 200, 25, 50, 20]

6. Given a Python list, write a program to remove all occurrences of item 20.
Input : list1 = [5, 20, 15, 20, 25, 50, 20]
output: [5, 15, 25, 50]

Classwork : for, range, printing patterns.

2 Mar 2022

Assignment : 

Write following Programmes in Thonny :

1) Print the following pattern using for loop

1 1 1 1 1 1

2 2 2 2 2 2

3 3 3 3 3 3

4 4 4 4 4 4

5 5 5 5 5 5

6 6 6 6 6 6

2) Print the following pattern using for loop

5 5 5 5 5

4 4 4 4 4

3 3 3 3 3

2 2 2 2 2

1 1 1 1 1

3) Print the following pattern using for loop

5 5 5 5 5

3 3 3 3 3

1 1 1 1 1

3) Print the following pattern using for loop

x
x x
x x x 
x x x x

3) Print the following pattern using for loop

@ @ @ @ @
@ @ @ @
@ @ @
@ @ 
@

3) Print the following pattern using for loop

@
@ @ 
@ @ @
@ @ @ @
@ @ @ @ @

3) Print the following pattern using for loop

@ @ @ @ @
@ @ @ @
@ @ @
@ @ 
@
@ @ 
@ @ @
@ @ @ @
@ @ @ @ @

23.2.22 Assignment

Write following programmes in Thonny :

1)  You need to remove items from a list while iterating but without creating a different copy of a list.
Remove numbers greater than 50

Given : number_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Expected output :  [10, 20, 30, 40, 50]

2) Print following pattern using for loop:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

3) Print following pattern using for loop :
5
4 4
3 3 3
2 2 2 2
1 1 1 1 1

4) Print the following pattern :
1 1 1 1 1 1
2 2 2 2 2
3 3 3 3
4 4 4 
5 5
6

5) Write a program to create a new string made of the middle three characters of an input string.
input : fatCATmat output : CAT
input: CarPenTer  output : Pen
Input : DhruvHasTable output : Has

Date : 23.2.22 Class Work

https://www.codewars.com/kata/57241e0f440cd279b5000829/train/python
https://www.codewars.com/kata/56bc28ad5bdaeb48760009b0/train/python
https://www.codewars.com/kata/551b4501ac0447318f0009cd/train/python
https://www.codewars.com/kata/5a2b703dc5e2845c0900005a/train/python
https://www.codewars.com/kata/5a3fe3dde1ce0e8ed6000097/train/python
https://www.codewars.com/kata/5467e4d82edf8bbf40000155/train/python
https://www.codewars.com/kata/568dc69683322417eb00002c/train/python
https://www.codewars.com/kata/56170e844da7c6f647000063/train/python
https://www.codewars.com/kata/5ae62fcf252e66d44d00008e/train/python
https://www.codewars.com/kata/5a995c2aba1bb57f660001fd/train/python
https://www.codewars.com/kata/609d17f9838b2c0036b10e89/train/python

 

Raz Manva

Raz Manva