Crash Course on Python
All Quiz Answer
Module 1 Graded Assessment
Q1) What is a computer program ?
- A step-by-step recipe of what needs to be done to complete a task, that gets executed by the computer.
Q2) What’s automation ?
- The process of replacing a manual step with one that happens automatically.
Q3) Which of the following tasks are good candidates for automation ? Check all that apply.
- Creating a report of how much each sales person has sold in the last month.
- Setting the home directory and access permissions for new employees joining your company.
- Populating your company's e-commerce site with the latest products in the catalog.
Q4) What are some characteristics of the Python programming language? Check all that apply.
- Python programs are easy to write and understand.
- The Python interpreter reads our code and transforms it into computer instructions.
- We can practice Python using web interpreters or codepads as well as executing it locally.
Q5) How does Python compare to other programming languages ?
- Each programming language has its advantages and disadvantages.
Q6) Write a Python script that outputs "Automating with Python is fun!" to the screen.
- print("Automating with Python is fun!")
Q7) Fill in the blanks so that the code prints "Yellow is the color of sunshine".
- color = "Yellow"
- thing = "sunshine"
- print(color + " is the color of " + thing)
Q8) Keeping in mind there are 86400 seconds per day, write a program that calculates how many seconds there are in a week, if a week is 7 days. Print the result on the screen.
Note: Your result should be in the format of just a number, not a sentence.
- day1= 86400 ;
- week7 = 7;
- total = week7 * day1;
- print(total);
Q9) Use Python to calculate how many different passwords can be formed with 6 lower case English letters. For a 1 letter password, there would be 26 possibilities. For a 2 letter password, each letter is independent of the other, so there would be 26 times 26 possibilities. Using this information, print the amount of possible passwords that can be formed with 6 letters.
- a=26**6;
- print(a);
Q10) Most hard drives are divided into sectors of 512 bytes each. Our disk has a size of 16 GB. Fill in the blank to calculate how many sectors the disk has.
Note: Your result should be in the format of just a number, not a sentence.
- disk_size = 16*1024*1024*1024
- sector_size = 512
- sector_amount = disk_size/sector_size
- print(sector_amount)
Module 2 Graded Assessment
- def color_translator(color):
- if color == "red":
- hex_color = "#ff0000"
- elif color == "green":
- hex_color = "#00ff00"
- elif color == "blue":
- hex_color = "#0000ff"
- else:
- hex_color = "unknown"
- return hex_color
- print(color_translator("blue")) # Should be #0000ff
- print(color_translator("yellow")) # Should be unknown
- print(color_translator("red")) # Should be #ff0000
- print(color_translator("black")) # Should be unknown
- print(color_translator("green")) # Should be #00ff00
- print(color_translator("")) # Should be unknown
- False
- To handle more than two comparison cases
- def exam_grade(score):
- if score>99:
- grade = "Top Score"
- elif score>56:
- grade = "Pass"
- else:
- grade = "Fail"
- return grade
- print(exam_grade(65)) # Should be Pass
- print(exam_grade(55)) # Should be Fail
- print(exam_grade(60)) # Should be Pass
- print(exam_grade(95)) # Should be Pass
- print(exam_grade(100)) # Should be Top Score
- print(exam_grade(0)) # Should be Fail
- 1
- def format_name(first_name, last_name):
- if first_name=='' and last_name != '':
- return "Name: "+last_name
- elif last_name =='' and first_name !='':
- return "Name: "+first_name
- elif first_name=='' and last_name == '':
- return ''
- else:
- return 'Name: '+ last_name+', '+ first_name
- print(format_name("Earnest", "Hemmingway"))
- # Should be "Name: Hemingway, Ernest"
- print(format_name("", "Madonna"))
- # Should be "Name: Madonna"
- print(format_name("Voltaire", ""))
- # Should be "Name: Voltaire"
- print(format_name('', ''))
- # Should be ""
- def longest_word(word1, word2, word3):
- if len(word1) >= len(word2) and len(word1) >= len(word3):
- word = word1
- elif len(word2) >= len(word3) and len(word2) >= len(word1):
- word = word2
- else:
- word = word3
- return(word)
- print(longest_word("chair", "couch", "table"))
- print(longest_word("bed", "bath", "beyond"))
- print(longest_word("laptop", "notebook", "desktop"))
- def sum(x, y):
- return(x+y)
- print(sum(sum(1,2), sum(3,4)))
- True
- def fractional_part(numerator, denominator):
- x=numerator
- y=denominator
- if (y == 0 ):
- return 0
- z = (x % y) / y
- if z == 0:
- return 0
- else:
- return z
- # Operate with numerator and denominator to
- # keep just the fractional part of the quotient
- print(fractional_part(5, 5)) # Should be 0
- print(fractional_part(5, 4)) # Should be 0.25
- print(fractional_part(5, 3)) # Should be 0.66...
- print(fractional_part(5, 2)) # Should be 0.5
- print(fractional_part(5, 0)) # Should be 0
- print(fractional_part(0, 5)) # Should be 0
Module 3 Graded Assessment
- number = 1
- while number <= 7:
- print(number, end=" ")
- number=number+1
- def show_letters(word):
- for character in ("Hello"):
- print(character)
- show_letters("Hello")
- # Should print one line per letter
- def digits(n):
- count = 0
- if n == 0:
- n=n+1
- while n != 0:
- n //= 10
- count+= 1
- return count
- print(digits(25)) # Should print 2
- print(digits(144)) # Should print 3
- print(digits(1000)) # Should print 4
- print(digits(0)) # Should print 1
- def multiplication_table(start, stop):
- for x in range(1,start+3):
- for y in range(1,start+3):
- print(str(x*y), end=" ")
- print()
- multiplication_table(1, 3)
- # Should print the multiplication table shown above
- def counter(start, stop):
- x = start
- if x==2:
- return_string = "Counting down: "
- while x >= stop:
- return_string += str(x)
- if x>1:
- return_string += ","
- x=x-1
- else:
- return_string = "Counting up: "
- while x <= stop:
- return_string += str(x)
- if x<stop:
- return_string += ","
- x=x+1
- return return_string
- print(counter(1, 10)) # Should be "Counting up: 1,2,3,4,5,6,7,8,9,10"
- print(counter(2, 1)) # Should be "Counting down: 2,1"
- print(counter(5, 5)) # Should be "Counting up: 5"
- def loop(start, stop, step):
- return_string = ""
- if step == 0:
- step=1
- if start > stop:
- step = abs(step) * -1
- else:
- step = abs(step)
- for count in range(start, stop, step):
- return_string += str(count) + " "
- return return_string.strip()
- print(loop(11,2,3)) # Should be 11 8 5
- print(loop(1,5,0)) # Should be 1 2 3 4
- print(loop(-1,-2,0)) # Should be -1
- print(loop(10,25,-2)) # Should be 10 12 14 16 18 20 22 24
- print(loop(1,1,1)) # Should be empty
- Failure to initialize variables
- 7
- 8
- votes(['yes', 'no', 'maybe'])
Module 4 Graded Assessment
- def format_address(address_string):
- hnum = []
- sname = []
- addr = address_string.split()
- for a in addr:
- if a.isnumeric():
- hnum.append(a)
- else:
- sname.append(a)
- return "house number {} on street named {}".format("".join(hnum), " ".join(sname))
- print(format_address("123 Main Street"))
- # Should print: "house number 123 on street named Main Street"
- print(format_address("1001 1st Ave"))
- # Should print: "house number 1001 on street named 1st Ave"
- print(format_address("55 North Center Drive"))
- # Should print "house number 55 on street named North Center Drive"
- def highlight_word(sentence, word):
- return(sentence.replace(word,word.upper()))
- print(highlight_word("Have a nice day", "nice"))
- print(highlight_word("Shhh, don't be so loud!", "loud"))
- print(highlight_word("Automating with Python is fun", "fun"))
- def combine_lists(list1, list2):
- # Generate a new list containing the elements of list2
- list_order = list2
- list_reverse=list1
- # Followed by the elements of list1 in reverse order
- list_reverse.reverse() #print (list_reverse)
- list_combine =[]
- for word in list_order:
- list_combine.append(word)
- for word in list_reverse:
- list_combine.append(word)
- return list_combine
- Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
- Drews_list = ["Mike", "Carol", "Greg", "Marcia"]
- print(combine_lists(Jamies_list, Drews_list))
- def squares(start, end):
- return [n*n for n in range(start,end+1)]
- print(squares(2, 3)) # Should be [4, 9]
- print(squares(1, 5)) # Should be [1, 4, 9, 16, 25]
- print(squares(0, 10)) # Should be [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
- def car_listing(car_prices):
- result = ""
- for car, price in car_prices.items():
- result += "{} costs {} dollars".format(car, price) + "\n"
- return result
- print(car_listing({"Kia Soul":19000, "Lamborghini Diablo":55000, "Ford Fiesta":13000, "Toyota Prius":24000}))
- def combine_guests(guests1, guests2):
- # Combine both dictionaries into one, with each key listed
- # only once, and the value from guests1 taking precedence
- new_guests = guests2.copy()
- new_guests.update(guests1)
- return new_guests
- Rorys_guests = { "Adam":2, "Brenda":3, "David":1, "Jose":3, "Charlotte":2, "Terry":1, "Robert":4}
- Taylors_guests = { "David":4, "Nancy":1, "Robert":2, "Adam":1, "Samantha":3, "Chris":5}
- print(combine_guests(Rorys_guests, Taylors_guests))
- def count_letters(text):
- result = {}
- # Go through each letter in the text
- for letter in text:
- # Check if the letter needs to be counted or not
- if letter.isupper():
- letter=letter.lower()
- if letter.isalpha():
- if letter not in result:
- result[letter] = 0
- result[letter] += 1
- # Add or increment the value in the dictionary
- return result
- print(count_letters("AaBbCc"))
- # Should be {'a': 2, 'b': 2, 'c': 2}
- print(count_letters("Math is fun! 2+2=4"))
- # Should be {'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}
- print(count_letters("This is a sentence."))
- # Should be {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}
- pop, t, us
- ['red', 'white', 'yellow', 'blue']
- ['router', 'localhost', 'google']
4 Comments
nice article...dude..
ReplyDeletevery nice article..
ReplyDeleteshare more PYTHON Questions
share more new Question please niyander
ReplyDeletegood work bro
ReplyDelete