Docsity

E010 Python Guide Questions with Answers, Exams of Database Programming

Toggle sidebar
bg1
1 / 27
E010 Python Guide Questions with Answers
1.
1. enter code with "Enter x" as prompt
then print out "x doubled is:" and display the answer:
1. print("Enter
x:")
x = int(input())
#x must be an integer to be able to multiply
print("x doubled is:", (2 * x))
2.
2. now add a y integer, then find the sum of y and x with the text "The
sum
of x and y is:": 2. print("Enter x:")
x
=
int(input())
print("x doubled is:", (2 * x))
print("Enter y:")
y = int(input())
print("The
sum
of
x
and
y
is:",
(x
+
y))
3. 3. Make the output display "Python programming can be useful: 3.
print("Python
programming can be useful")
4. 4. Why are Python frameworks like Flask and Django valuable in web
devel-
opment?:
4. They provide tools for handling requests and databases.
5. 5. What is the command used in Command Prompt to verify that Python
was
installed correctly?: 5. Python --version
6.
6.
Mac
Python
installation
verification?:
6. Python3 --
version
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download E010 Python Guide Questions with Answers and more Exams Database Programming in PDF only on Docsity!

1 / 27

E010 Python Guide Questions with Answers

1. 1. enter code with "Enter x" as prompt

then print out "x doubled is:" and display the answer: 1. print("Enter x:") x = int(input()) #x must be an integer to be able to multiply print("x doubled is:", (2 * x))

2. 2. now add a y integer, then find the sum of y and x with the text "The

sum of x and y is:": 2. print("Enter x:") x = int(input()) print("x doubled is:", (2 * x)) print("Enter y:") y = int(input()) print("The sum of x and y is:", (x + y))

3. 3. Make the output display "Python programming can be useful: 3.

print("Python programming can be useful")

4. 4. Why are Python frameworks like Flask and Django valuable in web

devel-opment?: 4. They provide tools for handling requests and databases.

5. 5. What is the command used in Command Prompt to verify that Python

was installed correctly?: 5. Python --version

6. 6. Mac Python installation verification?: 6. Python3 --version

2 / 27

7. 7. Which Python data structure prevents duplicate values from being

stored?: 7. Set

8. 8. If you want to get last 3 which slicing method works: 8. Filename[-3:]

9. 9. Complete the function add_item(numeric_list, new_number) that takes

a list of numbers and a new number, and returns a new list that appends the new number to the end of the list. For example, add_item([1, 2, 3], 4) should return [1, 2, 3, 4]. def add_item(numeric_list, new_number):

TODO: Add new_number to the end of the list and return the list

Example: add_item([1, 2, 3], 4) should return [1, 2, 3, 4]

pass: 9. def add_item(numeric_list, new_number): numeric_list.append(new_number) return numeric_list

10. 10. Complete the function update_grade(grades, student, new_grade)

that takes a grades dictionary, a student name, and a new grade, then updates that student's grade and returns the dictionary. For example, update_grade({"Alice": 85, "Bob": 90}, "Alice", 95) should return {"Alice": 95, "Bob": 90}. def update_grade(grades, student, new_grade):

TODO: Update the student's grade and return the dictionary

Example: update_grade({"Alice": 85, "Bob": 90}, "Alice", 95)

4 / 27 def is_positive(number):

TODO: Return True if number > 0, False otherwise

pass: 13. def is_positive(number): return number > 0

14. 14. Write a complete function calculate_discount(price,

discount_percent) that calculates and returns the final price after applying a discount percent-age. For example, calculate_discount(75, 20) should return 60.0. def calculate_discount(price, discount_percent):

TODO: Calculate and return the final price after discount

pass: 14. def calculate_discount(price, discount_percent): return price * (1 - discount_percent / 100)

15. 15. Write a complete function password_strength(password) that

returns "Strong" if the password is at least 8 characters long and contains both letters and numbers, "Weak" otherwise. For example, password_strength("abc123def") should return "Strong". def password_strength(password):

TODO: Return "Strong" or "Weak" based on password criteria

if len(password) < 8: return "Weak" has_letter = False

5 / 27 has_number = False for char in password: if char.isalpha(): has_letter = True elif char.isdigit(): has_number = True

TODO: Add your return logic here based on has_letter and has_number

pass: 15. def password_strength(password): if len(password) < 8: return "Weak" has_letter = False has_number = False for char in password: if char.isalpha(): has_letter = True elif char.isdigit(): has_number = True if has_letter and has_number: return "Strong" else: return "Weak"

7 / 27 else: return False

8 / 27

19. 19. Create a program that asks the user "What is your name? " and

"How old are you? " Then repeats back "Hello, " username, and then prints "Next year, you will be " use variables user_name, user_age, and new_age: 19. user_name = input("What is your name? ") user_age = input("How old are you? ") new_age = int(user_age) + 1 print("Hello, ", user_name) print("Next year, you will be ", new_age)

20. 20. A Python program produces the following sequence of

numbers: 3 6 9 12 Write a Python statement that creates this sequence using the range() func- tion and assigns it to a variable named numbers.: 20. numbers = range(3, 15, 3)

21. 21. What symbol gives the remainder of a division?: 21. %

22. 22. How to divide but remove the decimal after?: 22. //

23. 23. Create a message variable and then make sure that it prints

Go 4 times: 23. message = "Go" * 4 print(message)

24. 24. A network monitoring script keeps track of basic information

about devices on a network. One of the devices is a router, and its IP

10 / 27

30. 30. Print three things based off of this: code = "X9Z7", include

variable declaration Check if the first character is X and the last character is 7 Check if the first character is A or B Check that the second character is not 5: 30. code = "X9Z7" print(code[0] == "X" and code[-1] == "7") print(code[0] == "A" or code[0] == "B") print(not code[1] == "5")

31. 31. Make this upper and lower case: word = "Hello". print them both:

  1. word = "Hello" print(word.lower()) print(word.upper())

32. 32. Make this upper and lower case: word = phrase = "I like PyThOn": 32.

phrase = "I like PyThOn" print(phrase.lower()) print(phrase.upper())

33. 33. Make this upper and lower case: user_input = "Yes": 33. user_input

= "Yes" print(user_input == "yes")

11 / 27 print(user_input.lower() == "yes")

34. 34. Do a check on the following access

code: access_code = "dvc9" The rules are as follows: The code must start with the letter D. The code must end with the character 9. Letter case should not matter. Hints: Convert the code to uppercase so case does not matter Check the first and last characters using string indexes and logical operators Combine both checks using a logical operator Display the results: 34. access_code = "dvc9" access_code_upper = access_code.upper() starts_with_d = access_code_upper[0] == "D" ends_with_9 = access_code_upper[-1] == "9" is_valid_code = starts_with_d and ends_with_ print("Access code entered:", access_code) print("Access code is valid:", is_valid_code)

35. 35. Write a Python program that:

Stores Python and python as strings. (code_1 and code_2) Compares the two strings to see if they are equal. Converts both strings to lowercase and compares them again.

13 / 27 = "Python" reversed_text = text[::-1] print(reversed_text)

41. 41. message = "The sky

is blue" check if sky is in message check if rain is in message check if character "e" is present check user input in word variable: 41. message = "The sky is blue" print("sky" in message) print("rain" in message) print("e" in message) word = input("Type a word: ") print(word in message)

42. 42. What will this output? sentence = "Learning Python

is fun" position = sentence.find("Python") print(position): 42. 9

43. 43.What does the find method return when the searched text does

not appear within a string?: 43. -

44. 44. Write a Python program that:

Stores the sentence Learning Python is fun in a string variable. Checks whether the specific words Python, Learning, and fun appear in the sentence.

14 / 27 Prints the results of the searches. store Learning in bananaphone, Python in bananaphone_2 and fun in bananaphone_500. Print 3 variables afterwards: 44. sentence = "Learning Python is fun" bananaphone = sentence.find("Learning") bananaphone_2 = sentence.find("Python") bananaphone_500 = sentence.find("fun") print(bananaphone, bananaphone_2, bananaphone_500)

45. 45. What will this

become? text = "one two three" parts = text.split() print(parts): 45. ['one', 'two', 'three']

46. 46. What will this

become? data = "a,b,c" items = data.split(",") # Splits on commas print(items): 46. ['a', 'b', 'c']

47. 47. Join the words with spaces words = ["System", "update",

"complete"]: 47. words = ["System", "update", "complete"] message = " ".join(words) print(message)

16 / 27

55. 55. A script stores a response in a variable named response. Print True if

the response is yes regardless of letter case; otherwise print False.: 55. response = "Yes" print(response.lower() == "yes")

56. 56. A code is valid only if it starts with A and ends with 9. Given

code = "A7B9", indicate whether it is valid by printing True: 56. code = "A7B9" print(code[0] == "A" and code[-1] == "9")

57. 57. Given data = "red,blue,green", split it into a list using commas,

then join the items into one string separated by spaces, and print the final result.: 57. data = "red,blue,green" parts = data.split(",") parts = " ".join(parts) print(parts)

58. 58. Create a list of fruits, then a variable called count that checks the

length of the list. print the result: 58. fruits = ["apple", "banana", "orange", "grape"] count = len(fruits) print(count)

59. 59. fruits = ["apple", "banana", "orange",

"grape"] print(len(fruits) - 1): 59. 3

60. 60. reate a list named scores that contains three items: 88, 91, 93.

Print the second item in the list (91).: 60. scores = ["88", "91", "93"]

17 / 27 print(scores[1])

61. 61. How to pop out item from a variable? use list name items and

removed as popped variable. pop the second item out of the items list: 61. removed = items.pop(1)

62. 62. Given the list, write the Python statement that removes the

second item from the list and stores it in a variable named removed_item. colors = ["red", "blue", "green", "yellow"]: 62. removed_item = colors.pop(1)

63. 63. Given the list, write the Python statement that removes the first

occur-rence of cat from the list. animals = ["dog", "cat", "bird", "cat"]: 63. animals.remove("cat")

64. 64. tasks = ['email',

'meeting'] tasks.append("report") print(tasks): 64. How to append to a list? use "report" and append to tasks = ['email', 'meeting,]. print results

65. 65. A list named files stores three file names: "report.txt",

"data.csv", and "image.png". Remove the last file using pop(), store it in a variable called removed_file, and then print the removed file and the updated list.: 65. files = ["report.txt", "data.csv", "image.png"] removed_file = files.pop() print(removed_file) print(files)

66. 66. A list called names stores the names Alice, Bob, and Charlie. Find

19 / 27 print(fruit)

69. 69. A store gives a $5 voucher when a customer has at least 50

points. If eligible, print Voucher applied and subtract 50 points. Always print the final points.: 69. points = 665 while points > 50: points = points - 50 print("Voucher applied") print("Final points:", points)

70. 70. A system has a stored password. If entered_password matches

stored_password, print Access granted. Otherwise print Access denied.: 70. stored_password = "examplegarbage" entered_password = "examplegarbage" if stored_password == entered_password: print("Access granted") else: print("Access denied")

71. 71. Starting from countdown = 3, print Counting down: X while the

value is greater than 0. After the countdown, print Go!.: 71. countdown = 3 while countdown >= 1: print("Counting down:", countdown) countdown = countdown - 1

20 / 27 print("Go!")

72. 72. A list of scores is given. Print the first score that is below 60. If

none are below 60, nothing should print. scores = [88, 73, 59, 91, 45] include "First failing score:" in print result.: 72. scores = [88, 73, 59, 91, 45] for score in scores: if score < 60: print("First failing score:", score) break

73. 73. Create a function called greet that prints "Hello!". call function:

  1. def greet(): print("Hello!") greet()

74. 74. Create a function named show_message that prints

"Welcome to Python": 74. def show_message(): print("Welcome to Python") show_message()

75. 75. Write the correct Python statement to begin defining a user-

defined function named display_message. Only write the first line that starts the function definition.: 75. def display_message():

76. 76. call the greet function without any arguments: 76. greet()