PYTHON_LEARNING_DUMP

  

# using while loop

 a=10

count=0

while count<a :

  count = count+1

  print (count)


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


#count total number of spaces, vowels and consonants in the given string


string = input("ENTER THE STRING:\n")

length= len(string)

print(f"total length of given string is: {length}")


#number of vovel

vowel="aeiou"

vow_count=0

cons = "bcdfghjklmnpqrstvwxyz"

cons_count=0

space=" "

space_count=0

string=string.lower()

for i in string:

 if i in cons:

  cons_count = cons_count+1  

 elif i in vowel:

  vow_count = vow_count+1  

 elif i in space:

  space_count = space_count+1


print(f"Total number of spaces in the given string is: {space_count}")

print(f"Total number of consonants in the given string is: {cons_count}")

print(f"Total number of vowels in the given string is: {vow_count}")

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

"""

Create a function which accepts two parameters with default values
And it should process both the string paramters and print both of them first and then return a string with maximum length
Call it with overwriting the default parameters
"""











def func1(par1="I am happy to learn PYTHON", par2="I am happy to be into IT"):
    len1 = len(par1)
    len2 = len(par2)

    longer_str = par1 if len1 > len2 else par2
    return longer_str

num_var = func1()
print("The longer string is:", num_var)

print("this is for calling the function and overriding the default parameters")

num2_var = func1("hello this is nikita", "hello this is neha")
print("The longer string is:", num2_var)

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

"""
Create a function which can accept a number as parameter and get me the sum of all those number which are preceding given parameter
Def sum(n):
Sum(2) - 0,1,2 = 3
Sum(5) – 0,1,2,3,4,5=15
"""
def sum_of_num(n):
 if n > 0:
  summ = n + sum(n-1)
  return summ
 else:
  return 0

num1 = int(input("Enter the number:"))
num = sum_of_num(num1)
print(f"Sum of {num1} = {num}")

summ = n + sum(n-1)
               ^^^^^^^^
TypeError: 'int' object is not iterable
Instead of using sum(n-1), you should call the sum_of_num function with n-1 as an argument. 

def sum_of_num(n):
    if n > 0:
        summ = n + sum_of_num(n-1)
        return summ
    else:
        return 0

num1 = int(input("Enter the number:"))
num = sum_of_num(num1)
print(f"Sum of {num1} = {num}")

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

#Create a list of cities (minimum 10) and build a logic to identify a city with more consonants















cities = ["Delhi", "Indore", "Lucknow", "Jabalpur", "Gaziabad", "Noida", "Kota", "Satna", "Nagpur", "Ballia"]
print(f"The list of 10 cities are as follows:\n{cities}")

cons="bcdfghjklmnpqrstvwxyz"
max_c_count = 0 
max_c_city_name = []



for city in cities:
 c_count = 0  
 for char in city.lower(): 
    if char in cons:
      c_count += 1


 print(f"Number of consonants in {city}: {c_count}")
 if c_count > max_c_count:
        max_c_count = c_count
        max_c_city_name = [city]
 elif c_count == max_c_count:
        max_c_city_name.append(city)

print(f"Cities with the most consonants: {max_c_city_name}")

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

 #Create a list of menu items of 10

#Build a logic to loop through one by one and give me the min length item name

C:\Users\hp\notes>python min_len.py
Menu with 10 items:
['Samosa', 'Pizza', 'Idli', 'Salad', 'Dosa', 'Ice Cream', 'Pasta', 'Sandwich', 'Noodles', 'Manchurian']
Items with the minimum length: ['Idli', 'Dosa']

menu = ['Samosa', 'Pizza', 'Idli', 'Salad', 'Dosa', 'Ice Cream', 'Pasta', 'Sandwich', 'Noodles', 'Manchurian']

print(f"Menu with 10 items:\n{menu}")

min_len = len(menu[0])

min_len_items = []


for item in menu:

    item_len = len(item)


    if item_len < min_len:

        min_len = item_len

        min_len_items = [item]

    elif item_len == min_len:

        min_len_items.append(item)


print(f"Items with the minimum length: {min_len_items}")

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

'''

Create a list of odd numbers (at least 25) and find all those numbers which are divisible by other than 1 and on its own

(example 5 vs 9)

'''

odd_num = [3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51]

num = []


for i in odd_num:

    is_composite = False

    for j in range(2, i):

        if i % j == 0:

            is_composite = True

            break 

    if is_composite:

        num.append(i)


print(f"Composite numbers:{num}")


#output: Composite numbers:[9, 15, 21, 25, 27, 33, 35, 39, 45, 49, 51]

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

'''

Student_marks = [[‘Raj’, 850],[‘Ram’, 999],[‘Jay’,756],[‘Radha’,923],[‘Vaidhi’,990]]

Add grace marks of 30 for each students

Now, give me the list of students with marks greater than 850

'''

student_marks = [['Raj', 850],['Ram',999],['Jay',756],['Radha',923],['Vaidhi',990]]

print(f"Initial students marks: {student_marks}")


new_marks = list(map(lambda marks : marks[1]+30 , student_marks))


print (f"Students marks after adding grace marks, 30 : {new_marks}")


f_marks = list(filter(lambda marks : marks[1]>850 , student_marks))

print (f"Students with marks more than 850: {f_marks}")


"""

OUTPUT:

Initial students marks: [['Raj', 850], ['Ram', 999], ['Jay', 756], ['Radha', 923], ['Vaidhi', 990]]

Students marks after adding grace marks, 30 : [880, 1029, 786, 953, 1020]

Students with marks more than 850: [['Ram', 999], ['Radha', 923], ['Vaidhi', 990]]

"""

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

Given a year, determine whether it is a leap year. If it is a leap year, return the Boolean True, otherwise return False.

def is_leap(year):
    leap = False
    if year % 4 == 0 and (year % 400 == 0 or year % 100 != 0):
     return True
    else
     return leap
year = int(input())
print(is_leap(year))

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

The included code stub will read an integer, , from STDIN.

Without using any string methods, try to print the following:

Note that "" represents the consecutive values in between.

Example

Print the string : 12345

if __name__ == '__main__':

    n = int(input())
for i in range(n):
    j = i+1
    print(j, end = '')
-------------------------------------------------------------------------------
You are given a string. Split the string on a " " (space) delimiter and join using a - hyphen.

def split_and_join(line):
    # write your code here
    a = line
    a = a.split(" ") #this will split the
    a = "-".join(a)
    return a
line = "this is a string"
if __name__ == '__main__':
    line = input()
    result = split_and_join(line)
    print(result)
--------------------------------------------------------------------------------
ou are given the firstname and lastname of a person on two different lines. Your task is to read them and print the following:
Hello firstname lastname! You just delved into python.

def print_full_name(first, last):
    # Write your code here
    name_f = f'{first_name} {last_name}'
    print(f"Hello {name_f}! You just delved into python.")
------------------------------------------------------------------------

Comments