Python Exercise 1

Name of Innovation

Python Exercise 1

October 19, 2017 Uncategorized 1

 

----------------- sample -----------------------
def hello():
     """ prints hello, world """
     print "Hello, world!"
 
def areacircle(radius):
     """ Computes the area of a circle of the given radius """
     area = 3.14*radius**2
     print "The area of a circle of radius",radius,"is", area 

"""
Exercise:
Write a function 'def areatriangle(b,h)' to compute the area
of a triangle: formula is area = .5*b*h.
Output should look like:
The area of a triangle of base 3 and height 5 is 7.5
-------------------------------------------------------------------
Exercise: 
Print following using print on your screen.

name = "His name is Conan O'Brien"
cat = 'My cat is named "Butters"'


--------------------------------sample-----------------------------------------------------
def fahrenheit_to_celsius(temp):
     """ Converts Fahrenheit temperature to Celsius. 
     Formula is 5/9 of temp minus 32 """
     # Note that this line is not executed
     # end='' keeps print from starting a new line.
     newTemp = 5*(temp-32)/9
     print "The Fahrenheit temperature",temp,"is equivalent to",newTemp
     print " degrees Celsius"
 
#

"""
Exercise:
Write a function 'def celsius_to_fahrenheit(temp)' to convert Celsius
to Fahrentheit temperature. The formula is (9/5) times temp plus 32. 
Print the output in the form:
The Celsius temperature 50.0 is equivalent to 122.0 degrees Fahrenheit.
"""

----------------------------------sample --------------------------------------
def name():
     """ Input first and last name, combine to one string and print """
     fname = raw_input("Enter your first name: ")
     lname = raw_input("Enter your last name: ")
     fullname = fname + " " + lname
     print "Your name is:", fullname


Exercise:
Extend the name function written in class to include the city and state.
That is, ask two more questions to get the city and the state you live in.
Print where you are from on a new line. Put the customary comma between
city and state. to save time, here is the starting function.
Your run should look like the following (even if this is not the customary
way in your country):
Enter your first name: Bill

Enter your last name: Boyd

Enter the city you live in: Middletown

Enter the state you live in: CT
Your name is: Bill Boyd
You live in: Middletown, CT








-------------------------------- sample -------------------------------
def if_statement():
     """ Three slightly difference versions of if: if, if-else, if-elif-else"""
     x = 5
     y = 0
     z = 0
     if x > 0:
         print "x is positive" 
     if y > 0:
         print "y is positive" 
     else:
         print "y is not positive" 
     # elif can be repeated as often as necessary 
     if z > 0:
         print "z is positive" 
     elif z < 0:
         print "z is negative" 
     else:
         print "z must be 0" 
 
Exercise:
Write a function absolutevalue(num) that computes the absolute value of
a number. You will need to use an 'if' statement. Remember if a number is 
less than zero then you must multiply by -1 to make it greater than zero.
Give output in the form:
The absolute value of -5 is 5

------------------------------------------------ sample -----------------------
The 'while' loop. Loops are used to repeat actions and the scope of this
repetition is indicated by the indention after the 'while' statement.
"""
#
def cheer():
     """ Prints 2 4 6 8, who do we appreciate .... Note that everything in 
     the while loop is indented. The first line not indented is the first
     line following the while loop. """
     ct = 2
     while ct <= 8:
         print ct  # end = " " keeps from starting a new line
         ct = ct + 2
     print  # now we'll start a new line
     print "Who do we appreciate?" 
     print "Knowledge!" 
 
#
"""
Exercise:
Write a function count_down() that starts at 10 and counts down to rocket 
launch. It's output should be 10 9 8 7 6 5 4 3 2 1 BLASTOFF! You can make
all the numbers on the same line or different lines. Use a while loop.
"""
-------------------------------------------------------------------------
def cheer2():
     """ Same as cheer, but uses a for loop and range()
     range uses a start number, a stop number and a step size. """
     for ct in range(2,9,2):
         print ct 
         print
         print "Who do we appreciate?" 
         print "Engineers"

Exercise:
Write a function countdown1() that starts at 10 and counts down to rocket 
launch. It's output should be 10 9 8 7 6 5 4 3 2 1 BLASTOFF! You can make
all the numbers on the same line or different lines. Use a 'for' loop and
range(). range has a start and a stop and a step that MAY BE NEGATIVE.
"""

------------------------------------------ sample -----------------------

Some of our exercises involve finding and fixing errors in code. 
Here is an example. Can you see the errors (there are two)? Note that the
editor is pointing out one line with troubles.
You can find the error by reading the example carefully.
def favorite():
     my_toy = raw_input("What is my favorite toy? ")
      print "Your favorite toy is", my-toy

One Response

  1. […] at – Python Exercise 1 Python Exercise 2 […]

Comments are closed.