CODE

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

USING THE PRINT METHOD Complete the python code to take two integers as a user input and print their average. The formula average is as follows: Average = (a + b) /2 sample input: 2 2 sample output: 2.0

number1=int(input()) number2=int(input()) n=number1 + number2 avg=n/2 print(avg) The print() function prints that give an object to the standard output device or the textstream fil It prints the message to the screen or other standard output devic. It is also used to print operations.

WORKING WITH LISTS Complete the Python code that fetches the first n element from the array wher n denotes the number of elements. sample input: 1 sample output: Array: [55]

array = [55, 12, 37, 831, 57, 16, 93, 44, 22] print("Array:") n = int(input()) print(array[0:n]) In Python, arrays are known as LISTS. Lists are aggregated data types, menaing that they are composed of other data types. Lists consist of a comma-separated element, whihc is enclosed in a square bracket example [55, 12, 37] 1st Line: array is the varialbe to which several values are assigned namely, 55, 12, 37, 831, 57, 16, 93, 44, and 22. 2nd Line : print("Array:") prints Array 3rd line: takes the input from the user last line: print(array[0:n]) prints Array: [55] this will happen if the value of the user input is 1.

USING VARIABLE ASSIGNMENTS Complete the python code that will calculate the area of a square, which takes the side as the user input. FORMULA: Area of a square = side * side sample input: 7 sample output: the area of a square = 49

side = int(input()) area = side * side print ("The area of a square =", area) Variables in python can refer to values of different data types, such as strings, integers, float point values, and Booleons. In this code, two variables are used 1. side and 2. area The print() function prints the message.

USING ARITHMETIC OPERATORS Complete the Python code that takes the number of days as the user input, converts the days into a number of years and days, then prints them. sample input: 860 sample output: Years = 2 and Days = 130

days = int(input()) years = days // 365 days = days - (years * 365) print("Years: = {} and Days = {}".format (years, days)) Arithmetic operators are mathematical functions that take numberical values and perform calculations on those values. first line: takes the input from the user and is stored in the variable days next line: years=days//365 days is divided by 365 and the value is stored in the variable years. next line: days = days - ((years * 365)) the product of years and 365 is subtracted from days and the value is stored in the variable days. in the last line: print("Years = {} and Days = {}.format(years, days)) user prints the value of the variable years and days.

DISPLAYING A STATEMENT MULTIPLE TIMES Write python code that displays Hello Everyone five times in a single row. output: Hello Everyone Hello Everyone Hello Everyone Hello Everyone Hello Everyone

print("Hello Everyone " * 5) The print() function prints the specified message to the screen.

MANIPULATING STRINGS USING THE STRIP METHOD Compete the Python cod that counts and displays the number of occurences of a recurrent word, such as, afraid in a given sample input. **note** Assume that the word will not occue as a substring in other words and a comma is also not used in a sentence. sample input: I am not afraid to die. I am not afraid to live. I am not afraid to fail. I am not afraid to succeed. sample output: 4

sentence = input() query = input() sentence = sentence.lower().strip() query = query.lower().strip() num_occurrences = sentence.count(query) print(num_occurrences) To munipulate strings use the build-in Python method: strip() method The strip() method returns a copy of the string with the leading and trailing characters removed. The lower() method returns a copy of the sting with all characters converted to lowercase. First two lines: take the user input 3rd line: sentence = sentence.lower().strip() sentence is the variable to which sentence.lower().strip() is assigned 4th line: query = query.lower().strip() query is the varialbe which is assigned to query.lower().strip() 5th line: num_occurrences = sentence.count(query) num_occurrences is the variable which is assigned to sentence.count(query) Last line: print(num_occurrences) this will print num_occurences. it will reflect a number that shows the user how many times the word afraid appears in the whole sentence.

WORKING WITH STRINGS Complete the Python code that converts the last n letters of a given string to the uppercase. In this the string is converted into a integer, specifying the last n letters to convert as the input from a user where n denotes the number of elements. sample input: 3 sample output: bougainviLEA

st = "bougainvillea" a = int(input()) start = st[ :len(st) -a] end = st[len(st) -a:] print(start + end.upper()) Python has a very handy function for obtaing the user input from the command-line interface (CLI). This function is called the input() function. The upper() method converts all lowercase characters in the string into uppercase characters and returns it. The variable st is assigned the value bougainvillea second line: takes the input n from the user. next line: As per the user input, this line extracts the substring and stores it in the variable start. Similarily, the next line extracts the substring and stroes it in the variable end. last line: prints the start substring, converts the end subsrting to the uppercase using the uppercase() function and prints it.

USING VARIABLES AND ASSIGNING STATEMENTS Complete the Python code to take the user input as distance in kilometers and Time in hours And convert it into speed in kilometers per hour And miles per hour sample input: 150 2 sample output: Speed in kilometer per hour: 75.0 and miles per hour: 46.875

distance_in_km = int(input()) time_in_hours = int(input()) distance_in_mi = distance_in_km / 1.6 distance_in_mtrs = distance_in_km * 1000 speed_in_kph = distance_in_km / time_in_hours speed_in_mph = distance_in_mi / time_in_hours print("Speed in kilometers per hour:", speed_in_kph, "and miles per hour:", speed_in_mph) Variables in python can refer to values of different data types, such as strings integers Floating Points, and Booleans. In this code there are six variables 1. distance_in_km 2. time_in_hours 3. distance_in_mi 4. distance_in_mtrs 5. speed_in_kph 6. speed_in_mph The first and second line takes the input from the user FaceTime Foot is stored in a variables distance_in_km and time_in_hours In the rest of the code,users input is converted to speed in kilometers per hour and miles per hour. Finally, the print() function is used to print the value of the two variables. speed_in_kph and speed_in_mph

USING BOOLEAN OPERATORS Write the Python code to perform the follwoing task using Boolean operators: 1. Define an even number 124 and print Even. 2. Define an odd number 123 and then print Odd. Output: Even Odd

n = 124 if n % 2 == 0: print("Even") n = 123 if n % 2 == 1: print ("Odd") Boolean data type are values that can be either True or False. 1st line: n is the variable which is assigned to the value 124 2nd line is an if statment that says if the number is divisible by 2, then Even will print. 3rd line: n is the varialbe which is assigned to 123 4th line is an if statment that says if the number is NOT divisible by 2, then Odd will print.

DISPLAY THE MULTIPLICATION TABLE Complete the python code that takes a number as the user input and prints its multiples from one to 10. sample input: 10 sample output: 10 × 1 = 10 10 × 2 = 20 10 × 3 = 30 10 × 4 = 40 10 × 5 = 50 10 × 6 = 60 10 × 7 = 70 10 × 8 = 80 10 × 9 = 90 10 × 10 = 100

number = int(input()) for i in range(1, 11): print(number, 'x',i, '=', number*i) I found has a very handy function for obtaining the user keyboard input from the command line interface (CLI) called input(). This code takes a number as a user input and prints its multiple from 1 to 10 The first line takes the input from the user It's stores it in the variable number The next line, for i in range(1,11): inside the four loop, very well i is initialize with a value of one. The loop executed until the value of the variable is less than 11. After that, the condition becomes false. therefore, the first 10 multiples of the number entered by the user are printed.

PERFORMING STRING SLICING TASKS Write the Python code to perform the following string slicing tasks: 1. Print letter s from the given string Cats hate water 2. Print integer 9 from the given list [8,9,10]. 3. Print 1, 2, 3 fromt he given statement Testing 1, 2, 3 4. Print A man, a plan, a canal from the given sentence A man, a plan, a canal: Panama. Output should be s 9 math 1,2,3 A man, a plan, a canal

print("Cats hate water"[3]) print("[8, 9, 10]"[4]) print("He doesn't teach math"[17:21]) print("Testing1,2,3"[7:]) print("A man, a plan, a canal: Panama"[:-8]) Python strings are immutable: once they are assigned to a variable, their value CANNOT be changed. Using the slicing method, you can access characters within a range of indicies in a string and get a slice or substring of that string. the first line: print("Cats hate water"[3]) prints s, because the indexing of the string starts with 0 (zero) and the character on the thirs index is s. next line: print("[8, 9, 10]"[4]) prints 9, becuase the indexing of the string starts with 0 (zero) and the character on the fourth index is 9. next line: print("He doesn't teach math"[17:21]) prints math, because 17:21 will print characters from the 17 to the 20 position. next line: print("Testing1,2,3"[7:]) prints 1,2,3 becuase 7: will print characters from the 7th index to the last index. The last line: print("A man, a plan, a canal:Panama"[:-8]) prints A man, a plan, a canal, because -8 will remove the last 8 characters from the string.


Set pelajaran terkait

Chapter 13. Efficient Capital Markets and Behavioral Challenges

View Set

Chapter 32: Care of Critically Ill Patients with Respiratory Problems

View Set

Chapter 15: Intraoperative nursing management

View Set

Growth and Development of the School-age Child!

View Set

Financial Literacy Semester Test

View Set