Python Exercises

Python Exercises

Ex. 1: Write a function called multiply that accepts two parameters and returns the product of the two parameters.

multiple(10, 5) # 50

Ex. 2: Write a function called get_day which takes in one parameter (a number from 1-7) and returns the day of the week (1 is Sunday, 2 is Monday, etc.). If the number is less than 1 or greater than 7, the function should return None. Store all the days in a list or a dictionary and then use it in the conditional.

get_day(2) # Monday
get_day(3) # Wednesday
get_day(12) # None

Ex. 3: Write a function that takes in two parameters (both numbers). If the first numbers is greater than the second, this function returns “First number is greater” If the second number is greater than the first, the function returns “Second number is greater” Otherwise the function should return “Both the numbers are equal”.

Ex. 4: Write a function that takes a list as an argument and returns the last value in the list. It should return None if the list is empty.

Ex. 5: Write a function that takes a string and then returns the number of characters in the string, but it should not count spaces.

Ex. 6: Write a function that takes in two parameters, the first a string and second, a character. The function should return the number of times the character appears in the string. Use a for or a while loop to determine this and not a string method.

Ex. 7: Write a function that takes one string and it should return a dictionary of the number of times every letter exists in the string. For ex. if I provide a string like “Hello”, I should get an output of {"H": 1, "e": 1, "l": 2, "o": 1}

Ex. 8: Write a function that takes 4 parameters, (list, command, location, value).
I can pass a list of values as the first argument. Then I can provide “add” or “remove” as the second argument. I can pass, either “front” or “end” as the third argument and the last will be some value (could be of any data type). So if I provide something the command “add” and the location “end” then the function should take the last argument that is the value and append it to the list. If I pass, “remove” and “last”, then it should remove whatever is the last value in the list. If I provide a value as well, then it should check if that value is the last value and if it is, then remove it or else return some kind of an error message to the user. Write appropriate logic for all the other commands as well.

Ex. 9: Write a function called is_palindrome which accepts a string and checks if it is a palindrome. Your function should ignore all whitespace and case sensitivity. If the provided string is a palindrome then it should return True, otherwise False.

Ex. 10: Write a function that accepts three parameters. The first two parameters are lists and the third parameter should be either, “intersection”, “symmetric difference” or “union”. Based on the third argument, appropriate set logic should be appiled and should return the correct values but as a list and not a set.

Note: In every function you write, you should check if the argument provided is of the correct type and if not, you should return or print an error message, asking to provide the correct data.

Practical 1:

  1. Make a list. Add 10 city names to it.
    Write a for-loop to loop through everything and print them on the terminal.

2a. Make a dictionary called profile and add the following keys and it’s appropriate values:
"first_name", "last_name", "phone", "address", "age". Write print statement to print out each value on the terminal. Write 5 print statement for the 5 keys.

2b. Use a for loop to print all the keys and values in a nice formatted string. So print them like,
“My first_name is “
“My last_name is “
and so on….

3a. Write all these functions
add(num1, num2)
subtract(num1, num2)
divide(num1, num2)
multiply(num1, num2)
remainder(num1, num2)
exponent(num1, num2)

3b. Write a function called math() which accepts 2 variables and another function from the above.
math(num1, num2, fn)

3c. Convert the add and subtract function to accept unlimited arguments.

  1. Make a list of country names and use all these below list function to do it’s approprite task. Use the print function during or after each task, so that the output can be seen in the terminal.

[takes one arg] – Add an item to the end of a list.

[takes one arg, a list] – Add several items to the end of a list at the same time.

[takes two args, an index number and the item] Insert an item at a given index position

[takes no args] Remove all items from a list

[takes one optional arg, index no.] Remove the items at a given position

[takes one arg] Removes the first occurrence of the item we provide

[takes one arg] Returns the index of the specified item in the list

[takes one arg] Receives a value we provide and returns the numberof times it appears in the list.

[takes one arg] Reverse the elements of the list

[takes one arg] Sort the items of the list

  1. Make the following
    a. A list of 10 city names
    b. Convert the list to a tuple
    c. Use slicing (list_name[start:end:step]) to slice the first 3 cities and save that to a new variable.
    d. Use slicing to take the last 5 cities and save to another variable.
    e. Use slicing to only take alternate cities and save them to another variable.
    f. Now make a new string, using the .join() string method to make a new string from all new list made in above question.
  2. Create a list of numbers starting from 1 to 40. Use the range() function to generate the list using a list comprehension. Save the result of the comprehension to a variable.
  3. Create a list of numbers starting from 1 to 100. But only odd (alternate) numbers should be present. Ex. [1,3,5,7,9…,99]. Use a for loop with a range function to generate the numbers and save it to some list. Create the list earlier and then append all the values one by one inside the for loop to that list.

    Now use a while loop to print all those numbers from the list to the terminal.
  1. Write a list of random numbers. Use all the below built-in python methods to do their respective functions.

– use a list comprehension to use this function

While Loop Exercises

  1. Use a while loop to generate numbers between 1 to 100. Only even numbers should be generated.
  2. Use a while loop to generate random numbers between 1 and 100 and print them on the screen. The loop should keep generating numbers and printing till it does not generate the number 50. If 50 is generated, it should break out of the loop.
  3. Use a while loop to the following lines below. You can print any symbol you want but it should be printed as a triangle, each line should print one more symbol than the previous line. There are a couple ways of doing this, one is simply by printing the symbol with the index. Second you can use nested while loops to achieve it. Figure out and implement both the methods.