diff --git a/Python3/ex01-studydrills.py b/Python3/ex01-studydrills.py new file mode 100644 index 0000000..5f4a6e7 --- /dev/null +++ b/Python3/ex01-studydrills.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 + +# ex01: A Good First Program + +print("Hello World!") +print("Hello Again") +print("I like typing this.") +print("This is fun.") +print('Yay! Printing.') +print("I'd much rather you 'not'.") +# print('I "said" do not touch this.') +print('This is another line!') \ No newline at end of file diff --git a/Python3/ex01.py b/Python3/ex01.py index 0e2a5b7..27a1389 100755 --- a/Python3/ex01.py +++ b/Python3/ex01.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex01: A Good First Program @@ -8,5 +8,4 @@ print("This is fun.") print('Yay! Printing.') print("I'd much rather you 'not'.") -# print('I "said" do not touch this.') -print('This is another line!') +print('I "said" do not touch this.') \ No newline at end of file diff --git a/Python3/ex02.py b/Python3/ex02.py index 8c4c146..a5417aa 100755 --- a/Python3/ex02.py +++ b/Python3/ex02.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex2: Comments and Pound Characters @@ -10,4 +10,4 @@ # You can also use a comment to "disable" or comment out a piece of code: # print("This won't run.") -print("This will run.") +print("This will run.") \ No newline at end of file diff --git a/Python3/ex03-new.py b/Python3/ex03-new.py deleted file mode 100755 index 3d9c0d9..0000000 --- a/Python3/ex03-new.py +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/python3 - -# Find something you need to calculate and write a new .py file that -# does it. - -# integer -print(50 * 2) -print(1/500) -print(4 * 3 - 1) - -# float -print(3.14 * 2 * 200) -print(1.0/20) - -# The following expressions are more complicated calculations. -# Ignore them if you haven't learned anything about each type. - -# decimal: more accurate than float -# import decimal -# print (decimal.Decimal(9876) + decimal.Decimal("54321.012345678987654321")) - -# fraction -# import fractions -# print fractions.Fraction(1, 3) -# print fractions.Fraction(4, 6) -# print 3 * fractions.Fraction(1, 3) - -# complex -# print(1j * 1J) -# print(3 + 1j * 3)j diff --git a/Python3/ex03-studydrills.py b/Python3/ex03-studydrills.py new file mode 100644 index 0000000..1c25f8f --- /dev/null +++ b/Python3/ex03-studydrills.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +# ex3: Numbers and Math + +# Print "I will now count my chickens:" +print("I will now count my chickens:") + +# Print the number of hens +print("Hens", 25 + 30 / 6) +# Print the number of roosters +print("Roosters, 100 -25 * 3 % 4") + +# Print "Now I will count the eggs:" +print("Now I will count the eggs:") + +# number of eggs, Notice that '/' operator returns float value in Python3 +print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6) + +# Print "Is it true that 3 + 2 < 5 - 7?" +print("Is it true that 3 + 2 < 5 - 7?") + +# Print whether 3+2 is smaller than 5-7(True or False) +print(3 + 2 < 5 - 7) + +# Calculate 3+2 and print the result +print("What is 3 + 2?", 3 + 2) +# Calculate 5-7 and print the result +print("What is 5 - 7?", 5 - 7) + +# Print "Oh, that's why it's False." +print("Oh, that's why it's False.") + +# Print "Oh, that's why it's False." +print("How about some more.") + +# Print whether 5 is greater than -2(True or False) +print("Is it greater?", 5 > -2) +# Print whether 5 is greater than or equal to -2?(True or False) +print("Is it greater or equal?", 5 >= -2) +# Print whether 5 is less than or equal to -2 (True or False) +print("Is it less or equal?", 5 <= -2) + +# Find something you need to calculate and write a new .py file that +# does it. + +# integer +print(50 * 2) +print(1/500) +print(4 * 3 - 1) +print(3.14 * 2 * 200) +print(1.0/20) + +# The following expressions are more complicated calculations. +# Ignore them if you haven't learned anything about each type. + +# decimal: more accurate than float +import decimal +print(decimal.Decimal(9876) + decimal.Decimal("54321.012345678987654321")) + +# fraction +import fractions +print(fractions.Fraction(1, 3)) +print(fractions.Fraction(4, 6)) +print(3 * fractions.Fraction(1, 3)) + +# complex +print(3-4j) +print(3-4J) +print(complex(3,-4)) +print(3 + 1J * 3j) \ No newline at end of file diff --git a/Python3/ex03.py b/Python3/ex03.py index b95e96a..7cebf2b 100755 --- a/Python3/ex03.py +++ b/Python3/ex03.py @@ -1,41 +1,28 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex3: Numbers and Math -# Print "I will now count my chickens:" print("I will now count my chickens:") -# Print the number of hens print("Hens", 25 + 30 / 6) -# Print the number of roosters -print("Roosters, 100 -25 * 3 % 4") +print("Roosters", 100 -25 * 3 % 4) -# Print "Now I will count the eggs:" print("Now I will count the eggs:") -# number of eggs, Notice that '/' operator returns float value in Python3 print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6) -# Print "Is it true that 3 + 2 < 5 - 7?" print("Is it true that 3 + 2 < 5 - 7?") -# Print whether 3+2 is smaller than 5-7(True or False) print(3 + 2 < 5 - 7) -# Calculate 3+2 and print the result print("What is 3 + 2?", 3 + 2) -# Calculate 5-7 and print the result + print("What is 5 - 7?", 5 - 7) -# Print "Oh, that's why it's False." print("Oh, that's why it's False.") -# Print "Oh, that's why it's False." print("How about some more.") -# Print whether 5 is greater than -2(True or False) print("Is it greater?", 5 > -2) -# Print whether 5 is greater than or equal to -2?(True or False) print("Is it greater or equal?", 5 >= -2) -# Print whether 5 is less than or equal to -2 (True or False) -print("Is it less or equal?", 5 <= -2) +print("Is it less or equal?", 5 <= -2) \ No newline at end of file diff --git a/Python3/ex04-studydrills.py b/Python3/ex04-studydrills.py new file mode 100644 index 0000000..af495af --- /dev/null +++ b/Python3/ex04-studydrills.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +# ex4: Variables And Names + +# number of cars +cars = 100 +# space in a car. Can be 4 in Python 3. +space_in_a_car = 4 +# number of drivers +drivers = 30 +# number of passengers +passengers = 90 +# number of cars that are empty(without driver) +cars_not_driven = cars - drivers +# number of cars that are driven +cars_driven = drivers +# number of cars +carpool_capacity = cars_driven * space_in_a_car +# average number of passengers in each car +average_passengers_per_car = passengers / cars_driven + +print("There are", cars, "cars available.") +print("There are only", drivers, "drivers available.") +print("There will be", cars_not_driven, "empty cars today.") +print("We can transport", carpool_capacity, "people today.") +print("We have", passengers, "to carpool today.") +print("We need to put about", average_passengers_per_car, "in each car.") diff --git a/Python3/ex04.py b/Python3/ex04.py index 1087d16..496d6b3 100755 --- a/Python3/ex04.py +++ b/Python3/ex04.py @@ -1,22 +1,14 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex4: Variables And Names -# number of cars cars = 100 -# space in a car. Can be 4 in Python 3. space_in_a_car = 4.0 -# number of drivers drivers = 30 -# number of passengers passengers = 90 -# number of cars that are empty(without driver) cars_not_driven = cars - drivers -# number of cars that are driven cars_driven = drivers -# number of cars carpool_capacity = cars_driven * space_in_a_car -# average number of passengers in each car average_passengers_per_car = passengers / cars_driven print("There are", cars, "cars available.") @@ -24,4 +16,4 @@ print("There will be", cars_not_driven, "empty cars today.") print("We can transport", carpool_capacity, "people today.") print("We have", passengers, "to carpool today.") -print("We need to put about", average_passengers_per_car, "in each car.") +print("We need to put about", average_passengers_per_car, "in each car.") \ No newline at end of file diff --git a/Python3/ex05-studydrills.py b/Python3/ex05-studydrills.py new file mode 100644 index 0000000..2cd08c2 --- /dev/null +++ b/Python3/ex05-studydrills.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +# ex5: More Variables and Printing + +name = 'Zed A. Shaw' +age = 35 # not a lie +height = 74 # inches +weight = 180 # lbs +eyes = "Blue" +teeth = 'White' +hair = 'Brown' + +print("Let's talk about %s" % name) +print("He's %d inches tall." % height) +print("He\'s %d pounds heavy." % weight) +print("He's got %s eyes and %s hair." % (eyes, hair)) +print("Actually that's not too heavy") +print("His teeth are usually %s depending on the coffee." % teeth) + +# this line is tricky, try to get it exactly right +print('If I add %d, %d and %d I get %d.' % (age, height, weight, age + height + weight)) + +# try more format characters +my_greeting = "Hello,\t" +my_first_name = 'Joseph' +my_last_name = 'Pan' +my_age = 24 +# Print 'Hello,\t'my name is Joseph Pan, and I'm 24 years old. +print("%r my name is %s %s, and I'm %d years old." % (my_greeting, my_first_name, my_last_name, my_age)) + +# Try to write some variables that convert the inches and pounds to centimeters and kilos. +inches_per_centimeters = 2.54 +pounds_per_kilo = 0.45359237 + +print("He's %f centimeters tall." % (height * inches_per_centimeters)) +print("He's %f kilos heavy." % (weight * pounds_per_kilo)) \ No newline at end of file diff --git a/Python3/ex05.py b/Python3/ex05.py index 900a941..f023f72 100755 --- a/Python3/ex05.py +++ b/Python3/ex05.py @@ -1,36 +1,21 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex5: More Variables and Printing -name = 'Zed A. Shaw' -age = 35 # not a lie -height = 74 # inches -weight = 180 # lbs -eyes = "Blue" -teeth = 'White' -hair = 'Brown' +my_name = 'Zed A. Shaw' +my_age = 35 # not a lie +my_height = 74 # inches +my_weight = 180 # lbs +my_eyes = "Blue" +my_teeth = 'White' +my_hair = 'Brown' -print("Let's talk about %s" % name) -print("He's %d inches tall." % height) -print("He\'s %d pounds heavy." % weight) -print("He's got %s eyes and %s hair." % (eyes, hair)) +print("Let's talk about %s" % my_name) +print("He's %d inches tall." % my_height) +print("He\'s %d pounds heavy." % my_weight) +print("He's got %s eyes and %s hair." % (my_eyes, my_hair)) print("Actually that's not too heavy") -print("His teeth are usually %s depending on the coffee." % teeth) +print("His teeth are usually %s depending on the coffee." % my_teeth) # this line is tricky, try to get it exactly right -print('If I add %d, %d and %d I get %d.' % (age, height, weight, age + height + weight)) - -# try more format characters -my_greeting = "Hello,\t" -my_first_name = 'Joseph' -my_last_name = 'Pan' -my_age = 24 -# Print 'Hello,\t'my name is Joseph Pan, and I'm 24 years old. -print("%rmy name is %s %s, and I'm %d years old." % (my_greeting, my_first_name, my_last_name, my_age)) - -# Try to write some variables that convert the inches and pounds to centimeters and kilos. -inches_per_centimeters = 2.54 -pounds_per_kilo = 0.45359237 - -print("He's %f centimeters tall." % (height * inches_per_centimeters)) -print("He's %f kilos heavy." % (weight * pounds_per_kilo)) +print('If I add %d, %d and %d I get %d.' % (my_age, my_height, my_weight, my_age + my_height + my_weight)) \ No newline at end of file diff --git a/Python3/ex06-studydrills.py b/Python3/ex06-studydrills.py new file mode 100644 index 0000000..b948365 --- /dev/null +++ b/Python3/ex06-studydrills.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +# ex6: String and Text + +# Assign the string with 10 replacing the formatting character to variable 'x' +x = "There are %d types of people." % 10 + +# Assign the string with "binary" to variable 'binary' +binary = "binary" + +# Assign the string with "don't" to variable 'do_not' +do_not = "don't" + +# Assign the string with 'binary' and 'do_not' replacing the +# formatting character to variable 'y' +y = "Those who know %s and those who %s." % (binary, do_not) # Two strings inside of a string + +# Print "There are 10 types of people." +print(x) + +# Print "Those who know binary and those who don't." +print(y) + +# Print "I said 'There are 10 types of people.'" +print("I said %r." % x) # One string inside of a string + +# Print "I also said: 'Those who know binary and those who don't.'." +print("I also said: '%s'." % y) # One string inside of a string + +# Assign boolean False to variable 'hilarious' +hilarious = False + +# Assign the string with an unevaluated formatting character to 'joke_evaluation' +joke_evaluation = "Isn't that joke so funny?! %r" + +# Print "Isn't that joke so funny?! False" +print(joke_evaluation % hilarious) # One string inside of a string + +# Assign string to 'w' +w = "This is the left side of..." + +# Assign string to 'e' +e = "a string with a right side." + +# Print "This is the left side of...a string with a right side." +print(w + e) # Concatenate two strings with + operator diff --git a/Python3/ex06.py b/Python3/ex06.py index 844ea52..675812f 100755 --- a/Python3/ex06.py +++ b/Python3/ex06.py @@ -1,46 +1,24 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex6: String and Text -# Assign the string with 10 replacing the formatting character to variable 'x' x = "There are %d types of people." % 10 - -# Assign the string with "binary" to variable 'binary' binary = "binary" - -# Assign the string with "don't" to variable 'do_not' do_not = "don't" - -# Assign the string with 'binary' and 'do_not' replacing the -# formatting character to variable 'y' y = "Those who know %s and those who %s." % (binary, do_not) # Two strings inside of a string -# Print "There are 10 types of people." print(x) - -# Print "Those who know binary and those who don't." print(y) -# Print "I said 'There are 10 types of people.'" print("I said %r." % x) # One string inside of a string - -# Print "I also said: 'Those who know binary and those who don't.'." print("I also said: '%s'." % y) # One string inside of a string -# Assign boolean False to variable 'hilarious' hilarious = False - -# Assign the string with an unevaluated formatting character to 'joke_evaluation' joke_evaluation = "Isn't that joke so funny?! %r" -# Print "Isn't that joke so funny?! False" print(joke_evaluation % hilarious) # One string inside of a string -# Assign string to 'w' w = "This is the left side of..." - -# Assign string to 'e' e = "a string with a right side." -# Print "This is the left side of...a string with a right side." -print(w + e) # Concatenate two strings with + operator +print(w + e) diff --git a/Python3/ex07-studydrills.py b/Python3/ex07-studydrills.py new file mode 100644 index 0000000..1cc466a --- /dev/null +++ b/Python3/ex07-studydrills.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +# ex07: More Printing + +# Print "Mary had a little lamb." +print("Mary had a little lamb.") + +# Print "Its fleece was white as snow." +print("Its fleece was white as %s." % 'snow') # one string inside of another + +# Print "And everywhere that Mary went." +print("And everywhere that Mary went.") + +# Print ".........." +print("." * 10) # what'd that do? - "*" operator for strings is used to repeat the same characters for certain times + +# Assign string value for each variable +end1 = "C" +end2 = "h" +end3 = "e" +end4 = "e" +end5 = "s" +end6 = "e" +end7 = "B" +end8 = "u" +end9 = "r" +end10 = "g" +end11 = "e" +end12 = "r" +end10 = "g" +end11 = "e" +end12 = "r" + +# watch the first statement at the end. try removing it to see what happens +print(end1 + end2 + end3 + end4 + end5 + end6, end = " ") +print(end7 + end8 + end9 + end10 + end11 + end12) \ No newline at end of file diff --git a/Python3/ex07.py b/Python3/ex07.py index 8791e52..f92dc31 100755 --- a/Python3/ex07.py +++ b/Python3/ex07.py @@ -1,36 +1,24 @@ -#!/bin/python3 +#!/usr/bin/env python3 -# ex7: More Printing +# ex07: More Printing -# Print "Mary had a little lamb" -print("Mary had a little lamb") - -# Print "Its fleece was white as snow. -print("Its fleece was white as %s." % 'snow') # one string inside of another - -# Print "And everywhere that Mary went." +print('Mary had a little lamb') +print('Its fleece was white as %s' % 'snow') print("And everywhere that Mary went.") +print("." * 10) -# Print ".........." -print("." * 10) # what'd that do? - "*" operator for strings is used to repeat the same characters for certain times - -# Assign string value for each variable -end1 = "C" -end2 = "h" -end3 = "e" -end4 = "e" -end5 = "s" -end6 = "e" -end7 = "B" -end8 = "u" -end9 = "r" -end10 = "g" -end11 = "e" -end12 = "r" -end10 = "g" -end11 = "e" -end12 = "r" +end1 = 'C' +end2 = 'h' +end3 = 'e' +end4 = 'e' +end5 = 's' +end6 = 'e' +end7 = 'b' +end8 = 'u' +end9 = 'r' +end10 = 'g' +end11 = 'e' +end12 = 'r' -# watch the first statement at the end. try removing it to see what happens -print(end1 + end2 + end3 + end4 + end5 + end6, end = " ") -print(end7 + end8 + end9 + end10 + end11 + end12) +print(end1 + end2 + end3 + end4 + end5 + end6, end = '') +print(end7 + end8 + end9 + end10 + end11 + end12) \ No newline at end of file diff --git a/Python3/ex08.py b/Python3/ex08.py index 3059898..12b32a1 100755 --- a/Python3/ex08.py +++ b/Python3/ex08.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex8: Printing, Printing @@ -13,4 +13,4 @@ "That you could type up right.", "But it didn't sing.", # This line contains a apostrophe "So I said goodnight." - )) + )) \ No newline at end of file diff --git a/Python3/ex09.py b/Python3/ex09.py index c4d676a..d1f8889 100755 --- a/Python3/ex09.py +++ b/Python3/ex09.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex9: Printing, Printing, Printing @@ -9,11 +9,10 @@ print("Here are the days: ", days) print("Here are the months: ", months) -print("I said 'Here are the months: %r'" % months) print(""" There's something going on here. With the three double-quotes. We'll be able to type as much as we like. Even 4 lines if we want, or 5, or 6. -""") +""") \ No newline at end of file diff --git a/Python3/ex10-studydrills.py b/Python3/ex10-studydrills.py new file mode 100644 index 0000000..aca39f9 --- /dev/null +++ b/Python3/ex10-studydrills.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +# ex10: What Was That? + +tabby_cat = "\tI'm tabbed in." +persian_cat = "I'm split\non a line." +backslash_cat = "I'm \\ a \\ cat." + +fat_cat = ''' +I'll do a list: +\t* Cat food +\t* Fishies +\t* Catnip\n\t* Grass +''' + +print(tabby_cat) +print(persian_cat) +print(backslash_cat) +print(fat_cat) + +# Assign string value for each variable +intro = "I'll print a week:" +mon = "Mon" +tue = "Tue" +wed = "Wed" +thu = "Thu" +fri = "Fri" +sat = "Sat" +sun = "Sun" + +print("%s\n%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (intro, mon, tue, wed, thu, fri, sat, sun)) + +print("%r" % intro) +print("%r" % "She said \"I'll print a week\"") + +print("%s" % intro) +print("%s" % "She said \"I'll print a week\"") \ No newline at end of file diff --git a/Python3/ex10.py b/Python3/ex10.py index 162ed84..d23fb35 100755 --- a/Python3/ex10.py +++ b/Python3/ex10.py @@ -1,39 +1,19 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex10: What Was That? tabby_cat = "\tI'm tabbed in." persian_cat = "I'm split\non a line." backslash_cat = "I'm \\ a \\ cat." -test = "I will insert a \newline haha." fat_cat = ''' I'll do a list: \t* Cat food \t* Fishies -\t* Catnip \n\t* Grass +\t* Catnip\n\t* Grass ''' print(tabby_cat) print(persian_cat) print(backslash_cat) -print(fat_cat) - -# Assign string value for each variable -intro = "I'll print a week:" -mon = "Mon" -tue = "Tue" -wed = "Wed" -thu = "Thu" -fri = "Fri" -sat = "Sat" -sun = "Sun" - -print("%s\n%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (intro, mon, tue, wed, thu, fri, sat, sun)) - -print("%r" % intro) -print("%r" % "She said \"I'll print a week\"") - -print("%s" % intro) -print("%s" % "She said \"I'll print a week\"") - +print(fat_cat) \ No newline at end of file diff --git a/Python3/ex11-studydrills.py b/Python3/ex11-studydrills.py new file mode 100644 index 0000000..41b1212 --- /dev/null +++ b/Python3/ex11-studydrills.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +# ex11: Asking Questions + +# raw_input() was renamed to input() in Python v3.x, +# and the old input() is gone, but you can emulate it with eval(input()) + +print("How old are you?", end=" ") +age = input() +print("How tall are you?", end=" ") +height = input() +print("How much do you weight", end=" ") +weight = input() + +print("So, you're %r old, %r tall and %r heavy." % (age, height, weight)) + +print("Enter a integer: ", end=" ") +num = int(eval(input())) # won't work with int(raw_input)), with eval(input()) it would work +print("The number you've input is: %d" % num) +print("Enter a name: ", end=" ") +name = input() +print("What's %s's age?" % name, end=" ") +age = eval(input()) +print("What's %s's height?" % name, end=" ") +height = eval(input()) +print("What's %s's weight?" % name, end=" ") +weight = eval(input()) +print("What's the color of %s's eyes?" % name, end=" ") +eyes = input() +print("What's the color of %s's teeth?" % name, end=" ") +teeth = input() +print("What's the color of %s's hair?" % name, end=" ") +hair = input() + +type(name) # the data type of name will be +type(age) # the data type of age will be + +print("Let's talk about %s" % name) +print("He's %d years old." % age) +print("He's %d inches tall." % height) +print("He's %d pounds heavy." % weight) +print("Actually that's not too heavy") +print("He's got %s eyes and %s hair." % (eyes, hair)) +print("His teeth are usually %s depending on the coffee." % (teeth)) + +print('If I add %d, %d and %d I get %d.' % (age, height, weight, age + height + weight)) \ No newline at end of file diff --git a/Python3/ex11.py b/Python3/ex11.py index c00e3cd..a9d377d 100755 --- a/Python3/ex11.py +++ b/Python3/ex11.py @@ -1,46 +1,12 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex11: Asking Questions -# raw_input() was renamed to input() in Python v3.x, -# and the old input() is gone, but you can emulate it with eval(input()) - -print("How old are you?", end=" ") -age = input() -print("How tall are you?", end=" ") +print("How old are you?") +age = input() +print("How tall are you?") height = input() -print("How much do you weight", end=" ") +print("How much do you weight") weight = input() -print("So, you're %r old, %r tall and %r heavy." % (age, height, weight)) - -print("Enter a integer: ", end=" ") -num = int(eval(input())) # won't work with int(raw_input)), with eval(input()) it would work -print("The number you've input is: %d" % num) -print("Enter a name: ", end=" ") -name = input() -print("What's %s's age?" % name, end=" ") -age = eval(input()) -print("What's %s's height?" % name, end=" ") -height = eval(input()) -print("What's %s's weight?" % name, end=" ") -weight = eval(input()) -print("What's the color of %s's eyes?" % name, end=" ") -eyes = input() -print("What's the color of %s's teeth?" % name, end=" ") -teeth = input() -print("What's the color of %s's hair?" % name, end=" ") -hair = input() - -type(name) # the data type of name will be -type(age) # the data type of age will be - -print("Let's talk about %s" % name) -print("He's %d years old." % age) -print("He's %d inches tall." % height) -print("He's %d pounds heavy." % weight) -print("Actually that's not too heavy") -print("He's got %s eyes and %s hair." % (eyes, hair)) -print("His teeth are usually %s depending on the coffee." % (teeth)) - -print('If I add %d, %d and %d I get %d.' % (age, height, weight, age + height + weight)) +print("So, you're {0} years old, {1} tall and {2} heavy".format(age,height,weight)) \ No newline at end of file diff --git a/Python3/ex12.py b/Python3/ex12.py index 7ff367f..11d4d8d 100755 --- a/Python3/ex12.py +++ b/Python3/ex12.py @@ -1,9 +1,9 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex12: Prompting People age = input("How old are you?") -height = input("How tall are you? ") -weight = input("How much do you weigh? ") +height = input("How tall are you?") +weight = input("How much do you weigh?") -print("So, you're %r old, %r tall and %r heavy." % (age, height, weight)) +print("So, you're %r old, %r tall and %r heavy." % (age, height, weight)) \ No newline at end of file diff --git a/Python3/ex13-less.py b/Python3/ex13-studydrills1.py old mode 100755 new mode 100644 similarity index 78% rename from Python3/ex13-less.py rename to Python3/ex13-studydrills1.py index 0566a91..b49ce06 --- a/Python3/ex13-less.py +++ b/Python3/ex13-studydrills1.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex13: Parameters, Unpacking, Variables # Write a script that has fewer arguments @@ -9,4 +9,4 @@ print("The script is called:", script) print("Your first name is:", first_name) -print("Your last name is:", last_name) +print("Your last name is:", last_name) \ No newline at end of file diff --git a/Python3/ex13-more.py b/Python3/ex13-studydrills2.py old mode 100755 new mode 100644 similarity index 80% rename from Python3/ex13-more.py rename to Python3/ex13-studydrills2.py index 6ebf367..3a17712 --- a/Python3/ex13-more.py +++ b/Python3/ex13-studydrills2.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex13: Parameters, Unpacking, Variables # Write a script that has more arguments. @@ -11,4 +11,4 @@ print("Your name is:", name) print("Your age is:", age) print("Your height is %d inches" % int(height)) -print("Your weight is %d pounds" % int(weight)) +print("Your weight is %d pounds" % int(weight)) \ No newline at end of file diff --git a/Python3/ex13-input.py b/Python3/ex13-studydrills3.py old mode 100755 new mode 100644 similarity index 89% rename from Python3/ex13-input.py rename to Python3/ex13-studydrills3.py index 5996291..9335d2e --- a/Python3/ex13-input.py +++ b/Python3/ex13-studydrills3.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex13: Parameters, Unpacking, Variables # Combine input with argv to make a script that gets more input from a user. @@ -9,4 +9,4 @@ middle_name = input("What's your middle name?") -print("Your full name is %s %s %s." % (first_name, middle_name, last_name)) +print("Your full name is %s %s %s." % (first_name, middle_name, last_name)) \ No newline at end of file diff --git a/Python3/ex13.py b/Python3/ex13.py index 83b0122..af1c2fa 100755 --- a/Python3/ex13.py +++ b/Python3/ex13.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex13: Parameters, Unpacking, Variables @@ -9,4 +9,4 @@ print("The script is called:", script) print("Your first variable is:", first) print("Your second variable is:", second) -print("Your third variable is:", third) +print("Your third variable is:", third) \ No newline at end of file diff --git a/Python3/ex14-studydrills.py b/Python3/ex14-studydrills.py new file mode 100644 index 0000000..09d6d4c --- /dev/null +++ b/Python3/ex14-studydrills.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +# ex14: Prompting and Passing + +from sys import argv + +# Add another argument and use it in your script +script, user_name, city = argv +# Change the prompt variable to something else +prompt = 'Please type the answer: ' + +print("Hi %s from %s, I'm the %s script." % (user_name, city, script)) +print("I'd like to ask you a few questions.") +print("Do you like me %s?" % user_name) +likes = input(prompt) + +print("What's the whether like today in %s?" % city) +weather = input(prompt) + +print("What kind of computer do you have?") +computer = input(prompt) + +print(""" +Alright, so you said %r about liking me. +The weather in your city is %s. +But I can't feel it because I'm a robot. +And you have a %r computer. Nice. +""" % (likes, weather, computer)) \ No newline at end of file diff --git a/Python3/ex14.py b/Python3/ex14.py index 8000afb..97b3dea 100755 --- a/Python3/ex14.py +++ b/Python3/ex14.py @@ -1,14 +1,11 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex14: Prompting and Passing from sys import argv -# Add another argument and use it in your script -script, user_name, city = argv -# prompt = '> ' -# Change the prompt variable to something else -prompt = 'Please type the answer: ' +script, user_name = argv +prompt = '> ' print("Hi %s from %s, I'm the %s script." % (user_name, city, script)) print("I'd like to ask you a few questions.") @@ -26,4 +23,4 @@ The weather in your city is %s. But I can't feel it because I'm a robot. And you have a %r computer. Nice. -""" % (likes, weather, computer)) +""" % (likes, weather, computer)) \ No newline at end of file diff --git a/Python3/ex15-studydrills.py b/Python3/ex15-studydrills.py new file mode 100644 index 0000000..c16d6ef --- /dev/null +++ b/Python3/ex15-studydrills.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +# ex15: Reading Files + +# import argv variables from sys submodule +from sys import argv + +# get the argv variables +script, filename = argv + +# open a file +txt = open(filename) + +# print file name +print("Here's your file %r: " % filename) +# print all the contents of the file +print(txt.read()) + +# close the file +txt.close() + +# prompt to type the file name again +print("Type the filename again:") +# input the new file name +file_again = input("> ") + +# open the new selected file +txt_again = open(file_again) + +# print the contents of the new selected file +print(txt_again.read()) + +# close the file +txt_again.close() \ No newline at end of file diff --git a/Python3/ex15.py b/Python3/ex15.py index 7f54fb6..f6ebb4b 100755 --- a/Python3/ex15.py +++ b/Python3/ex15.py @@ -1,34 +1,19 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex15: Reading Files -# import argv variables from sys submodule from sys import argv -# get the argv variables script, filename = argv -# open a file txt = open(filename) -# print file name print("Here's your file %r: " % filename) -# print all the contents of the file print(txt.read()) -# close the file -txt.close() - -# prompt to type the file name again print("Type the filename again:") -# input the new file name file_again = input("> ") -# open the new selected file txt_again = open(file_again) -# print the contents of the new selected file -print(txt_again.read()) - -# close the file -txt_again.close() +print(txt_again.read()) \ No newline at end of file diff --git a/Python3/ex16-studydrills.py b/Python3/ex16-studydrills.py new file mode 100644 index 0000000..df4f4c7 --- /dev/null +++ b/Python3/ex16-studydrills.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +# ex16: Reading and Writing Files + +from sys import argv + +script, filename = argv + +print("We're going to erase %r." % filename) +print("If you don't want that, hit CTRL-C (^C).") +print("If you do want that, hit RETURN.") + +input("?") + +print("Opening the file...") +# Open this file in 'write' mode +target = open(filename, 'w') + +print("Truncating the file. Goodbye!") +target.truncate() + +print("Now I'm going to ask you for three lines.") + +line1 = input("line 1: ") +line2 = input("line 2: ") +line3 = input("line 3: ") + +print("I'm going to write these to the file.") + +target.write("%s\n%s\n%s\n" % (line1, line2, line3)) + +print("And finally, we close it.") +target.close() \ No newline at end of file diff --git a/Python3/ex16_read.py b/Python3/ex16-studydrills2.py old mode 100755 new mode 100644 similarity index 86% rename from Python3/ex16_read.py rename to Python3/ex16-studydrills2.py index 9870f9d..a987768 --- a/Python3/ex16_read.py +++ b/Python3/ex16-studydrills2.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # A script similar to ex16 that uses read and argv to read the file @@ -10,4 +10,4 @@ target = open(filename) contents = target.read() print(contents) -target.close() +target.close() \ No newline at end of file diff --git a/Python3/ex16.py b/Python3/ex16.py index e704c3b..4239eb0 100755 --- a/Python3/ex16.py +++ b/Python3/ex16.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex16: Reading and Writing Files @@ -6,10 +6,6 @@ script, filename = argv -from sys import argv - -script, filename = argv - print("We're going to erase %r." % filename) print("If you don't want that, hit CTRL-C (^C).") print("If you do want that, hit RETURN.") @@ -17,11 +13,8 @@ input("?") print("Opening the file...") -# Open this file in 'write' mode target = open(filename, 'w') -# Not neccessary if opening the file with 'w' mode -# target.truncate() print("Truncating the file. Goodbye!") target.truncate() @@ -33,7 +26,12 @@ print("I'm going to write these to the file.") -target.write("%s\n%s\n%s\n" % (line1, line2, line3)) +target.write(line1) +target.write("\n") +target.write(line2) +target.write("\n") +target.write(line3) +target.write("\n") print("And finally, we close it.") -target.close() +target.close() \ No newline at end of file diff --git a/Python3/ex17-studydrills.py b/Python3/ex17-studydrills.py new file mode 100644 index 0000000..f4e490b --- /dev/null +++ b/Python3/ex17-studydrills.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python3 + +# ex17: More Files + +from sys import argv + +script, from_file, to_file = argv + +open(to_file, 'w').write(open(from_file).read()) \ No newline at end of file diff --git a/Python3/ex17.py b/Python3/ex17.py index 6afebc0..3e4173c 100755 --- a/Python3/ex17.py +++ b/Python3/ex17.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex17: More Files @@ -7,4 +7,22 @@ script, from_file, to_file = argv -open(to_file, 'w').write(open(from_file).read()) +print("Copying from %s to %s" % (from_file,to_file)) + +# we could do these two on one line too, how? +in_file = open(from_file) +indata = in_file.read() + +print("The input file is %d bytes long" % len(indata)) + +print("Does the output file exist? %r" % exists(to_file)) +print("Ready, hit RETURN to continue, CTRL-C to abort.") +input() + +out_file = open(to_file, 'w') +out_file.write(indata) + +print("Alright, all done") + +out_file.close() +in_file.close() \ No newline at end of file diff --git a/Python3/ex18.py b/Python3/ex18.py index 48a9045..131d347 100755 --- a/Python3/ex18.py +++ b/Python3/ex18.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex18: Names, Variables, Code, Functions @@ -19,8 +19,7 @@ def print_one(arg1): def print_none(): print("I got nothin'.") - print_two("Zed","Shaw") print_two_again("Zed","Shaw") print_one("First!") -print_none() +print_none() \ No newline at end of file diff --git a/Python3/ex19-studydrills.py b/Python3/ex19-studydrills.py new file mode 100644 index 0000000..7f3bc71 --- /dev/null +++ b/Python3/ex19-studydrills.py @@ -0,0 +1,145 @@ +#!/usr/bin/env python3 + +# ex19: Functions and Variables + +# Import argv variables from the sys module +from sys import argv + +# Assign the first and the second arguments to the two variables +script, input_file = argv + +# Define a function called print_call to print the whole contents of a +# file, with one file object as formal parameter +def print_all(f): + # print the file contents + print f.read() + +# Define a function called rewind to make the file reader go back to +# the first byte of the file, with one file object as formal parameter +def rewind(f): + # make the file reader go back to the first byte of the file + f.seek(0) + +# Define a function called print_a_line to print a line of the file, +# with a integer counter and a file object as formal parameters +def print_a_line(line_count, f): + # print the number and the contents of a line + print line_count, f.readline() + +# Open a file +current_file = open(input_file) + +# Print "First let's print the whole file:" +print "First let's print the whole file:\n" + +# call the print_all function to print the whole file +print_all(current_file) + +# Print "Now let's rewind, kind of like a tape." +print "Now let's rewind, kind of like a tape." + +# Call the rewind function to go back to the beginning of the file +rewind(current_file) + +# Now print three lines from the top of the file + +# Print "Let's print three lines:" +print "Let's print three lines:" + +# Set current line to 1 +current_line = 1 +# Print current line by calling print_a_line function +print_a_line(current_line, current_file) + +# Set current line to 2 by adding 1 +current_line = current_line + 1 +# Print current line by calling print_a_line function +print_a_line(current_file, current_file) + +# Set current line to 3 by adding 1 +current_line = current_line + 1 +# Print current line by calling print_a_line function +current_line(current_line, current_file) + +# Define a function named "cheese_and_crackers" +def cheese_and_crackers(cheese_count, boxes_of_crackers): + print("You have %d cheeses!" % cheese_count) + print("You have %d boxes of crackers!" % boxes_of_crackers) + print("Man that's enough for a party!") + print("Get a blanket.\n") + +# Print "We can just give the function numbers directly:" +print("We can just give the function numbers directly:") +cheese_and_crackers(20, 30) + +# Print "OR, we can use variables from our script:" +print("OR, we can use variables from our script:") +# assign 10 to a variable named amount_of_cheese +amount_of_cheese = 10 +# assign 50 to a variable named amount_of_crackers +amount_of_crackers = 50 + +# Call the function, with 2 variables as the actual parameters +cheese_and_crackers(amount_of_cheese, amount_of_crackers) + +# Print "We can even do math inside too:" +print("We can even do math inside too:") +# Call the function, with two math expression as the actual +# parameters. Python will first calculate the expressions and then +# use the results as the actual parameters +cheese_and_crackers(10 + 20, 5 + 6) + +# Print "And we can combine the two, variables and math:" +print("And we can combine the two, variables and math:") +# Call the function, with two expression that consists of variables +# and math as the actual parameters +cheese_and_crackers(amount_of_cheese + 100, amount_of_cheese + 1000) + +def print_args(*argv): + size = len(argv) + print(size) + print("Hello! Welcome to use %r!" % argv[0]) + if size > 1: + for i in range(1, size): + print("The param %d is %r" % (i, argv[i])) + return 0 + return -1 + +# 1. use numbers as actual parameters +print_args(10, 20, 30) + +# 2. use string and numbers as actual parameters +print_args("print_args", 10, 20) + +# 3. use strings as actual parameters +print_args("print_args", "Joseph", "Pan") + +# 4. use variables as actual parameters +first_name = "Joseph" +last_name = "Pan" +print_args("print_args", first_name, last_name) + +# 5. contain math expressions +print_args("print_args", 5*4, 2.0/5) + +# 6. more complicated calculations +print_args("print_args", '.'*10, '>'*3) + +# 7. more parameters +print_args("print_args", 10, 20, 30, 40, 50) + +# 8. tuples as parameters +nums1 = (10, 20, 30) +nums2 = (40, 50, 60) +print_args("print_args", nums1, nums2) + +# 9. more complicated types +nums3 = [70, 80, 90] +set1 = {"apple", "banana", "orange"} +dict1 = {'id': '0001', 'name': first_name+" "+last_name} +str1 = "Wow, so complicated!" +print_args("print args", nums1, nums2, nums3, set1, dict1, str1) + +# 10. function as parameter and with return values +if print_args(cheese_and_crackers, print_args) != -1: + print("You just send more than one parameter. Great!") \ No newline at end of file diff --git a/Python3/ex19.py b/Python3/ex19.py index c51d970..89d1697 100755 --- a/Python3/ex19.py +++ b/Python3/ex19.py @@ -1,155 +1,24 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex19: Functions and Variables -# ex20: Functions and Files - -# Import argv variables from the sys module -from sys import argv - -# Assign the first and the second arguments to the two variables -script, input_file = argv - - -# Define a function called print_call to print the whole contents of a -# file, with one file object as formal parameter -def print_all(f): - # print the file contents - print f.read() - - -# Define a function called rewind to make the file reader go back to -# the first byte of the file, with one file object as formal parameter -def rewind(f): - # make the file reader go back to the first byte of the file - f.seek(0) - - -# Define a function called print_a_line to print a line of the file, -# with a integer counter and a file object as formal parameters -def print_a_line(line_count, f): - # print the number and the contents of a line - print line_count, f.readline() - - -# Open a file -current_file = open(input_file) - -# Print "First let's print the whole file:" -print "First let's print the whole file:\n" - -# call the print_all function to print the whole file -print_all(current_file) - -# Print "Now let's rewind, kind of like a tape." -print "Now let's rewind, kind of like a tape." - -# Call the rewind function to go back to the beginning of the file -rewind(current_file) - -# Now print three lines from the top of the file - -# Print "Let's print three lines:" -print "Let's print three lines:" - -# Set current line to 1 -current_line = 1 -# Print current line by calling print_a_line function -print_a_line(current_line, current_file) - -# Set current line to 2 by adding 1 -current_line = current_line + 1 -# Print current line by calling print_a_line function -print_a_line(current_file, current_file) - -# Set current line to 3 by adding 1 -current_line = current_line + 1 -# Print current line by calling print_a_line function -current_line(current_line, current_file) - - -# Define a function named "cheese_and_crackers" def cheese_and_crackers(cheese_count, boxes_of_crackers): print("You have %d cheeses!" % cheese_count) print("You have %d boxes of crackers!" % boxes_of_crackers) print("Man that's enough for a party!") print("Get a blanket.\n") - -# Print "We can just give the function numbers directly:" print("We can just give the function numbers directly:") cheese_and_crackers(20, 30) -# Print "OR, we can use variables from our script:" print("OR, we can use variables from our script:") -# assign 10 to a variable named amount_of_cheese amount_of_cheese = 10 -# assign 50 to a variable named amount_of_crackers amount_of_crackers = 50 -# Call the function, with 2 variables as the actual parameters cheese_and_crackers(amount_of_cheese, amount_of_crackers) -# Print "We can even do math inside too:" print("We can even do math inside too:") -# Call the function, with two math expression as the actual -# parameters. Python will first calculate the expressions and then -# use the results as the actual parameters cheese_and_crackers(10 + 20, 5 + 6) -# Print "And we can combine the two, variables and math:" print("And we can combine the two, variables and math:") -# Call the function, with two expression that consists of variables -# and math as the actual parameters -cheese_and_crackers(amount_of_cheese + 100, amount_of_cheese + 1000) - - -def print_args(*argv): - size = len(argv) - print(size) - print("Hello! Welcome to use %r!" % argv[0]) - if size > 1: - for i in range(1, size): - print("The param %d is %r" % (i, argv[i])) - return 0 - return -1 - - -# 1. use numbers as actual parameters -print_args(10, 20, 30) - -# 2. use string and numbers as actual parameters -print_args("print_args", 10, 20) - -# 3. use strings as actual parameters -print_args("print_args", "Joseph", "Pan") - -# 4. use variables as actual parameters -first_name = "Joseph" -last_name = "Pan" -print_args("print_args", first_name, last_name) - -# 5. contain math expressions -print_args("print_args", 5*4, 2.0/5) - -# 6. more complicated calculations -print_args("print_args", '.'*10, '>'*3) - -# 7. more parameters -print_args("print_args", 10, 20, 30, 40, 50) - -# 8. tuples as parameters -nums1 = (10, 20, 30) -nums2 = (40, 50, 60) -print_args("print_args", nums1, nums2) - -# 9. more complicated types -nums3 = [70, 80, 90] -set1 = {"apple", "banana", "orange"} -dict1 = {'id': '0001', 'name': first_name+" "+last_name} -str1 = "Wow, so complicated!" -print_args("print args", nums1, nums2, nums3, set1, dict1, str1) - -# 10. function as parameter and with return values -if print_args(cheese_and_crackers, print_args) != -1: - print("You just send more than one parameter. Great!") +cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) \ No newline at end of file diff --git a/Python3/ex20-studydrills.py b/Python3/ex20-studydrills.py new file mode 100644 index 0000000..28daa10 --- /dev/null +++ b/Python3/ex20-studydrills.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 + +# ex20: Functions and Files + +# Import argv variables from the sys module +from sys import argv + +# Assign the first and the second arguments to the two variables +script, input_file = argv + +# Define a function called print_call to print the whole contents of a +# file, with one file object as formal parameter +def print_all(f): + # print the file contents + print(f.read()) + +# Define a function called rewind to make the file reader go back to +# the first byte of the file, with one file object as formal parameter +def rewind(f): + # make the file reader go back to the first byte of the file + f.seek(0) + +# Define a function called print_a_line to print a line of the file, +# with a integer counter and a file object as formal parameters +def print_a_line(line_count, f): + # Test whether two variables are carrying the same value + print("line_count equal to current_line?:", (line_count == current_line)) + # print the number and the contents of a line + print(line_count, f.readline()) + +# Open a file +current_file = open(input_file) + +# Print "First let's print the whole file:" +print("First let's print the whole file:\n") + +# call the print_all function to print the whole file +print_all(current_file) + +# Print "Now let's rewind, kind of like a tape." +print("Now let's rewind, kind of like a tape.") + +# Call the rewind function to go back to the beginning of the file +rewind(current_file) + +# Now print three lines from the top of the file + +# Print "Let's print three lines:" +print("Let's print three lines:") + +# Set current line to 1 +current_line = 1 +# Print current line by calling print_a_line function +print_a_line(current_line, current_file) + +# Set current line to 2 by adding 1 +current_line += 1 +# Print current line by calling print_a_line function +print_a_line(current_line, current_file) + +# Set current line to 3 by adding 1 +current_line += 1 +# Print current line by calling print_a_line function +print_a_line(current_line, current_file) diff --git a/Python3/ex20.py b/Python3/ex20.py index 701c6eb..48abb39 100755 --- a/Python3/ex20.py +++ b/Python3/ex20.py @@ -1,68 +1,37 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex20: Functions and Files -# Import argv variables from the sys module from sys import argv -# Assign the first and the second arguments to the two variables script, input_file = argv - -# Define a function called print_call to print the whole contents of a -# file, with one file object as formal parameter def print_all(f): - # print the file contents print(f.read()) - -# Define a function called rewind to make the file reader go back to -# the first byte of the file, with one file object as formal parameter def rewind(f): - # make the file reader go back to the first byte of the file f.seek(0) - -# Define a function called print_a_line to print a line of the file, -# with a integer counter and a file object as formal parameters def print_a_line(line_count, f): - # Test whether two variables are carrying the same value - print("line_count equal to current_line?:", (line_count == current_line)) - # print the number and the contents of a line print(line_count, f.readline()) - -# Open a file current_file = open(input_file) -# Print "First let's print the whole file:" print("First let's print the whole file:\n") -# call the print_all function to print the whole file print_all(current_file) -# Print "Now let's rewind, kind of like a tape." print("Now let's rewind, kind of like a tape.") -# Call the rewind function to go back to the beginning of the file rewind(current_file) -# Now print three lines from the top of the file - -# Print "Let's print three lines:" print("Let's print three lines:") -# Set current line to 1 current_line = 1 -# Print current line by calling print_a_line function print_a_line(current_line, current_file) -# Set current line to 2 by adding 1 -current_line += 1 -# Print current line by calling print_a_line function +current_line = current_line + 1 print_a_line(current_line, current_file) -# Set current line to 3 by adding 1 -current_line += 1 -# Print current line by calling print_a_line function -print_a_line(current_line, current_file) +current_line = current_line + 1 +print_a_line(current_line, current_file) \ No newline at end of file diff --git a/Python3/ex21-studydrills.py b/Python3/ex21-studydrills.py new file mode 100644 index 0000000..7495cce --- /dev/null +++ b/Python3/ex21-studydrills.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 + +# ex21: Functions Can Return Something + +def add(a, b): + print("ADDING %d + %d" % (a, b)) + return a + b + +def subtract(a, b): + print("SUBTRACTING %d - %d" % (a, b)) + return a - b + +def multiply(a, b): + print("MULTIPLYING %d * %d" % (a, b)) + return a * b + +def divide(a, b): + print("DIVIDING %d / %d" % (a, b)) + return a / b + +# my function to test return +def isequal(a, b): + print("Is %r equal to %r? - " % (a, b), end="") + return (a == b) + +print("Let's do some math with just functions!") + +age = add(30, 5) +height = subtract(78, 4) +weight = multiply(92, 2) +iq = divide(100, 2) + +print("Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)) + + +# A puzzle for the extra credit, type it in anyway. +print("Here is a puzzle.") + +# switch the order of multiply and divide +what = add(age, subtract(height, divide(weight, multiply(iq, 2)))) + +print("That becomes: ", what, "Can you do it by hand?") + +# test the return value of isequal() +num1 = 40 +num2 = 50 +num3 = 50 + +print(isequal(num1, num2)) +print(isequal(num2, num3)) + + +# A new puzzle. +print("Here is a new puzzle.") + +# write a simple formula and use the function again +uplen = 50 +downlen = 100 +height = 80 +what_again = divide(multiply(height, add(uplen, downlen)), 2) + +print("That become: ", what_again, "Bazinga!") \ No newline at end of file diff --git a/Python3/ex21.py b/Python3/ex21.py index 16f3828..a7f8c2f 100755 --- a/Python3/ex21.py +++ b/Python3/ex21.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex21: Functions Can Return Something @@ -6,28 +6,18 @@ def add(a, b): print("ADDING %d + %d" % (a, b)) return a + b - def subtract(a, b): print("SUBTRACTING %d - %d" % (a, b)) return a - b - def multiply(a, b): print("MULTIPLYING %d * %d" % (a, b)) return a * b - def divide(a, b): print("DIVIDING %d / %d" % (a, b)) return a / b - -# my function to test return -def isequal(a, b): - print("Is %r equal to %r? - " % (a, b), end="") - return (a == b) - - print("Let's do some math with just functions!") age = add(30, 5) @@ -41,28 +31,6 @@ def isequal(a, b): # A puzzle for the extra credit, type it in anyway. print("Here is a puzzle.") -# switch the order of multiply and divide what = add(age, subtract(height, divide(weight, multiply(iq, 2)))) -print("That becomes: ", what, "Can you do it by hand?") - - -# test the return value of isequal() -num1 = 40 -num2 = 50 -num3 = 50 - -print(isequal(num1, num2)) -print(isequal(num2, num3)) - - -# A new puzzle. -print("Here is a new puzzle.") - -# write a simple formula and use the function again -uplen = 50 -downlen = 100 -height = 80 -what_again = divide(multiply(height, add(uplen, downlen)), 2) - -print("That become: ", what_again, "Bazinga!") +print("That becomes: ", what, "Can you do it by hand?") \ No newline at end of file diff --git a/Python3/ex24-studydrills.py b/Python3/ex24-studydrills.py new file mode 100644 index 0000000..34b1497 --- /dev/null +++ b/Python3/ex24-studydrills.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +# ex24: More Practice + +print("Let's practice everything.") +print("You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.") + +poem = """ +\t The lovely world +with logic so firmly planted +cannot discern \n the needs of love +nor comprehend passion from intuition +and requires an explanation +\n\t\twhere there is none. +""" + +print("--------------------") +print(poem) +print("--------------------") + + +five = 10 - 2 + 3 - 6 +print("This should be five: %s" % five) + +def secret_formala(started): + jelly_beans = started * 500 + jars = jelly_beans / 1000 + crates = jars / 100 + return jelly_beans, jars, crates + + +start_point = 1000 +beans, jars, crates = secret_formala(start_point) + +print("With a starting point of: %d" % start_point) +# use the tuple as the parameters for the formatter +print("We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates)) + +start_point = start_point / 10 + +print("We can also do that this way:") +# call the function and use its return values as the parameters for the formatter +print("We'd have %d beans, %d jars, and %d crates." % secret_formala(start_point)) diff --git a/Python3/ex24.py b/Python3/ex24.py index 0ee67e9..e39c85a 100755 --- a/Python3/ex24.py +++ b/Python3/ex24.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex24: More Practice @@ -22,7 +22,6 @@ five = 10 - 2 + 3 - 6 print("This should be five: %s" % five) - def secret_formala(started): jelly_beans = started * 500 jars = jelly_beans / 1000 @@ -34,11 +33,9 @@ def secret_formala(started): beans, jars, crates = secret_formala(start_point) print("With a starting point of: %d" % start_point) -# use the tuple as the parameters for the formatter print("We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates)) start_point = start_point / 10 print("We can also do that this way:") -# call the function and use its return values as the parameters for the formatter print("We'd have %d beans, %d jars, and %d crates." % secret_formala(start_point)) diff --git a/Python3/ex25.py b/Python3/ex25.py index 8a954f0..6862505 100755 --- a/Python3/ex25.py +++ b/Python3/ex25.py @@ -1,46 +1,39 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex: even more practice - def break_words(stuff): """ This function will break up words for us. """ words = stuff.split(' ') return words - def sort_words(words): """ Sorts the words. """ return sorted(words) - def print_first_word(words): """ Prints the first word after popping it off. """ word = words.pop(0) print(word) - def print_last_word(words): """ Prints the last word after popping it off. """ word = words.pop(-1) print(word) - def sort_sentence(sentence): """ Takes in a full sentence and returns the sorted words. """ words = break_words(sentence) return sort_words(words) - def print_first_and_last(senence): """ Prints the first and last words of the sentences. """ words = break_words(senence) print_first_word(words) print_last_word(words) - def print_first_and_last_sorted(sentence): """ Sorts the words then prints the first and last one. """ words = sort_sentence(sentence) print_first_word(words) - print_last_word(words) + print_last_word(words) \ No newline at end of file diff --git a/Python3/ex26.py b/Python3/ex26.py index b7f889c..f05886c 100755 --- a/Python3/ex26.py +++ b/Python3/ex26.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 def break_words(stuff): """This function will break up words for us.""" @@ -116,4 +116,4 @@ def secret_formula(started): print_first_and_last(sentence) # bug: print_first_a_last_sorted(senence) -print_first_and_last_sorted(sentence) +print_first_and_last_sorted(sentence) \ No newline at end of file diff --git a/Python3/ex29-studydrills.py b/Python3/ex29-studydrills.py new file mode 100644 index 0000000..0331fc2 --- /dev/null +++ b/Python3/ex29-studydrills.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +# ex29: What If + +people = 20 +cats = 30 +dogs = 15 + +if people < cats: + print("Too many cats! The world is doomed!") + +if people > cats: + print("Not many cats! The world is saved!") + +if people < dogs: + print("The world is drooled on!") + +if people > dogs: + print("The world is dry!") + +dogs += 5 + +if people >= dogs: + print("People are greater than or equal to dogs.") + +if people <= dogs: + print("People are less than or equal to dogs.") + +if people == dogs: + print("People are dogs.") + +dogs += 5 + +if (dogs < cats) and (people < cats): + print("Cats are more than people and dogs. People are scared by cats!") + +if (dogs < cats) and not (people < cats): + print("Cats are more than dogs. Mice are living a hard life!") + +if (dogs == cats) or (cats < 10): + print("Cats are fighting against dogs! Mice are happy!") + +if cat != 0: + print("Cats are still exist. Mice cannot be too crazy.") \ No newline at end of file diff --git a/Python3/ex29.py b/Python3/ex29.py index a4a6297..7474564 100755 --- a/Python3/ex29.py +++ b/Python3/ex29.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex29: What If @@ -6,7 +6,6 @@ cats = 30 dogs = 15 - if people < cats: print("Too many cats! The world is doomed!") @@ -28,27 +27,4 @@ print("People are less than or equal to dogs.") if people == dogs: - print("People are dogs.") - -dogs += 5 - -if people >= dogs: - print("People are greater than or equal to dogs.") - -if people <= dogs: - print("People are less than or equal to dogs.") - -if people == dogs: - print("People are dogs.") - -if (dogs < cats) and (people < cats): - print("Cats are more than people and dogs. People are scared by cats!") - -if (dogs < cats) and not (people < cats): - print("Cats are more than dogs. Mice are living a hard life!") - -if (dogs == cats) or (cats < 10): - print("Cats are fighting against dogs! Mice are happy!") - -if cat != 0: - print("Cats are still exist. Mice cannot be too crazy.") + print("People are dogs.") \ No newline at end of file diff --git a/Python3/ex30-studydrills.py b/Python3/ex30-studydrills.py new file mode 100644 index 0000000..a59ba71 --- /dev/null +++ b/Python3/ex30-studydrills.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 + +# ex30: Else and If + +# assign 30 to variable people +people = 30 +# assign 40 to variable cars +cars = 40 +# assign 15 to variable buses +buses = 15 + +# if cars are more than people +if cars > people: + # print "We should take the cars." + print("We should take the cars.") +# if cars are less than people +elif cars < people: + # print "We should not take the cars" + print("We should not take the cars") +# if cars are equal to people +else: + print("We can't decide.") + +# if buses are more than cars +if buses > cars: + # print "That's too many buses." + print("That's too many buses.") +# if buses are less than cars +elif buses < cars: + # print "Maybe we could take the buses." + print("Maybe we could take the buses.") +# if buses are equal to cars +else: + # print "We still can't decide." + print("We still can't decide.") + +# if people are more than buses +if people > buses: + # print "Alright, let's just take the buses." + print("Alright, let's just take the buses.") +# if people are less than buses +else: + # print "Fine, let's stay home then." + print("Fine, let's stay home then.") + +# if people are more than cars but less than buses +if people > cars and people < buses: + # print "There are many Busses but few cars." + print("There are many Busses but few cars.") +# if people are less than cars but more than buses +elif people < cars and people > buses: + # print "There are many cars but few busses." + print("There are many cars but few busses.") +# if people are more than cars and buses +elif people > cars and people > buses: + # print "There are few busses and cars." + print("There are few busses and cars.") +# if people are less than cars and buses +else: + # print "There are many busses and cars." + print("There are many busses and cars.") \ No newline at end of file diff --git a/Python3/ex30.py b/Python3/ex30.py index 8e06352..3724a3f 100755 --- a/Python3/ex30.py +++ b/Python3/ex30.py @@ -1,61 +1,26 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex30: Else and If -# assign 30 to variable people people = 30 -# assign 40 to variable cars cars = 40 -# assign 15 to variable buses buses = 15 -# if cars are more than people if cars > people: - # print "We should take the cars." print("We should take the cars.") -# if cars are less than people elif cars < people: - # print "We should not take the cars" print("We should not take the cars") -# if cars are equal to people else: print("We can't decide.") -# if buses are more than cars if buses > cars: - # print "That's too many buses." print("That's too many buses.") -# if buses are less than cars elif buses < cars: - # print "Maybe we could take the buses." print("Maybe we could take the buses.") -# if buses are equal to cars else: - # print "We still can't decide." print("We still can't decide.") -# if people are more than buses if people > buses: - # print "Alright, let's just take the buses." print("Alright, let's just take the buses.") -# if people are less than buses else: - # print "Fine, let's stay home then." - print("Fine, let's stay home then.") - -# if people are more than cars but less than buses -if people > cars and people < buses: - # print "There are many Busses but few cars." - print("There are many Busses but few cars.") -# if people are less than cars but more than buses -elif people < cars and people > buses: - # print "There are many cars but few busses." - print("There are many cars but few busses.") -# if people are more than cars and buses -elif people > cars and people > buses: - # print "There are few busses and cars." - print("There are few busses and cars.") -# if people are less than cars and buses -else: - # print "There are many busses and cars." - print("There are many busses and cars.") + print("Fine, let's stay home then.") \ No newline at end of file diff --git a/Python3/ex31-studydrills.py b/Python3/ex31-studydrills.py new file mode 100644 index 0000000..ffd70cf --- /dev/null +++ b/Python3/ex31-studydrills.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +# ex31: Making Decisions + +print("You enter a dark room with two doors. Do you go through door #1 or door #2?") + +door = input("> ") + +if door == "1": + print("There's a giant bear here eating a cheese cake. What do you do?") + print("1. Take the cake.") + print("2. Scream at the bear.") + + bear = input("> ") + + if bear == "1": + print("The bear eats your face off. Good job!") + elif bear == "2": + print("The bear eats your legs off. Good job!") + else: + print("Well, doing %s is probably better. Bear runs away." % bear) + +elif door == "2": + print("You stare into the endless abyss at Cthulhu's retina.") + print("1. Blueberries.") + print("2. Yellow jacket clothespins.") + print("3. Understanding revolvers yelling melodies.") + + insanity = input("> ") + + if insanity == "1" or insanity == "2": + print("Your body survives powered by a mind of jello. Good job!") + else: + print("The insanity rots you ") + +elif door == "3": + print("You are asked to select one pill from two and take it. One is red, and the other is blue.") + print("1. take the red one.") + print("2. take the blue one.") + + pill = input("> ") + + if pill == "1": + print("You wake up and found this is just a ridiculous dream. Good job!") + elif pill == "2": + print("It's poisonous and you died.") + else: + print("The man got mad and killed you.") + +else: + print("You wake up and found this is just a ridiculous dream.") + print("However you feel a great pity haven't entered any room and found out what it will happens!") \ No newline at end of file diff --git a/Python3/ex31.py b/Python3/ex31.py index 60ffc6a..0189cad 100755 --- a/Python3/ex31.py +++ b/Python3/ex31.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex31: Making Decisions @@ -18,36 +18,20 @@ elif bear == "2": print("The bear eats your legs off. Good job!") else: - print("Well, doing %s is probably better. Bear runs away." % bear) - + print("Well, doing {} is probably better. Bear runs away.".format(bear)) + elif door == "2": print("You stare into the endless abyss at Cthulhu's retina.") print("1. Blueberries.") print("2. Yellow jacket clothespins.") print("3. Understanding revolvers yelling melodies.") - + insanity = input("> ") - + if insanity == "1" or insanity == "2": - print("Your body survives powered by a mind of jello. Good job!") - else: - print("The insanity rots you ") - -elif door == "3": - print("You are asked to select one pill from two and take it. One is red, and the other is blue.") - print("1. take the red one.") - print("2. take the blue one.") - - pill = input("> ") - - if pill == "1": - print("You wake up and found this is just a ridiculous dream. Good job!") - elif pill == "2": - print("It's poisonous and you died.") + print("Your body survives powered by a mind of a jello. good job!") else: - print("The man got mad and killed you.") - + print("The insanity roots your eyes into a pool of muck. Good job!") else: - print("You wake up and found this is just a ridiculous dream.") - print("However you feel a great pity haven't entered any room and found out what it will happens!") + print("You stumble around and fall on a knife and die. Good job!") \ No newline at end of file diff --git a/Python3/ex32.py b/Python3/ex32.py index d655bee..c4b3bef 100755 --- a/Python3/ex32.py +++ b/Python3/ex32.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex32: Loops and Lists @@ -10,12 +10,10 @@ for number in the_count: print("This is count %d" % number) - # same as above for fruit in fruits: print("A fruit of type: %s" % fruit) - # also we can go through mixed lists too # notice we have to use %r since we don't know what's in it for i in change: @@ -29,4 +27,4 @@ # now we can print them out too for i in elements: - print("Element was: %d" % i) + print("Element was: %d" % i) \ No newline at end of file diff --git a/Python3/ex33-studydrills.py b/Python3/ex33-studydrills.py new file mode 100644 index 0000000..70641eb --- /dev/null +++ b/Python3/ex33-studydrills.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +# ex33: While Loops + +def createNumbers(max, step): + i = 0 + numbers = [] + for i in range(0, max, step): + print("At the top i is %d" % i) + numbers.append(i) + + print("Numbers now: ", numbers) + print("At the bottom i is %d" % i) + return numbers + +numbers = createNumbers(10, 2) + +print("The number: ") + +for num in numbers: + print(num) \ No newline at end of file diff --git a/Python3/ex33.py b/Python3/ex33.py index 7a89ae5..7fc7151 100755 --- a/Python3/ex33.py +++ b/Python3/ex33.py @@ -1,21 +1,19 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex33: While Loops -def createNumbers(max, step): - i = 0 - numbers = [] - for i in range(0, max, step): - print("At the top i is %d" % i) - numbers.append(i) +i = 0 +numbers = [] - print("Numbers now: ", numbers) - print("At the bottom i is %d" % i) - return numbers +while i < 6: + print("At the top i is {}".format(i)) + numbers.append(i) + + i = i + 1 + print("Numbers now: ", numbers) + print("At the bottom i is {}".format(i)) -numbers = createNumbers(10, 2) - -print("The number: ") +print("The numbers:") for num in numbers: - print(num) + print(num) \ No newline at end of file diff --git a/Python3/ex34.py b/Python3/ex34.py index d7b1b01..45e02e9 100755 --- a/Python3/ex34.py +++ b/Python3/ex34.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex34: Accessing Elements of Lists @@ -17,4 +17,4 @@ def printAnimal(index): print("The animal at %d is the %s animal and is a %s" % (index-1, num2en, animals[index-1])) for i in range(1, 7): - printAnimal(i) + printAnimal(i) \ No newline at end of file diff --git a/Python3/ex35.py b/Python3/ex35.py index fb24860..18f1615 100755 --- a/Python3/ex35.py +++ b/Python3/ex35.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex35: Branches and Functions @@ -82,5 +82,4 @@ def start(): else: dead("You stumble around the room until you starve.") - -start() +start() \ No newline at end of file diff --git a/Python3/ex37.py b/Python3/ex37.py index ad53056..56513fe 100755 --- a/Python3/ex37.py +++ b/Python3/ex37.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex37: Symbol Review @@ -221,4 +221,4 @@ def makeDouble(): num_list = [5, 10, 15, 'hello'] print('\nNow we multiples each elements in num_list with 2:') for num in num_list: - print(adder(num)) + print(adder(num)) \ No newline at end of file diff --git a/Python3/ex38.py b/Python3/ex38.py index e3e17e8..056d62f 100755 --- a/Python3/ex38.py +++ b/Python3/ex38.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex38: Doing Things To Lists @@ -23,4 +23,4 @@ print(stuff[-1]) # whoa! fancy print(stuff.pop()) print(' '.join(stuff)) # what? cool! -print('#'.join(stuff[3:5])) # super stellar! +print('#'.join(stuff[3:5])) # super stellar! \ No newline at end of file diff --git a/Python3/ex39-studydrills.py b/Python3/ex39-studydrills.py new file mode 100644 index 0000000..d0c33c0 --- /dev/null +++ b/Python3/ex39-studydrills.py @@ -0,0 +1,94 @@ +#!/bin/python3 + +# ex39: Dictionaries, Oh Lovely Dictionaries + +# create a mapping of state to abbreviation +states = { + 'Oregon': 'OR', + 'Florida': 'FL', + 'California': 'CA', + 'New York': 'NY', + 'Michigan': 'MI' +} + +# create a basic set of states and some cities in them +cities = { + 'CA': 'San Fransisco', + 'MI': 'Detroit', + 'FL': 'Jacksonville' + } + +# add some more cities +cities['NY'] = 'New York' +cities['OR'] = 'Portland' + +# print out some cities +print('-' * 10) +print("NY State has: ", cities['NY']) +print('OR State has: ', cities['OR']) + +# do it by using the state then cities dict +print('-' * 10) +print("Michigan has: ", cities[states['Michigan']]) +print("Florida has:", cities[states['Florida']]) + +# print every state abbreviation +print('-' * 10) +for state, abbrev in states.items(): + print("%s is abbreviated %s" % (state, abbrev)) + +# print every city in state +print('-' * 10) +for abbrev, city in cities.items(): + print("%s has the city %s" % (abbrev, city)) + +# now do both at the same time +print('-' * 10) +for state, abbrev in states.items(): + print("%s state is abbreviated %s and has city %s" % ( + states, abbrev, cities[abbrev])) + +print('-' * 10) +# safely get a abbreviation by state that might not be there +state = states.get('Texas', None) + +if not state: + print("Sorry, no Texas.") + +# get a city with a default value +city = cities.get("TX", 'Does Not Exist') +print("The city for the state 'TX' is %s" % city) + +# create a mapping of province to abbreviation +cn_province = { + '广东': '粤', + '湖南': '湘', + '四川': '川', + '云南': '滇', + } + +# create a basic set of provinces and some cities in them +cn_cities = { + '粤': '广州', + '湘': '长沙', + '川': '成都', + } + +# add some more data +cn_province['台湾'] = '台' + +cn_cities['滇'] = '昆明' +cn_cities['台'] = '高雄' + +print('-' * 10) +for prov, abbr in cn_province.items(): + print("%s省的缩写是%s" % (prov, abbr)) + +print('-' * 10) +cn_abbrevs = {values: keys for keys, values in cn_province.items()} +for abbrev, prov in cn_abbrevs.items(): + print("%s是%s省的缩写" % (abbrev, prov)) + +print('-' * 10) +for abbrev, city in cn_cities.items(): + print("%s市位于我国的%s省" % (city, cn_abbrevs[abbrev])) diff --git a/Python3/ex39.py b/Python3/ex39.py index d0c33c0..88c851a 100755 --- a/Python3/ex39.py +++ b/Python3/ex39.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex39: Dictionaries, Oh Lovely Dictionaries @@ -57,38 +57,4 @@ # get a city with a default value city = cities.get("TX", 'Does Not Exist') -print("The city for the state 'TX' is %s" % city) - -# create a mapping of province to abbreviation -cn_province = { - '广东': '粤', - '湖南': '湘', - '四川': '川', - '云南': '滇', - } - -# create a basic set of provinces and some cities in them -cn_cities = { - '粤': '广州', - '湘': '长沙', - '川': '成都', - } - -# add some more data -cn_province['台湾'] = '台' - -cn_cities['滇'] = '昆明' -cn_cities['台'] = '高雄' - -print('-' * 10) -for prov, abbr in cn_province.items(): - print("%s省的缩写是%s" % (prov, abbr)) - -print('-' * 10) -cn_abbrevs = {values: keys for keys, values in cn_province.items()} -for abbrev, prov in cn_abbrevs.items(): - print("%s是%s省的缩写" % (abbrev, prov)) - -print('-' * 10) -for abbrev, city in cn_cities.items(): - print("%s市位于我国的%s省" % (city, cn_abbrevs[abbrev])) +print("The city for the state 'TX' is %s" % city) \ No newline at end of file diff --git a/Python3/ex40-studydrills.py b/Python3/ex40-studydrills.py new file mode 100644 index 0000000..568a0e3 --- /dev/null +++ b/Python3/ex40-studydrills.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 + +# ex40: Modules, CLasses, and Objects + +class Song(object): + + def __init__(self, disk): + self.index = 0 + self.disk = disk + self.jump() + + def next(self): + ''' next song. ''' + self.index = (self.index + 1) % len(self.disk) + self.jump() + + def prev(self): + ''' prev song. ''' + self.index = (self.index - 1) % len(self.disk) + self.jump() + + def jump(self): + ''' jump to the song. ''' + self.lyrics = self.disk[self.index] + + def sing_me_a_song(self): + for line in self.lyrics: + print(line) + +# construct a disk +song1 = ["Happy birthday to you", +"I don't want to get sued", +"So I'll stop right there"] + +song2 = ["They rally around the family", +"With pockets full of shells" +] + +song3 = ["Never mind I find", +"Some one like you" +] + +disk = [song1, song2, song3] + +mycd = Song(disk) +mycd.sing_me_a_song() + +mycd.next() +mycd.sing_me_a_song() + +mycd.next() +mycd.sing_me_a_song() + +mycd.next() +mycd.sing_me_a_song() + +mycd.prev() +mycd.sing_me_a_song() + +mycd.prev() +mycd.sing_me_a_song() + +mycd.prev() +mycd.sing_me_a_song() + +mycd.prev() +mycd.sing_me_a_song() \ No newline at end of file diff --git a/Python3/ex40.py b/Python3/ex40.py index e1fb253..9df7882 100755 --- a/Python3/ex40.py +++ b/Python3/ex40.py @@ -1,65 +1,23 @@ -#!/bin/python3 +#!/usr/bin/env python3 -class Song(object): - - def __init__(self, disk): - self.index = 0 - self.disk = disk - self.jump() - - def next(self): - ''' next song. ''' - self.index = (self.index + 1) % len(self.disk) - self.jump() - - def prev(self): - ''' prev song. ''' - self.index = (self.index - 1) % len(self.disk) - self.jump() - - def jump(self): - ''' jump to the song. ''' - self.lyrics = self.disk[self.index] +# ex40: Modules, CLasses, and Objects +class Song(object): + + def __init__(self,lyrics): + self.lyrics = lyrics + def sing_me_a_song(self): for line in self.lyrics: print(line) -# construct a disk -song1 = ["Happy birthday to you", -"I don't want to get sued", -"So I'll stop right there"] - -song2 = ["They rally around the family", -"With pockets full of shells" -] - -song3 = ["Never mind I find", -"Some one like you" -] - -disk = [song1, song2, song3] - -mycd = Song(disk) -mycd.sing_me_a_song() - -mycd.next() -mycd.sing_me_a_song() - -mycd.next() -mycd.sing_me_a_song() - -mycd.next() -mycd.sing_me_a_song() - -mycd.prev() -mycd.sing_me_a_song() +happy_bday = Song(["Happy birthday to you", + "I don't want to get sued", + "So I'll stop right there"]) -mycd.prev() -mycd.sing_me_a_song() +bulls_on_parade = Song(["They rally around the family", + "With pockets full of shells"]) -mycd.prev() -mycd.sing_me_a_song() +happy_bday.sing_me_a_song() -mycd.prev() -mycd.sing_me_a_song() +bulls_on_parade.sing_me_a_song() \ No newline at end of file diff --git a/Python3/ex41.py b/Python3/ex41.py index 30ad6ba..0e57a80 100755 --- a/Python3/ex41.py +++ b/Python3/ex41.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 # ex41: Learning To Speak Object Oriented @@ -31,12 +31,10 @@ # load up the words from the website for word in urlopen(WORD_URL).readlines(): - WORDS.append(word.strip()) - + WORDS.append(word.strip().decode("utf-8")) def convert(snippet, phrase): - class_names = [w.capitalize() for w in - random.sample(WORDS, snippet.count("%%%"))] + class_names = [w.capitalize() for w in random.sample(WORDS, snippet.count("%%%"))] other_names = random.sample(WORDS, snippet.count("***")) results = [] param_names = [] @@ -64,7 +62,6 @@ def convert(snippet, phrase): return results - # keep going until they hit CTRL-D try: while True: @@ -82,4 +79,4 @@ def convert(snippet, phrase): input("> ") print("ANSWER: %s\n\n" % answer) except EOFError: - print("\nBye") + print("\nBye") \ No newline at end of file