



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















1 / 27
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))
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))
print("Python programming can be useful")
devel-opment?: 4. They provide tools for handling requests and databases.
was installed correctly?: 5. Python --version
2 / 27
stored?: 7. Set
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):
pass: 9. def add_item(numeric_list, new_number): numeric_list.append(new_number) return numeric_list
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):
4 / 27 def is_positive(number):
pass: 13. def is_positive(number): return number > 0
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):
pass: 14. def calculate_discount(price, discount_percent): return price * (1 - discount_percent / 100)
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):
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
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
"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)
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)
Go 4 times: 23. message = "Go" * 4 print(message)
about devices on a network. One of the devices is a router, and its IP
10 / 27
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")
phrase = "I like PyThOn" print(phrase.lower()) print(phrase.upper())
= "Yes" print(user_input == "yes")
11 / 27 print(user_input.lower() == "yes")
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)
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)
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)
is fun" position = sentence.find("Python") print(position): 42. 9
not appear within a string?: 43. -
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)
become? text = "one two three" parts = text.split() print(parts): 45. ['one', 'two', 'three']
become? data = "a,b,c" items = data.split(",") # Splits on commas print(items): 46. ['a', 'b', 'c']
"complete"]: 47. words = ["System", "update", "complete"] message = " ".join(words) print(message)
16 / 27
the response is yes regardless of letter case; otherwise print False.: 55. response = "Yes" print(response.lower() == "yes")
code = "A7B9", indicate whether it is valid by printing True: 56. code = "A7B9" print(code[0] == "A" and code[-1] == "9")
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)
length of the list. print the result: 58. fruits = ["apple", "banana", "orange", "grape"] count = len(fruits) print(count)
"grape"] print(len(fruits) - 1): 59. 3
Print the second item in the list (91).: 60. scores = ["88", "91", "93"]
17 / 27 print(scores[1])
removed as popped variable. pop the second item out of the items list: 61. removed = items.pop(1)
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)
occur-rence of cat from the list. animals = ["dog", "cat", "bird", "cat"]: 63. animals.remove("cat")
'meeting'] tasks.append("report") print(tasks): 64. How to append to a list? use "report" and append to tasks = ['email', 'meeting,]. print results
"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)
19 / 27 print(fruit)
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)
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")
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!")
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
"Welcome to Python": 74. def show_message(): print("Welcome to Python") show_message()
defined function named display_message. Only write the first line that starts the function definition.: 75. def display_message():