Problems

Your one stop guide to polish your Python skills.

Green means easy. Orange means challenging. Red is a bit difficult. The difficulty of these questions are with reference to the course taught in class and takes into account the average performance of students in assignments and quizzes. Some might feel the reds == easy for them while some students might feel orange == difficult.

CHAPTER 1: BASICS

  • Enter an input from user asking their age and name. Print it Note: You may save it in a variable or just print it directly.
  • Perform the following tasks:
    • Take an input(a decimal/float number) from the user, save it in a variable (let’s say r2d2). Do not type cast yet.
    • Print the type of variable r2d2 (without any conversion or type casting)
    • Convert/type cast it to float and reprint its data type.
  • Print the following line using a single print statement. Hint: use escape operator
    • “Hello Mr. O’Brien, It’s a pleasure to meet you”, said the manager.
  • Take three integer input from the user. These input are the scores they received in a test(out of 20). Print the total scores, average and names of the subjects in a single line using three different print statements. Hint: How do you stitch multiple prints?
  • Take age from the user and save it in a variable. Print the number of years required for that user to reach the age of 50.
  • Take two numbers from the user. Perform an operation involving two arithmetic operations. It can be any arithmetic operation. DO NOT use logical or comparator operators.

CHAPTER 2: CONDITIONS

  1. Take length and breadth of a rectangle and check if it is a square or not.
  2. Take age as user input from 3 users. Find the youngest and oldest among them.
  3. Ask user for a year and find if it is a leap year or not.
  4. Write a program to accept cost price of a car and display road tax to be paid according to following table.
    1. Price of the car and equivalent road tax to be paid on brand-new cars ​
      Price of the car Road tax to be paid
      < $50,000 5%
      $50,000 – $99,999 10%
      $100,000 – 149,999 15%
      >150,000 20%
      Price of the car and equivalent road tax to be paid on brand-new cars

  5. Hey! It’s that time of the year. Warm coffee, Gloomy weather, and filling taxes…..wait?WHAT!!
    In this exercise, you will not only learn to condition but also to calculate taxes.
    Imagine user being a single filer for 2023. The tax rate by IRS can be found here. In total, there are 7 tax brackets, we will deal with 5 of them as shown in table below.<br>
    * Take an input salary from the user.
    * Based on the salary, calculate the tax amount the user owns to the IRS.

    Use a series of if elif statements to achieve the result
    Salary Tax
    < $11,000 10%
    $11,000 – $44,725 $1,100 + 12% of difference
    $44,725 – $95,375 $5,147 + 22% of the difference
    $95,375 – $182,100 $16,290 + 24% of the difference
    $182,100 – $231,250 $37,104 + 32% of the difference
    >$231,250 Contact our tax experts

6. Ask user for their age, gender(M or F), and marital status(Y or N). Based on the following table, print the appropriate message.

  • If gender is female, “urban areas”
  • If gender is male and age is between 20 and 40, “anywhere”
  • If gender is male and age is between 40 and 60, “urban areas”
  • Other input, “Error”

CHAPTER 3: LOOPS

1. Take a number from a user. Display number -1 to -n using a loop.

2. Take a number n from the user. Calculate the sum of all numbers from 1 to n. Eg: if user enters, 5, it should show 15 because 1+2+3+4+5 = 15.
Similarly if user enters 7, it must show 28 because 1+2+3+4+5+6+7 = 28.
 
3. Print the following pattern
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1

4. Write a Python program that prints all the numbers from 0 to 19(including them both) except 3 and 6.

5. Take a number from the user and find factorial of that given number. This is how to calculate a factorial .
5!(read as 5 factorial) = 5 X 4 X 3 X 2 X 1 = 120
similarly factorial of 7 = 7! = 7 X 6 X 5 X 4 X 3 X 2 X 1 = 5040.
 
6. Take a number n from the user. Print the perfect squares of number starting with 1 until n.
Perfect square is square of a number. In other words, when a number is multiplied by itself
For example, if user enter 6, the output must be squares of numbers between 1 and 6 (including 6)<br>
1 4 9 16 25 36
 
7. Write a Python program which iterates the integers from 1 to 50. For multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
 
8. Write a program to print multiplication table of a given number
 
9. Write a program to count the total number of digits in a number using a while loop.

For example, the number is 75869, so the output should be 5.

10. Write a program to display all prime numbers within a range

Note: A Prime Number is a number that cannot be made by multiplying other whole numbers. A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers

Examples:

  • 12 is not a prime number because it can be made by 3×4 = 12
  • 37 is a prime number because no other whole numbers multiply together to make it.

11. Write a program to print the cube of all numbers from 1 to a given number.

If input is 6 print 1 8 27 64 125 216

12. Take a number from the user and reverse that integer number

Given: 587249

Expected output: 942785

13. Print the following pattern

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
14. Write a program that takes a text from the user and finds the number of time a vowel has occurred in that text

CHAPTER 4: FUNCTION AND SCOPE RULES

1. Write a function to find power of a number but without using arithmetic operator

Take two input from a user base and exponent. Multiply the base with itself. You will need to use a loop.
For eg: if number = 7 and power = 4, you need to perform 7*7*7*7 = 2401.
DO NOT use (**) operator
 
2. Create a function that takes radius(r) of a circle and returns the area and parameter of a circle. The catch here is to use only ONE function and return both area and perimeter in a single return. Hint: Try to find, how to return multiple values without a list, tuple, dictionary or set. Check out how to define multiple variables in a single line.3. Take length and breadth of a rectangle and write a function that returns if the rectangle is square or not.
4. Take a string from a user and write a function to check if it is a palindrome or not. A palindrome is a word, phrase, or sequence that reads the same backward as forward.
Hint: Check how to reverse a string. It resembles like parameters of range where the start and stop are empty and the step is decrementing by 1.
5. This problem is related to scope rule. You need a global variable called function_called. Each time a function is called, you need to change the value of this global variable from within this function. Take an input from a user, if user inputs “Y”, call this function and print the value of function called. If the user inputs anything except “Y”, break the loop. Use an infinite loop like while(True) to keep your program running.
6. Write a function that inputs a number and prints the multiplication table of that number.
7. Write a program to print twin primes less than 1000. If two consecutive odd numbers are both prime then they are known as twin primes
8. Write a program to find out the prime factors of a number. Example: prime factors of 56 – 2, 2, 2, 7
9. Write a program to implement these formulae of permutations and combinations.
Number of permutations of n objects taken r at a time: p(n, r) = n! / (n-r)!.
Number of combinations of n objects taken r at a time is: c(n, r) = n! / (r!*(n-r)!) = p(n,r) / r!
10. Write a function prodDigits() that inputs a number and returns the product of digits of that number.

CHAPTER 5: LISTS

1. Perform the following functions(after each operation, print the list):

  • Create an empty list
  • Add 8 *random* numbers in that list(use random generator) and a loop and print it
  • Remove the last element from the list. You must have 7 elements in this list, print the list and its length.
  • Insert an element in fourth position in this list and now you have 8 elements. Print the list.
  • Remove the first and seventh element from the list and now you have 6 elements. Print these final 6 elements.
2. Write a **function** that returns the elements on even positions in a list. <br>For eg: if list has [2,5,4,30,51,79,72,66,43,29] it must return [5,30,79,66,29]
3. Write a function that merges two sepeate lists into a new *sorted* list. [10,74,56],[22,93,35] → [10,22,35,56,74,93].
4. NASA has landed a rover named Perseverance on Mars. Check out some amazing pictures for wallpaper [here](https://mars.nasa.gov/mars2020/). <br>
This rover took reading of the surface temperature for seven sols(a single martian day is called sol and is 40 minutes longer than that of Earth’s day). Create surface temperature readings(use a random generator) of 7 sols and save them in a list. <br>
Print the list and find the average temperature for that week on Mars, and print it.<br>
Later in an investigation, it was found that rover had some calibration issue and last two readings were 4.7F(Fahrenheit) larger than usual. Rectify the last two readings in the list and recompute the average. Show the erroneous average, the corrected average and their difference.
5. Create a list of 35 random numbers(using a random generator) in the range -20 to +20 including them both. Print this list.<br>
Pick positive numbers in this list(0-20) and save them in another list. Find the length of this new list containing all positive elements and print it.
6. Create a list of 20 random numbers(you can enter the number manually). Pass this list to a function that returns all odd numbers in a list. Print this new list with all odd numbers.
7. Take a string from the user and save each letter/character of that string in a list using a loop and a list-based function. DO NOT manually add elements to the list. Use a loop.

CHAPTER 6: SETS AND TUPLES

1. Create a tuple of length 9 and write a Python program to get the 4th element from the last element of a tuple.

2. Write a Python program to check whether an element exists within a tuple. Add these elements to a new tuple named repeated_tuples.

3. Write a Python program to remove an item from a tuple.

Write a Python program to find the index of an item in a tuple.

Remove items from set1 that are not common to both set1 and set2

Update set1 by adding items from set2, except common items

CHAPTER 7: DICTIONARY