Whsmrsb.weebly.com



Learning Python ExercisesDefining Variables, User Input, & Variable Types (Chapter 2)Exercise 1: Create a variable and assign it the value of 5. Create another variable and assign it the value of 30. Display the two variables onto the console. EXAMPLE CONSOLE:1020Exercise 2: Create a variable and give it a string data type. Create another variable and give it a float data type. Display the two variables onto the console. EXAMPLE CONSOLE (yours MUST be different than mine!):Hello World!20.75Exercise 3: Type the program below to see what gets displayed and explain what happened. userName = input(“Hi! Type in your name: ”)print(userName + “, that’s a great name!”)Exercise 4: Write a program that prompts the user for their age. Display the results with the sentence: “You said you are ____ years old!” with the blank being their input. (MAKE SURE TO CHANGE THE NUMBER INTO A STRING DATA TYPE!)EXAMPLE CONSOLE (yours MUST be different than mine!):How old are you? 30You said you are 30 years old!Exercise 5: Write a program to ask the user for the number of hours they worked in a week and their hourly rate to calculate their pay for the week before taxes. EXAMPLE CONSOLE (yours MUST be different than mine!):Enter how many hours you worked this week: 35Enter your hourly rate: 6.25Your pay is $218.75Exercise 6: Write a program which prompts the user for a Celsius temperature and then tell the user the converted temperature in Fahrenheit.b) Show the results if the temperature in Celsius is 10? -15?Conditionals: If, Elif, and Else (Chapter 3)Exercise 1: Rewrite your pay computation to give the employee 1.5 times their hourly rate for hours they worked above 40 hours.EXAMPLE OUTPUT (yours MUST be different than mine!):Enter how many hours you worked this week: 45Enter your hourly rate: 8.5Your pay is $403.75Exercise 2: Rewrite your pay program using?try?and?except?so that your program handles non-numeric input gracefully by printing a message and stopping the program without crashing. EXAMPLE OUTPUT (yours MUST be different than mine!):Enter how many hours you worked this week: 20Enter your hourly rate: nineINVALID ENTRY! Try again!Enter how many hours you worked this week: tenINVALID ENTRY! Try again!Enter how many hours you worked this week: 55Enter your hourly rate: 13.25Your pay is $828.125Exercise 3: Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error message. If the score is between 0.0 and 1.0, print a grade using the following table:Score Grade>= 0.9 A>= 0.8 B>= 0.74 C>= 0.7 D< 0.7 FALWAYS run the program repeatedly as shown below to test the various different values for input.EXAMPLE OUTPUTSEnter score: perfectBad scoreEnter score: 10.0Bad scoreEnter score: 0.75CEnter score: 0.5FIterations - While and For Loops (Chapter 5)Exercise 1: Write a program which repeatedly reads numbers until the user enters "done". Once "done" is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using?try?and?except?and print an error message and skip to the next number. You should use a WHILE LOOP. EXAMPLE OUTPUT (yours should be different)Enter a number: 4Enter a number: 5Enter a number: bad dataInvalid input, try again!Enter a number: 7Enter a number: done> The sum is 16, the count is 3, and the average is 5.333333333333333Exercise 2: Write a program that has a list that contains at least 7 different numbers and prints only the numbers that are even. (HINT: Use the modulo operator %) using a FOR LOOPEXAMPLE OUTPUT (yours should be different)468012Exercise 3: Write a program that counts how many e’s, are in a word that the user must input. Make sure the output is grammatically correct (i.e. it should not say “there are 1 e’s in your word”)EXAMPLE OUTPUT (yours should be different)Give me a random word and I will tell you how many e’s are in the word:antidisestablishmentarianismThere are 2 e’s in your wordGive me a random word and I will tell you how many e’s are in the word:pneumonoultramicroscopicsilicovolcanoconiosisThere is 1 e in your wordBuilt-In Functions and Modules (Chapter 4)Exercise 1: Write a program that has a list that contains at least 7 different numbers and prints the maximum and minimum numbers using the built-in functions max() and min()EXAMPLE OUTPUT > The maximum number is 79 and the minimum number is -3Exercise 2: Write a program that prints 3 random numbers between 1 and 100. EXAMPLE OUTPUT > 6> 89> 43Exercise 3: Write a program that has a list that contains at least 5 different names. Print the result of using the built-in function choice using the random module to choose 2 random names from the list.EXAMPLE OUTPUT > Sally> PiperExercise 4: Write a program that prints the result of the square root of 64 and 180 using the math module. Exercise 5: Write a program to tell the length of a list filled with numbers, a string, and a list with different string values such as a list of names or animals. EXAMPLE OUTPUT > The length of list1 is 8> The length of superhero is 9> The length of animalList is 10Created Functions (Chapter 4)Exercise 1: Type the program below to see what gets displayed and explain what happened. def fred(): print(“Zap”)def jane(): print(“ABC”)jane()fred()jane()Exercise 2: Rewrite your pay computation with time-and-a-half for overtime and create a function called?computepay?which takes two parameters (hours?and?rate) and call the function at the end of your program without user input.END OF PROGRAMcomputepay(55, 13.25)EXAMPLE OUTPUTYour pay is $828.125Exercise 3: Rewrite the grade program from the previous section using a function called?computegrade?that takes a score as its parameter and returns a grade as a string with user input as the argument. Score Grade>= 0.9 A>= 0.8 B>= 0.74 C>= 0.7 D< 0.7 FEXAMPLE OUTPUT Program Execution:Enter score: 0.95AEnter score: perfectBad scoreEnter score: 0.75CEnter score: 0.5FStrings (Chapter 6)Exercise 1: Write a?while?loop that starts at the last character in a string and works its way backwards to the first character in the string, printing each letter on a separate line, except backwards.Exercise 2: Write a program that would print the resultsExercise 3: Encapsulate (place and revise) the code below into a function named?count, and generalize it so that it accepts any string and any letter as arguments (so you should have two parameters).Exercise 4: Take the following Python code that stores a string:str = 'X-DSPAM-Confidence:0.8475'Use?find?and string-slicing to extract the portion of the string after the colon character and then use the?float?function to convert the extracted string into a floating-point number. Print the result. Exercise 5: Create a Mad Lib game that has 5 different inputs for an adjective, verb, name of person, verb that ends with -ing, and noun. Use the paragraph below as your Mad Lib and replace the blanks with the appropriate string format puter Science is my ____ class! We ___ a lot and have tons of fun. ____ is the best in the class at coding, but I am the best at ____. I hope we study ____ soon though!”Lists (Chapter 8)Exercise 1: Then write a function called?middle?that takes a list and returns a new list that contains all but the first and last elements.Exercise 2:Ask for a string of numbers from the user. Print the user’s input as two different lists, original and sorted from least to greatest. Exercise 3:?Rewrite the program that prompts the user for a list of numbers from Iteration section and prints out the maximum and minimum of the numbers at the end when the user enters "done". Write the program to store the numbers the user enters in a list and use the?max()?and?min()?functions to compute the maximum and minimum numbers after the loop completes. (HINT: use append to continue adding to your list!)EXAMPLE:Enter a number: 6Enter a number: 2Enter a number: 9Enter a number: 3Enter a number: 5Enter a number: doneMaximum: 9.0Minimum: 2.0Dictionaries (Chapter 9)Exercise 1Given the following dictionary:inventory = { 'gold' : 500, 'pouch' : ['flint', 'twine', 'gemstone'], 'backpack' : ['xylophone', 'dagger', 'bedroll', 'bread loaf']}Do the following:Add a key to inventory called 'pocket'.Set the value of 'pocket' to be a list consisting of the strings?'seashell', 'strange berry', and 'lint'..sort()the items in the list stored under the 'backpack' key.Then?.remove('dagger')?from the list of items stored under the 'backpack' key.Add 50 to the number stored under the 'gold' keyExercise 2Follow the steps bellow: - Create a new dictionary called?prices?using?{}?format like the example above.Put these values in your?prices?dictionary:"banana": 4, 6"apple": 2, 0"orange": 1.5, 2"pear": 3, 1Loop through each key in?prices. For each key, print out the key along with its price and stock information. Print the answer in the following format:appleprice: 2stock: 0Let's determine how much money you would make if you sold all of your food.Create a variable called?total?and set it to zero.Loop through the prices dictionaries. For each key in prices, multiply the number in prices by the number in stock. Print that value into the console and then add it to total.Finally, outside your loop, print total. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download