initial commit

This commit is contained in:
booboy 2017-12-20 23:35:11 -06:00
parent 5a0dc3d7e6
commit 96c8c45570
12 changed files with 247 additions and 0 deletions

View file

@ -0,0 +1 @@
# python_programming_fundamentals

View file

@ -0,0 +1,18 @@
#!/usr/bin/env python
# python programming fundamentals
# page 33
# 1.19 excercise #1
# 1. write a program that asks the user to enter their name. Then it should print out the ASCII equivalent of each of the first four characters of your name. For instance, here is a sample run of the program below:
# Please enter your name: Kent
# K ASCII value is 75
# e ASCII value is 101
# n ASCII value is 110
# t ASCII value is 116
name = input('Enter your name: ')
print(name[0] + ' ' + 'ASCII value is ' + str(ord(name[0])))
print(name[1] + ' ' + 'ASCII value is ' + str(ord(name[1])))
print(name[2] + ' ' + 'ASCII value is ' + str(ord(name[2])))
print(name[3] + ' ' + 'ASCII value is ' + str(ord(name[3])))

View file

@ -0,0 +1,36 @@
#!/usr/bin/env python
# python programming fundamentals
# page 33
# 1.19 excercise #2
# 2. write a program that capitalizes the first four characters of a string by converting their characters to their ASCII equivlaent , then adding the necessary amount to capitlize them , and converting the integers back to characters. Print the capitalized string.
# the developers of ascii made the 6th bit be a 0 for the upper case and a 1 for lower case. here is an example
#A - 01000001 or 65 in dec
#a - 01100001 or 65(+32) in dec = 97
# so subtract or add 32 depending on if you wanna go lower --> upper or vice versa and it is the equivalent to flipping the 6th bit.
name = input('Enter your name in lowercase: ')
print('\n')
# store the integers in a list so we don't have to rewrite the same code
name_ascii_list_l= [int(ord(name[0])), int(ord(name[1])), int(ord(name[2])), int(ord(name[3]))]
print('lower case:')
# print lower case decimal ascii value
print(name[0] + ' ' + 'ASCII value is ' + str(name_ascii_list_l[0]))
print(name[1] + ' ' + 'ASCII value is ' + str(name_ascii_list_l[1]))
print(name[2] + ' ' + 'ASCII value is ' + str(name_ascii_list_l[2]))
print(name[3] + ' ' + 'ASCII value is ' + str(name_ascii_list_l[3]))
print('\n')
# now convert to upper case. again we store values in a list.
name_ascii_list_u = [name_ascii_list_l[0] - 32, name_ascii_list_l[1] - 32, name_ascii_list_l[2] - 32, name_ascii_list_l[3] - 32]
print ('upper case:')
print(chr(name_ascii_list_u[0]) + ' ' + 'ASCII value is ' + str(name_ascii_list_u[0]))
print(chr(name_ascii_list_u[1]) + ' ' + 'ASCII value is ' + str(name_ascii_list_u[1]))
print(chr(name_ascii_list_u[2]) + ' ' + 'ASCII value is ' + str(name_ascii_list_u[2]))
print(chr(name_ascii_list_u[3]) + ' ' + 'ASCII value is ' + str(name_ascii_list_u[3]))

View file

@ -0,0 +1,16 @@
#!/usr/bin/env python
# python programming fundamentals
# page 34
# 3. you can keep track of your car's miles per gallon if you keep track of how many miles you drive your car on a tank of gas and you always fill up your tank when getting gas
# write a program that asks the user to enter the number of miles you drove your car and the numbe rof gallons of gas you put in your car and then prints the miles per gallon you
# got on that tank of gas.
miles = input('How many miles did you drive your car? ')
gallons = input('How much gas did you put into your car? ')
# miles drive / gallons used
mpg = int(miles) / int(gallons)
print('Here is the total mpg based on those numbers:' + str(mpg))

View file

@ -0,0 +1,17 @@
#!/usr/bin/env python
# ex4.py
# python programming fundamentals - page34
# 4. Write a program that converts US dollars to a Foreign Currency. You can do this by finding the exchange rate on the internet and then prompting for the exchange rate in your program. It should look exactly like this:
# What is the amount of US Dollars you wish to convert? 31.67
# What is the current exchange rate
# (1 US Dollar equals what in the Foreign Currency)? 0.9825
# The amount in the Foreign Currency is $31.12
usd = input('What is the amount of US Dollars you wish to convert? ')
exchange_rate = input('(1 US Dollar equals what in the Foreign Currency?) ' )
# formula is total usd / exchange rate. we need to make sure usd is represented as a float to do the math correctly.
conversion = float(usd) / float(exchange_rate)
print(conversion)

View file

@ -0,0 +1,22 @@
#!/usr/bin/env python
# python fundamentals
# ex5.py
# page 34
# 5. Write a program that converts centimeters to yards, feet, and inches. There are 2.54cm in an inch. You can solve this problem by doing division, multiplication, addition, and subtraction. Converting a float to an int at the appropriate time will help in solving this problem. When you run the program, it should look exactly like this(except possibly for decimal places in the inches):
# How many centimeters do you want to convert? 127.25
# This is 1 yards, 1 feet, 2.098425 inches.
cm_per_inch = 2.54
total_cm = float(input('How many centimers do you want to convert? '))
inches = float(total_cm/cm_per_inch)
# 36 inches in 1 yard
yard = int(inches/36)
# 3 feet in 1 yard
feet = int(yard/3)
print("This is {0} yards, {1} feet, {2} inches." .format(yard, feet, inches))

View file

@ -0,0 +1,28 @@
#!/usr/bin/env python
# python programming fundamentals
# chapter 1 - page 34 - ex6
#6. write a program that computes the minimum number of bills and coins needed to make change for a person. For instance, if you need to give $34.36 in change you would need one twenty, one ten, four ones, a quarter, a dime, and a penny. You don't have to compute change for bills greater than $20 dollar bills or for fifty cent pieces. You can solve this problem by doing division, multiplication, subtraction, and converting floats to ints when appropriate. So when you run the program it should look exactly like this:
# How much did the item cost: 65.64
# How much did the person give you: 100.00
# The person's change is $34.36
# The bills or the change should be:
# 1 twenties
# 1 tens
# 0 fives
# 4 ones
# 1 quarters
# 1 dimes
# 0 nickels
# 1 pennies
cost = float(input('How much did the item cost: '))
amt_given = float(input('How much did the person give to you: '))
# dictionary mapping bill/coin name to currency value
bills = {'penny': 0.01, 'nickel': 0.05, 'dime': 0.10, 'quarter': 0.25, 'one': 1.00, 'five': 5.00, 'ten': 10.00, 'twenty': 20.00}
# compute the change
change = float(amt_given - cost)
# conditional to test each if each dict value goes into the total # of change

View file

@ -0,0 +1,18 @@
#!/usr/bin/env python
# python programming fundamentals
# page 33
# 1.19 excercise #1
# 1. write a program that asks the user to enter their name. Then it should print out the ASCII equivalent of each of the first four characters of your name. For instance, here is a sample run of the program below:
# Please enter your name: Kent
# K ASCII value is 75
# e ASCII value is 101
# n ASCII value is 110
# t ASCII value is 116
name = input('Enter your name: ')
print(name[0] + ' ' + 'ASCII value is ' + str(ord(name[0])))
print(name[1] + ' ' + 'ASCII value is ' + str(ord(name[1])))
print(name[2] + ' ' + 'ASCII value is ' + str(ord(name[2])))
print(name[3] + ' ' + 'ASCII value is ' + str(ord(name[3])))

View file

@ -0,0 +1,36 @@
#!/usr/bin/env python
# python programming fundamentals
# page 33
# 1.19 excercise #2
# 2. write a program that capitalizes the first four characters of a string by converting their characters to their ASCII equivlaent , then adding the necessary amount to capitlize them , and converting the integers back to characters. Print the capitalized string.
# the developers of ascii made the 6th bit be a 0 for the upper case and a 1 for lower case. here is an example
#A - 01000001 or 65 in dec
#a - 01100001 or 65(+32) in dec = 97
# so subtract or add 32 depending on if you wanna go lower --> upper or vice versa and it is the equivalent to flipping the 6th bit.
name = input('Enter your name in lowercase: ')
print('\n')
# store the integers in a list so we don't have to rewrite the same code
name_ascii_list_l= [int(ord(name[0])), int(ord(name[1])), int(ord(name[2])), int(ord(name[3]))]
print('lower case:')
# print lower case decimal ascii value
print(name[0] + ' ' + 'ASCII value is ' + str(name_ascii_list_l[0]))
print(name[1] + ' ' + 'ASCII value is ' + str(name_ascii_list_l[1]))
print(name[2] + ' ' + 'ASCII value is ' + str(name_ascii_list_l[2]))
print(name[3] + ' ' + 'ASCII value is ' + str(name_ascii_list_l[3]))
print('\n')
# now convert to upper case. again we store values in a list.
name_ascii_list_u = [name_ascii_list_l[0] - 32, name_ascii_list_l[1] - 32, name_ascii_list_l[2] - 32, name_ascii_list_l[3] - 32]
print ('upper case:')
print(chr(name_ascii_list_u[0]) + ' ' + 'ASCII value is ' + str(name_ascii_list_u[0]))
print(chr(name_ascii_list_u[1]) + ' ' + 'ASCII value is ' + str(name_ascii_list_u[1]))
print(chr(name_ascii_list_u[2]) + ' ' + 'ASCII value is ' + str(name_ascii_list_u[2]))
print(chr(name_ascii_list_u[3]) + ' ' + 'ASCII value is ' + str(name_ascii_list_u[3]))

View file

@ -0,0 +1,16 @@
#!/usr/bin/env python
# python programming fundamentals
# page 34
# 3. you can keep track of your car's miles per gallon if you keep track of how many miles you drive your car on a tank of gas and you always fill up your tank when getting gas
# write a program that asks the user to enter the number of miles you drove your car and the numbe rof gallons of gas you put in your car and then prints the miles per gallon you
# got on that tank of gas.
miles = input('How many miles did you drive your car? ')
gallons = input('How much gas did you put into your car? ')
# miles drive / gallons used
mpg = int(miles) / int(gallons)
print('Here is the total mpg based on those numbers:' + str(mpg))

View file

@ -0,0 +1,17 @@
#!/usr/bin/env python
# ex4.py
# python programming fundamentals - page34
# 4. Write a program that converts US dollars to a Foreign Currency. You can do this by finding the exchange rate on the internet and then prompting for the exchange rate in your program. It should look exactly like this:
# What is the amount of US Dollars you wish to convert? 31.67
# What is the current exchange rate
# (1 US Dollar equals what in the Foreign Currency)? 0.9825
# The amount in the Foreign Currency is $31.12
usd = input('What is the amount of US Dollars you wish to convert? ')
exchange_rate = input('(1 US Dollar equals what in the Foreign Currency?) ' )
# formula is total usd / exchange rate. we need to make sure usd is represented as a float to do the math correctly.
conversion = float(usd) / float(exchange_rate)
print(conversion)

View file

@ -0,0 +1,22 @@
#!/usr/bin/env python
# python fundamentals
# ex5.py
# page 34
# 5. Write a program that converts centimeters to yards, feet, and inches. There are 2.54cm in an inch. You can solve this problem by doing division, multiplication, addition, and subtraction. Converting a float to an int at the appropriate time will help in solving this problem. When you run the program, it should look exactly like this(except possibly for decimal places in the inches):
# How many centimeters do you want to convert? 127.25
# This is 1 yards, 1 feet, 2.098425 inches.
cm_per_inch = 2.54
total_cm = float(input('How many centimers do you want to convert? '))
inches = float(total_cm/cm_per_inch)
# 36 inches in 1 yard
yard = int(inches/36)
# 3 feet in 1 yard
feet = int(yard/3)
print("This is {0} yards, {1} feet, {2} inches." .format(yard, feet, inches))