├── Coursera SHGSZXZTR5FZ- python 1.pdf ├── README.md ├── Week 3 - Assignment Write hello world ├── Week 3 Chapter 1 quiz answers ├── Week 4 Chapter 2- quiz answers ├── Week 4 - Assignment 2.2 ├── Week 4 - Assignment 2.3 ├── Week 5 Chapter 3 quiz answers ├── Week 5 - Assignment 3.3 ├── Week 5 - Assignment 3.1 ├── Week 6 Chapter 4 quiz answers ├── Week 6- Assignment 4.6 ├── Week 7 Chapter 5 quiz answers └── Week 7- Assignment 5.2 /Coursera SHGSZXZTR5FZ- python 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ritik2703/Coursera---Programming-for-Everybody-Getting-Started-with-Python-/83b31f3fafbae94d6da5d69cabfdd7b1efbe53e9/Coursera SHGSZXZTR5FZ- python 1.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Coursera---Programming for Everybody (Getting Started with Python) 2 | this contains all the answers to the quizes and asssignments for "Programming for Everybody (Getting Started with Python)" on Coursera by the University of Michigan. 3 | -------------------------------------------------------------------------------- /Week 3 - Assignment Write hello world: -------------------------------------------------------------------------------- 1 | Write a program that uses a print statement to say 'hello world' as shown in 'Desired Output'. 2 | 3 | Desired Output 4 | hello world 5 | 6 | 7 | # the code below almost works 8 | 9 | Code----- 10 | ******** 11 | 12 | 13 | print("hello world") 14 | 15 | 16 | ******** 17 | 18 | Your Output 19 | hello world 20 | -------------------------------------------------------------------------------- /Week 3 Chapter 1 quiz answers: -------------------------------------------------------------------------------- 1 | 2 | Week3 - Chapter 1- Quiz 1 3 | 4 | 5 | Q-1 When Python is running in the interactive mode and displaying the chevron prompt (>>>) – what question is Python asking you? 6 | 7 | answer- What would you like to do? 8 | 9 | 10 | 2. What will the following program print out: 11 | >>> x = 15; 12 | >>> x = x + 5; 13 | >>> print x 14 | 15 | answer- 20 16 | 17 | 18 | 3.) Python scripts (files) have names that end with: 19 | 20 | answrs- .py 21 | 22 | 23 | 4.) Which of these words are reserved words in Python ? 24 | 25 | answer- if 26 | break 27 | 28 | 29 | 5. ) What is the proper way to say “good-bye” to Python? 30 | 31 | answer- quit() 32 | 33 | 34 | 6.) Which of the parts of a computer actually executes the program instructions? 35 | 36 | answer- Central Processing Unit 37 | 38 | 39 | 7.) What is “code” in the context of this course? 40 | 41 | answer- A sequence of instructions in a programming language 42 | 43 | 44 | 8.) A USB memory stick is an example of which of the following components of computer architecture? 45 | 46 | answer- Secondary Memory 47 | 48 | 49 | 9.) What is the best way to think about a “Syntax Error” while programming? 50 | 51 | answer- The computer did not understand the statement that you entered 52 | 53 | 54 | 10.) Which of the following is not one of the programming patterns covered in Chapter 1? 55 | 56 | answer- Random steps 57 | 58 | 59 | 60 | 61 | for any other query/help- 62 | contact - ritikdagar0203@gmail.com 63 | -------------------------------------------------------------------------------- /Week 4 Chapter 2- quiz answers: -------------------------------------------------------------------------------- 1 | Week 4 2 | Chapter 2 3 | Quiz answers:- 4 | 5 | 6 | 1.) In the following code, print 98.6. What is “98.6”? 7 | 8 | answer- A constant 9 | 10 | 11 | In the following code, x = 42. What is “x”? 12 | A variable 13 | 14 | 15 | Which of the following variables is the “most mnemonic”? 16 | hours 17 | 18 | 19 | Which of the following is not a Python reserved word? 20 | speed 21 | 22 | 23 | Assume the variable x has been initialized to an integer value (e.g., x = 3). What does the following statement do? x = x + 2 24 | Retrieve the current value for x, add two to it and put the sum back into x 25 | 26 | 27 | Which of the following elements of a mathematical expression in Python is evaluated first? 28 | –Parenthesis() 29 | 30 | What is the value of the following expression 31 | 2 32 | 33 | What will be the value of x after the following statement executes: x = 1 + 2 * 3 – 8 / 4 34 | 5 35 | 36 | 37 | What will be the value of x when the following statement is executed: x = int(98.6) 38 | 98 39 | 40 | 41 | What does the Python raw_input() function do? 42 | Pause the program and read data from the user 43 | 44 | 45 | // if you score 100% ..please rate this repository file 46 | 47 | Studying can feel like an enormous challenge, especially when tests and exams are looming. I will help you to find studying easier than perhaps it has been to this point. 48 | for any other query/help:- 49 | contact- ritikdagr0203@gmail.com 50 | 51 | -------------------------------------------------------------------------------- /Week 4 - Assignment 2.2: -------------------------------------------------------------------------------- 1 | 2.2 Write a program that uses input to prompt a user for their name and then welcomes them. Note that input will pop up a dialog box. 2 | Enter Sarah in the pop-up box when you are prompted so your output will match the desired output. 3 | 4 | 5 | code- 6 | 7 | 8 | # The code below almost works 9 | 10 | ******************* 11 | 12 | 13 | name = input("Enter your name") 14 | 15 | print ("Hello",name) 16 | 17 | *********************** 18 | 19 | Desired Output 20 | Hello Sarah 21 | 22 | your output 23 | Hello Sarah 24 | -------------------------------------------------------------------------------- /Week 4 - Assignment 2.3: -------------------------------------------------------------------------------- 1 | 2 | 3 | 2.3 4 | Write a program to prompt the user for hours and rate per hour using input to compute gross pay. 5 | Use 35 hours and a rate of 2.75 per hour to test the program (the pay should be 96.25). 6 | You should use input to read a string and float() to convert the string to a number. Do not worry about error checking or bad user data. 7 | 8 | 9 | 10 | code---- 11 | ******************** 12 | 13 | 14 | hrs = float(input("Enter Hours:")) 15 | rate = float(input("Enter Rate:")) 16 | pay= hrs*rate 17 | print ("Pay:",pay) 18 | 19 | 20 | ************************ 21 | 22 | 23 | Desired Output 24 | Pay: 96.25 25 | 26 | your output 27 | Pay: 96.25 28 | -------------------------------------------------------------------------------- /Week 5 Chapter 3 quiz answers: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 1 What do we do to a Python statement that is immediately after an if statement to indicate that the statement is to be executed only when the if statement is true? 5 | ans Indent the line below the if statement 6 | 7 | 8 | 2 Which of these operators is not a comparison / logical operator? 9 | ans= 10 | 11 | 12 | 3 What is true about the following code segment: if x == 5 : print ‘Is 5’ print ‘Is Still 5’ print ‘Third 5’ 13 | ans Depending on the value of x, either all three of the print statements will execute or none of the statements will execute 14 | 15 | 16 | 4 When you have multiple lines in an if block, how do you indicate the end of the if block? 17 | ans You de-indent the next line past the if block to the same level of indent as the original if statement 18 | 19 | 20 | 5 You look at the following text: if x == 6 : print ‘Is 6’ print ‘Is Still 6’ print ‘Third 6’. It looks perfect but Python is giving you an ‘Indentation Error’ on the second print statement. What is the most likely reason? 21 | ans You have mixed tabs and spaces in the file 22 | 23 | 24 | 6 What is the Python reserved word that we use in two-way if tests to indicate the block of code that is to be executed if the logical test is false? 25 | ans else 26 | 27 | 28 | 7 What will the following code print out? x = 0 if x < 2 : print ‘Small’ elif x < 10 : print ‘Medium’ else : print ‘LARGE’ print ‘All done’ 29 | ans Small All done 30 | 31 | 32 | 8 For the following code, if x < 2: print ‘Below 2’ elif x >= 2: print ‘Two or more’ else: print ‘Something else’. What value of ‘x’ will cause ‘Something else’ to print out? 33 | ans This code will never print ‘Something else’ regardless of the value for ‘x’ 34 | 35 | 36 | 9 In the following code (numbers added) – which will be the last line to execute successfully? (1) astr = ‘Hello Bob’; (2) istr = int(astr); (3) print ‘First’, istr; (4) astr = ‘123’; (5) istr = int(astr); (6) print ‘Second’, istr 37 | ans 1 38 | 39 | 40 | 10 For the following code: astr = ‘Hello Bob’ istr = 0 try: istr = int(astr) except: istr = -1. What will the value be for istr after this code executes? 41 | ans -1 42 | -------------------------------------------------------------------------------- /Week 5 - Assignment 3.3: -------------------------------------------------------------------------------- 1 | 3.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. If the score is between 0.0 and 1.0, print a grade using the following table: 2 | Score Grade 3 | >= 0.9 A 4 | >= 0.8 B 5 | >= 0.7 C 6 | >= 0.6 D 7 | < 0.6 F 8 | If the user enters a value out of range, print a suitable error message and exit. For the test, enter a score of 0.85. 9 | 10 | 11 | code---- 12 | 13 | ************************* 14 | 15 | score = float(raw_input("Enter Score: ")) 16 | if(score>1.0): 17 | print ("out of range") 18 | elif score >= 0.9: 19 | print ("A") 20 | elif score >= 0.8: 21 | print ("B") 22 | elif score >= 0.7: 23 | print ("C") 24 | elif score >= 0.6: 25 | print ("D") 26 | elif score < 0.6: 27 | print ("F") 28 | 29 | 30 | *********************** 31 | 32 | Your Output 33 | B 34 | 35 | 36 | Desired Output 37 | B 38 | -------------------------------------------------------------------------------- /Week 5 - Assignment 3.1: -------------------------------------------------------------------------------- 1 | 2 | 3 | 3.1 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. 4 | Pay the hourly rate for the hours up to 40 and 1.5 times the hourly rate for all hours worked above 40 hours. 5 | Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). 6 | You should use input to read a string and float() to convert the string to a number. 7 | Do not worry about error checking the user input - assume the user types numbers properly. 8 | 9 | code----------- 10 | 11 | ******************** 12 | 13 | hrs = input("Enter Hours:") 14 | h = float(hrs) 15 | rate = float(input("Enter Rate:")) 16 | if h > 40 : 17 | rate1 = (rate * 1.5) * (h-40) 18 | 19 | pay = ((h-5)*rate) + rate1 20 | print (pay) 21 | 22 | 23 | ************************ 24 | 25 | Desired Output 26 | 498.75 27 | 28 | your output 29 | 498.75 30 | 31 | 32 | -------------------------------------------------------------------------------- /Week 6 Chapter 4 quiz answers: -------------------------------------------------------------------------------- 1 | 2 | 3 | 1. Which Python keyword indicates the start of a function definition? 4 | ans- def 5 | 6 | 7 | 2. In Python, how do you indicate the end of the block of code that makes up the function? 8 | ans- You de-indent a line of code to the same indent level as the def keyword 9 | 10 | 11 | 3. In Python what is the raw_input() feature best described as? 12 | ans- A built-in function 13 | 14 | 15 | 4. What does the following code print out? def thing(): print 'Hello'; print 'There' 16 | ans- There 17 | 18 | 19 | 5. In the following Python code, which of the following is an "argument" to a function? x = 'banana'; y = max(x); print y 20 | ans- x 21 | 22 | 23 | 6. What will the following Python code print out? def func(x) : print x; func(10); func(20) 24 | ans- 10, 20 25 | 26 | 27 | 7. Which line of the following Python program is useless? def stuff(): print 'Hello' return print 'World' stuff() 28 | ans- print 'World' 29 | 30 | 31 | 8. -What will the following Python program print out? def greet(lang): if lang == 'es': return 'Hola' elif lang == 'fr': return 'Bonjour' else: return 'Hello' print greet('fr'),'Michael' 32 | ans- Bonjour Michael 33 | 34 | 35 | 9. What does the following Python code print out? (Note that this is a bit of a trick question and the code has what many would consider to be a flaw/bug - so read carefully). def addtwo(a, b): added = a + b return a; x = addtwo(2, 7); print x 36 | ans- 2 37 | 38 | 39 | 10. What is the most important benefit of writing your own functions? 40 | ans- Avoiding writing the same non-trivial code more than once in your program 41 | -------------------------------------------------------------------------------- /Week 6- Assignment 4.6: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.6 4 | Write a program to prompt the user for hours and rate per hour using input to compute gross pay. 5 | Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. 6 | Put the logic to do the computation of pay in a function called computepay() and use the function to do the computation. 7 | The function should return a value. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). 8 | You should use input to read a string and float() to convert the string to a number. 9 | Do not worry about error checking the user input unless you want to - you can assume the user types numbers properly. 10 | Do not name your variable sum or use the sum() function. 11 | 12 | 13 | code------------ 14 | 15 | ****************************************** 16 | 17 | def computepay(h,r): 18 | if h>40: 19 | rate1 = (r*1.5)*(h-40) 20 | return ((h-5)*r)+rate1 21 | hrs = float(raw_input("Enter Hours:")) 22 | rate = float(raw_input("Enter Rate:")) 23 | p = computepay(hrs,rate) 24 | print (p) 25 | 26 | **************************************** 27 | Your Output 28 | 498.75 29 | 30 | Desired Output 31 | 498.75 32 | -------------------------------------------------------------------------------- /Week 7 Chapter 5 quiz answers: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 1 What is wrong with this Python loop: n = 5; while n > 0 : print n; print 'All done' 6 | ans This loop will run forever. 7 | 8 | 9 | 2 What does the break statement do? 10 | ans Exits the currently executing loop. 11 | 12 | 13 | 3 What does the continue statement do? 14 | ans Jumps to the "top" of the loop and starts the next iteration. 15 | 16 | 17 | 4 What does the following Python program print out? tot = 0; for i in [5, 4, 3, 2, 1] : tot = tot + 1; print tot 18 | ans 5 19 | 20 | 21 | 5 What is the iteration variable in the following Python code: friends = ['Joseph', 'Glenn', 'Sally']; for friend in friends : print 'Happy New Year:', friend; print 'Done!' 22 | ans friend 23 | 24 | 25 | 6 What is a good description of the following bit of Python code?; zork = 0; for thing in [9, 41, 12, 3, 74, 15] : zork = zork + thing; print 'After', zork 26 | ans Sum all the elements of a list 27 | 28 | 29 | 7 What will the following code print out? smallest_so_far = -1; for the_num in [9, 41, 12, 3, 74, 15] : if the_num < smallest_so_far : smallest_so_far = the_num; print smallest_so_far 30 | ans -1 31 | 32 | 33 | 8 What is a good statement to describe the is operator as used in the following if statement: if smallest is None : smallest = value 34 | ans matches both type and value 35 | 36 | 37 | 9 Which reserved word indicates the start of an "indefinite" loop in Python? 38 | ans while 39 | 40 | 41 | 10 How many times will the body of the following loop be executed? n = 0; while n > 0 : print 'Lather' print 'Rinse'; print 'Dry off!' 42 | ans 0 43 | 44 | 45 | -------------------------------------------------------------------------------- /Week 7- Assignment 5.2: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. 4 | Once 'done' is entered, print out the largest and smallest of the numbers. 5 | If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message 6 | and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below. 7 | 8 | 9 | code------ 10 | 11 | 12 | ******************************** 13 | 14 | 15 | largest = None 16 | smallest = None 17 | while True: 18 | num = raw_input("Enter a number: ") 19 | if num == "done": break 20 | try: 21 | num = int(num) 22 | if largest is None or largest < num: largest = num 23 | if smallest is None or smallest > num: smallest = num 24 | except: 25 | print ("Invalid input") 26 | continue 27 | print ("Maximum is",largest) 28 | print ("Minimum is",smallest) 29 | 30 | **************************************** 31 | 32 | Your Output 33 | Invalid input 34 | Maximum is 10 35 | Minimum is 2 36 | 37 | Desired Output 38 | Invalid input 39 | Maximum is 10 40 | Minimum is 2 41 | 42 | --------------------------------------------------------------------------------