Beginners

Question 1: Given a two integer numbers return their product and if the product is greater than 1000, then return their sum

  1. number1 = 20
  2. number2 = 30
  3. The result is 600
  4. number1 = 40
  5. number2 = 30
  6. The result is 70


Question 2: Given a range of first 10 numbers, Iterate from start number to the end number, print the sum of the current number and previous number.**

  1. Printing current and previous number sum in a given range(10)
  2. Current Number 0 Previous Number 0 Sum: 0
  3. Current Number 1 Previous Number 0 Sum: 1
  4. Current Number 2 Previous Number 1 Sum: 3
  5. Current Number 3 Previous Number 2 Sum: 5
  6. Current Number 4 Previous Number 3 Sum: 7
  7. Current Number 5 Previous Number 4 Sum: 9
  8. Current Number 6 Previous Number 5 Sum: 11
  9. Current Number 7 Previous Number 6 Sum: 13
  10. Current Number 8 Previous Number 7 Sum: 15
  11. Current Number 9 Previous Number 8 Sum: 17

Question 3: Given a string, display only those characters which are present at an even index number. For example str = “pynative”, so you should display ‘p’, ‘n’, ‘t’, ‘v’.

  1. Orginal String is: pynative
  2. Printing only even index chars
  3. p
  4. n
  5. t
  6. v

Question 4: Given a string and an integer number n, remove characters from a string starting from zero up to n and return a new string
**
For example, removeChars("pynative", 4) so output must be tive.

Note: n must be less than the length of the string.


Question 5: Given a list of numbers, return True if first and last number of a list is same**

  1. Given list is [10, 20, 30, 40, 10]
  2. result is True
  3. Given list is [10, 20, 30, 40, 50]
  4. result is False

Question 6: Given a list of numbers, Iterate it and print only those numbers which are divisible of 5

  1. Given list is [10, 20, 33, 46, 55]
  2. Divisible of 5 in the list:
  3. 10
  4. 20
  5. 55


Question 7: Return the total count of sub-string “Emma” appears in the given string

Given string is “Emma is good developer. Emma is a writer”

  1. Emma appeared 2 times

Question 8: Print the following pattern

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

Question 9: Reverse a given number and return true if it is the same as the original number

  1. original number 121
  2. The original and reverse number is the same
  3. original number 125
  4. The original and reverse number is not same

Question 10: Given a two list of numbers create a new list such that new list should contain only odd numbers from the first list and even numbers from the second list

  1. First List [10, 20, 23, 11, 17]
  2. Second List [13, 43, 24, 36, 12]
  3. result List is [23, 11, 17, 24, 36, 12]

Question 11: Write a code to extract each digit from an integer, in the reverse order
For example, if the given int is 7536, the output shall be “6 3 5 7”, with a space separating the digits.

Question 12: Calculate income tax for the given income by adhering to the below rules

Taxable Income Rate (%)
First $10,000 0
Next $10,000 10
The remaining 20

For example, suppose that the taxable income is $45000, then the income tax payable is:

$100000% + $1000010% + $2500020% = $6000
*

Question 13: Print multiplication table form 1 to 10

  1. 1 2 3 4 5 6 7 8 9 10
  2. 2 4 6 8 10 12 14 16 18 20
  3. 3 6 9 12 15 18 21 24 27 30
  4. 4 8 12 16 20 24 28 32 36 40
  5. 5 10 15 20 25 30 35 40 45 50
  6. 6 12 18 24 30 36 42 48 54 60
  7. 7 14 21 28 35 42 49 56 63 70
  8. 8 16 24 32 40 48 56 64 72 80
  9. 9 18 27 36 45 54 63 72 81 90
  10. 10 20 30 40 50 60 70 80 90 100

Question 14: Print downward Half-Pyramid Pattern with Star (asterisk)

  1. * * * * *
  2. * * * *
  3. * * *
  4. * *
  5. *

Question 15: Write a function called exponent(base, exp) that returns an int value of base raises to the power of exp.

Note here exp is a non-negative integer, and the base is an integer.

  1. ====================Case 1====================
  2. base = 2
  3. exponent = 5
  4. 2 raises to the power of 5 is: 32 i.e. (2 *2 * 2 *2 *2 = 32)
  5. ====================Case 2====================
  6. base = 5
  7. exponent = 4
  8. 5 raises to the power of 4 is: 625 i.e. (5 *5 * 5 *5 = 625)

Input and Output

Question 1: Accept two numbers from the user and calculate multiplication

Question 2: Display “My Name Is James” as “MyNameIsJames” using output formatting of a print() function**

Use print() statement formatting to display ** separator between each word.

For example: print(‘My’, ‘Name’, ‘Is’, ‘James’) will display MyNameIsJames

So use one of the formatting argument of print() to turn the output into MyNameIs**James

Question 3: Convert decimal number to octal using print() output formatting

Display decimal number 8 as 10

Question 4: Display float number with 2 decimal places using print()

Display 458.541315 as 458.54

Question 5: Accept list of 5 float numbers as a input from user

  1. //Numbers can be any
  2. [78.6, 78.6, 85.3, 1.2, 3.5]

Question 6: write all file content into new file by skiping line 5 from following file

test.txt file:

  1. line1
  2. line2
  3. line3
  4. line4
  5. line5
  6. line6
  7. line7

newFile.txt should be

  1. line1
  2. line2
  3. line3
  4. line4
  5. line6
  6. line7

Question 7: Accept any three string from one input() call

Question 8: You have the following data.

  1. totalMoney = 1000
  2. quantity = 3
  3. price = 450

display above data using string.format() method

  1. I have 1000 dollars so I can buy 3 football for 450.00 dollars.

Question 9: How to check file is empty or not

Question 10: Read line number 4 from the following file

test.txt file:

  1. line1
  2. line2
  3. line3
  4. line4
  5. line5
  6. line6
  7. line7

Loop

Question 1: Print First 10 natural numbers using while loop

  1. 0
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. 10

Question 2: Print the following pattern

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

Question 3: Accept number from user and calculate the sum of all number between 1 and given number

For example user given 10 so the output should be 55

Question 4: Print multiplication table of given number

For example num = 2 so the output should be

  1. 2
  2. 4
  3. 6
  4. 8
  5. 10
  6. 12
  7. 14
  8. 16
  9. 18
  10. 20

Question 5: Given a list iterate it and display numbers which are divisible by 5 and if you find number greater than 150 stop the loop iteration

  1. list1 = [12, 15, 32, 42, 55, 75, 122, 132, 150, 180, 200]
  2. 15
  3. 55
  4. 75
  5. 150

Question 6: Given a number count the total number of digits in a number

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

Question 7: Print the following pattern using for loop

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

Question 8: Reverse the following list using for loop

  1. list1 = [10, 20, 30, 40, 50]
  2. 50
  3. 40
  4. 30
  5. 20
  6. 10

Question 9: Display -10 to -1 using for loop

  1. -10
  2. -9
  3. -8
  4. -7
  5. -6
  6. -5
  7. -4
  8. -3
  9. -2
  10. -1

Question 10: Display a message “Done” after successful execution of for loop

For example, the following loop will execute without any error.

  1. for i in range(5):
  2. print(i)

So the Expected output should be:

  1. 0
  2. 1
  3. 2
  4. 3
  5. 4
  6. Done!

Functions

String

Data Structure

List

Dictionary

Set

tuple

JSON

Random Data Generation

NumPy

Pandas

Matplotlib

Database

Question 1: Connect to your database server and print its version

Write SQL query to get the database server version.
Connect to the database and use cursor.execute() to execute this query.
Next, use cursor.fetchone() to fetch the record.

  1. import sqlite3
  2. def get_connection():
  3. connection = sqlite3.connect('python_db.db')
  4. return connection
  5. def close_connection(connection):
  6. if connection:
  7. connection.close()
  8. def read_database_version():
  9. try:
  10. connection = get_connection()
  11. cursor = connection.cursor()
  12. cursor.execute("select sqlite_version();")
  13. db_version = cursor.fetchone()
  14. print("You are connected to SQLite version: ", db_version)
  15. close_connection(connection)
  16. except (Exception, sqlite3.Error) as error:
  17. print("Error while getting data", error)
  18. print("Question 1: Print Database version")
  19. read_database_version()

Question 2: Fetch Hospital and Doctor Information using hospital Id and doctor Id

Implement the functionality to read the details of a given doctor from the doctor table and Hospital from the hospital table. i.e., read records from Hospital and Doctor Table as per given hospital Id and Doctor Id.

  1. import sqlite3
  2. def get_connection():
  3. connection = sqlite3.connect('python_db.db')
  4. return connection
  5. def close_connection(connection):
  6. if connection:
  7. connection.close()
  8. def get_hospital_detail(hospital_id):
  9. try:
  10. connection = get_connection()
  11. cursor = connection.cursor()
  12. select_query = """select * from Hospital where Hospital_Id = ?"""
  13. cursor.execute(select_query, (hospital_id,))
  14. records = cursor.fetchall()
  15. print("Printing Hospital record")
  16. for row in records:
  17. print("Hospital Id:", row[0], )
  18. print("Hospital Name:", row[1])
  19. print("Bed Count:", row[2])
  20. close_connection(connection)
  21. except (Exception, sqlite3.Error) as error:
  22. print("Error while getting data", error)
  23. def get_doctor_detail(doctor_id):
  24. try:
  25. connection = get_connection()
  26. cursor = connection.cursor()
  27. select_query = """select * from Doctor where Doctor_Id = ?"""
  28. cursor.execute(select_query, (doctor_id,))
  29. records = cursor.fetchall()
  30. print("Printing Doctor record")
  31. for row in records:
  32. print("Doctor Id:", row[0])
  33. print("Doctor Name:", row[1])
  34. print("Hospital Id:", row[2])
  35. print("Joining Date:", row[3])
  36. print("Specialty:", row[4])
  37. print("Salary:", row[5])
  38. print("Experience:", row[6])
  39. close_connection(connection)
  40. except (Exception, sqlite3.Error) as error:
  41. print("Error while getting data", error)
  42. print("Question 2: Read given hospital and doctor details \n")
  43. get_hospital_detail(2)
  44. print("\n")
  45. get_doctor_detail(105)

Questions 3: Get the list of doctors as per the given specialty and salary

Note: Fetch all doctors whos salary higher than the input amount and specialty is the same as the input specialty.

  1. import sqlite3
  2. def get_connection():
  3. connection = sqlite3.connect('python_db.db')
  4. return connection
  5. def close_connection(connection):
  6. if connection:
  7. connection.close()
  8. def get_specialist_doctors_list(speciality, salary):
  9. try:
  10. connection = get_connection()
  11. cursor = connection.cursor()
  12. sql_select_query = """select * from Doctor where Speciality = ? and Salary > ?"""
  13. cursor.execute(sql_select_query, (speciality, salary))
  14. records = cursor.fetchall()
  15. print("Printing doctors whose specialty is", speciality, "and salary greater than", salary, "\n")
  16. for row in records:
  17. print("Doctor Id: ", row[0])
  18. print("Doctor Name:", row[1])
  19. print("Hospital Id:", row[2])
  20. print("Joining Date:", row[3])
  21. print("Specialty:", row[4])
  22. print("Salary:", row[5])
  23. print("Experience:", row[6], "\n")
  24. close_connection(connection)
  25. except (Exception, sqlite3.Error) as error:
  26. print("Error while getting data", error)
  27. print("Question 3: Get Doctors as per given Speciality\n")
  28. get_specialist_doctors_list("Garnacologist", 30000)

Question 4: Get a list of doctors from a given hospital

Note: Implement the functionality to fetch all the doctors as per the given Hospital Id. You must display the hospital name of a doctor.

  1. import sqlite3
  2. def get_connection():
  3. connection = sqlite3.connect('python_db.db')
  4. return connection
  5. def close_connection(connection):
  6. if connection:
  7. connection.close()
  8. def get_hospital_name(hospital_id):
  9. # Fetch Hospital Name using Hospital id
  10. try:
  11. connection = get_connection()
  12. cursor = connection.cursor()
  13. select_query = """select * from Hospital where Hospital_Id = ?"""
  14. cursor.execute(select_query, (hospital_id,))
  15. record = cursor.fetchone()
  16. close_connection(connection)
  17. return record[1]
  18. except (Exception, sqlite3.Error) as error:
  19. print("Error while getting data", error)
  20. def get_doctors(hospital_id):
  21. # Fetch Hospital Name using Hospital id
  22. try:
  23. hospital_name = get_hospital_name(hospital_id)
  24. connection = get_connection()
  25. cursor = connection.cursor()
  26. sql_select_query = """select * from Doctor where Hospital_Id = ?"""
  27. cursor.execute(sql_select_query, (hospital_id,))
  28. records = cursor.fetchall()
  29. print("Printing Doctors of ", hospital_name, "Hospital")
  30. for row in records:
  31. print("Doctor Id:", row[0])
  32. print("Doctor Name:", row[1])
  33. print("Hospital Id:", row[2])
  34. print("Hospital Name:", hospital_name)
  35. print("Joining Date:", row[3])
  36. print("Specialty:", row[4])
  37. print("Salary:", row[5])
  38. print("Experience:", row[6], "\n")
  39. close_connection(connection)
  40. except (Exception, sqlite3.Error) as error:
  41. print("Error while getting doctor's data", error)
  42. print("Question 4: Get List of doctors of a given Hospital Id\n")
  43. get_doctors(2)

Operation 5: Update doctor experience in years

The value of the experience column for each doctor is null. Implement the functionality to update the experience of a given doctor in years.

  1. import sqlite3
  2. import datetime
  3. from dateutil.relativedelta import relativedelta
  4. def get_connection():
  5. connection = sqlite3.connect('python_db.db')
  6. return connection
  7. def close_connection(connection):
  8. if connection:
  9. connection.close()
  10. def update_doctor_experience(doctor_id):
  11. # Update Doctor Experience in Years
  12. try:
  13. # Get joining date
  14. connection = get_connection()
  15. cursor = connection.cursor()
  16. select_query = """select Joining_Date from Doctor where Doctor_Id = ?"""
  17. cursor.execute(select_query, (doctor_id,))
  18. joining_date = cursor.fetchone()
  19. # calculate Experience in years
  20. joining_date_1 = datetime.datetime.strptime(''.join(map(str, joining_date)), '%Y-%m-%d')
  21. today_date = datetime.datetime.now()
  22. experience = relativedelta(today_date, joining_date_1).years
  23. # Update doctor's Experience now
  24. connection = get_connection()
  25. cursor = connection.cursor()
  26. sql_select_query = """update Doctor set Experience = ? where Doctor_Id = ?"""
  27. cursor.execute(sql_select_query, (experience, doctor_id))
  28. connection.commit()
  29. print("Doctor Id:", doctor_id, " Experience updated to ", experience, " years")
  30. close_connection(connection)
  31. except (Exception, sqlite3.Error) as error:
  32. print("Error while getting doctor's data", error)
  33. print("Question 5: Calculate and Update experience of all doctors \n")
  34. update_doctor_experience(101)