Second Python Exercises

Your second python exercises

Python Cheatsheet

Basic Methods

Multiply (Easy)

Write a method that you can multiple two numbers (x,y). Test with the following examples:

  • 4, 2
  • 0, 4
  • 900, 32
  • 29999, 0
def multiply(x, y):
  return(x * y)

print(multiply(4, 2))
print(multiply(0, 4))
print(multiply(900, 32))

Divide (Easy, with a special case)

Write a method that you can divide two numbers (x,y). If the denominator is zero, set it to the numerator (so that the method returns one). Test with the following examples:

  • 4, 2
  • 0, 4
  • 900, 32
def divide(x, y):
  if(y == 0):
    y = x
  else:
    return(x / y)

print(divide(4, 2))
print(divide(0, 4))
print(divide(900, 32))

Print Name

Write a method that takes a parameter (name) and greets that user.

def greet(name):
  return("Hello %s, you rock!" % name)

print(greet("phybernightmare"))

Smallest Number

Write a method that evaluates two numbers and returns the smallest. If the numbers are the same, it should return a message stating so.

def smallest_number(x, y):
  smallest = x

  if x == y:
    smallest = "They are the same."
  elif y < x:
    smallest = y
  elif x < y:
    smallest = x

  return(smallest)

print(smallest_number(400,2))

String Reverse

Write a method that accepts a string and returns the characters in reverses order.

Hint: Look at the Python documentation.

def reverse(string):
  return(string[::-1])

test_string = "Praxis Program"
print(reverse(test_string))

Loops

While Loop

Write a method that uses a while loop to count from 1 to 1000 and print the number of the current iteration to the screen. (That is, the first time through, the loop should print “1”; “2” the next time through; and so forth.)

def count(limit):
  counter = 0
  while counter <= limit:
    print(counter)
    counter += 1

count(1000)

“Until” Loop

As we talked about in class, there is no “until” loop in Python. However, While loops can provide similar results. Write a method that uses an while loop to print each number from 0 to 5.

def count(limit):
  counter = 0
  while counter <= limit:
    print(counter)
    counter += 1

count(5)

For Loop

Write a method that uses a for loop to print each number from 1 to 10.

def count(limit):
  counter = 0
  for i in range(limit + 1)
    print(counter)
    counter += 1

count(10)

Classes

Write a simple class that defines a person with attributes of first_name, last_name and has a method signature of to_s which prints the object as “Jefferson, Thomas”.

class Person:
    def __init__(self, fname, lname):
        self.first_name = fname
        self.last_name = lname

    def to_s(self):
        print(self.last_name + ", " + self.first_name)


tj = Person("Thomas", "Jefferson")
tj.to_s()

Can’t Get Enough?

Can’t get enough? Work through the Learn Python the Hard Way exercises and/or implement the last programming exercises as Objects.