You are on page 1of 6

#This is where I will save Ch1 Works

#This is code line No. 1


x1 = -4
y1 = abs(x1)
print(x1)
print(y1)

#This is code No. 2


x2 = 1.2345680251025681852015
y2 = round(x2,2)
print(x2)
print(y2)

#This is code No. 3


pi = 3.14159265358979
x3 = 128
y3 = -345.67890987
z3 = -999.9999
print(abs(z3))
print(int(z3))
print(int(abs(z3)))
print(round(pi,4))
print(bin(x3))
print(hex(x3))
print(oct(x3))
print(max(pi,x3,y3,z3))
print(min(pi,x3,y3,z3))
print(type(pi))
print(type(x3))
print(type(str(y3)))

#This is code No 4 introducting 'math' module


import math
z4 = 81
print(math.sqrt(z4))

#This is code No 5 using 'math' module more


import math
pi = math.pi
e = math.e
tau = math.tau
x5 = 81
y5 = 7
z5 = -23234.5454
print(pi)
print(e)
print(tau)
print(math.sqrt(x5))
print(math.factorial(y5))
print(math.floor(z5))
print(math.degrees(y5))
print(math.radians(45))

#This is code No 6 introducing f-string


username = "Alan"
print(f"Hello {username}")

#This is code No 7
sales_tax_rate = 0.065
print(f"Sales Tax Rate {sales_tax_rate:.2%}")

#This is code No 8
sample1 = f"Sales Tax Rate {sales_tax_rate:.2%}"
sample2 = f'Sales Tax Rate {sales_tax_rate:.2%}'
sample3 = f"""Sales Tax Rate {sales_tax_rate:.2%}"""
sample4 = f'''Sales Tax Rate {sales_tax_rate:.2%}'''
print(sample1)
print(sample2)
print(sample3)
print(sample4)

#This is code Nos 9, 10 & 11 and is important it involves adding multil


ines to f-string
#Use \n if you use single quotes,but if you use triple quotes you don't
need \n
user1 = "Alan9"
user2 = "Babs9"
user3 = "Carlos9"
output = f"{user1} \n{user2} \n{user3}"
print(output)
#This is using triple quotes
#Numerical values saved to variables
unit_price9 = 49.95
quantity9 = 32
sales_tax_rate9 = 0.065
subtotal9 = unit_price9 * quantity9
sales_tax9 = sales_tax_rate9 * subtotal9
total9 = subtotal9 + sales_tax9
#Amounts formatted to strings, and saved to new variables
s_subtotal9 = "$" + f"{subtotal9:,.2f}"
s_sales_tax9 = "$" + f"{sales_tax9:,.2f}"
s_total9 = "$" + f"{total9:,.2f}"
#Output strings with $ signs already attached
output9 = f"""
Subtotal: {s_subtotal9:>9}
Sales Tax: {s_sales_tax9:>9}
Total: {s_total9:>9}
"""
print(output9)

#This is code No 12; using binary, octal and hexadecimal conversions


#Numerical value
x12 = 255
#Convert decimal to other values
print(bin(x12))
print(oct(x12))
print(hex(x12))
print("\n")
#Shows number in decimal
print(0b11111111)
print(0o377)
print(0xff)

#This is looking at use of complex numbers


z12 = complex(2,-3)
print(z12)
print(z12.real)
print(z12.imag)

#Concatenating strings
first_name = "Alan"
middle_init = "C"
last_name = "Simpson"
full_name = first_name + " " + middle_init + ". " + last_name
print(full_name)

#Computing length of string


s1 = ""
s2 = " "
s3 = "A B C"
print(len(s1))
print(len(s2))
print(len(s3))

#This is code No 14 which looks at operators with strings


s14 = "Abracadabra Hocus Pocus you're a turtle dove"
#What is the length of s14?
print(len(s14))
#Is there a lower case t in s14?
print("t" in s14)
#Is there an upper case T in s14?
print("T" in s14)
#Is there no upper case T in s14?
print("T" not in s14)
#Print 15 hyphens in a row
print("-" * 15)
#Print first character in s14
print(s14[0])
#Print characters 33 to 39 in s14
print(s14[33:39])
#Print every 3rd character in s14 starting at 0
print(s14[0:44:3])
#This will print 'lowest' character in s14 (space is lower than a)
print(min(s14))
#Print 'highest' character in s14
print(max(s14))
#Print location of first uppercase P
print(s14.index("P"))
#Where is first lower case o in latter half of string s14?
#Note it will still provide value counting from 0
print(s14.index("o",22,44))
#How many lower case a are there in s14?
print(s14.count("a"))

#This is code No 16, which applies 'methods' to strings (cool stuff)


s16 = "There is no such word as schmeedledorp."
y16 = " a b c "
z16 = "ABC"
#This will capitalize first letter in z16, leave rest lower
print(z16.capitalize())
#This will count the number of spaces in s16
print(s16.count(" "))
#This will find the dot in s16
print(s16.find("."))
#This will check that y16 is all lower case
print(y16.islower())
#This will convert z16 to all lower case
print(z16.lower())
#This will strip leading spaces from y16
print(y16.lstrip())
#This will strip leading and trailing spaces from y16
print(y16.strip())
#This will swap the case of letters in s16
print(s16.swapcase())
#This will show s16 in 'title case'
print(s16.title())
#This will show s16 in upper case
print(s16.upper())

#This is code No 17,using the datetime module


#import datetime module; give it alias of dt
import datetime as dt
#Put today's date in a variable called 'today'
today = dt.date.today()
#Put another date in variable 'last_of_teens'
last_of_teens = dt.date(2019,12,31)
#See whats in the variables
print(today)
print(last_of_teens)
#Use them to create a sentence (I have adde this)
x17 = f"{today}" + " is a long time from " + f"{last_of_teens}"
print(x17)
print(last_of_teens.year)
print(last_of_teens.month)
print(last_of_teens.day)
print(f"{last_of_teens:%A, %B %d, %Y}")
print(f"{today:%m/%d/%Y}")

#This will check calculation of 'timespans'


new_years_day = dt.date(2019,1,1)
memorial_day = dt.date(2019,5,27)
days_btwn = memorial_day - new_years_day
print(days_btwn)

#Check another way


duration = dt.timedelta(days=146)
print(new_years_day + duration)

#Calculate your age in days?


birthday = dt.date(1976, 4, 7)
today = dt.date.today()
age = (today - birthday)
print(age)

#This is code No 18, using TimeDelta


#Get age in days
days_old = age.days
#Divide days by 365 to get years
years = days_old // 365
#Days left over from age is used to calculate months
months = (days_old % 365) // 30
#Print in f-string
print (f"You are {years} years, and {months} months old.")

#This is code No 20, dealing with time zones


#Get time from computer clock
here_now = dt.datetime.now()
#Get UTC datetime right now
utc_now = dt.datetime.utcnow()
#Subtract to see the difference
time_diff = (here_now - utc_now)
#Show results
print(f"My time : {here_now:%I:%M:%p}")
print(f"UTC time : {utc_now: %I:%M:%p}")
print(f"Difference : {time_diff}")

#This is code No 21, looking at time zones


#Note datetime has already been imported as dt
#Import timezone helpers from dateutil
from dateutil.tz import gettz
#UTC timezone right now
utc = dt.datetime.now(gettz('Etc/UTC'))
print (f"{utc:%A %D %I:%M %p %Z}")
#USA Eastern time
est = dt.datetime.now(gettz('America/New_York'))
print(f"{est:%A %D %I:%M %p %Z}")
#USA Central time
cst = dt.datetime.now(gettz('America/Chicago'))
print(f"{cst:%A %D %I:%M %p %Z}")
#USA Mountain time
mst = dt.datetime.now(gettz('America/Boise'))
print(f"{mst:%A %D %I:%M %p %Z}")
#Is this last one 'Pacific' time?
pst = dt.datetime.now(gettz('America/Los_Angeles'))
print(f"{pst:%A %D %I:%M %p %Z}" + "\n")

#This is code No 22, using event rather than 'now'


#Note datetime and gettz have been imported
#July 4 event, 7:00 local time (no specific time zone)
event = dt.datetime(2020,7,4,19,0,0)
#Show Local date and time
print("Local " + f"{event:%D %I:%M %p %Z}" + "\n")
event_eastern = event.astimezone(gettz("America/New_York"))
print(f"{event_eastern:%D %I:%M %p %Z}")
event_central = event.astimezone(gettz("America/Chicago"))
print (f"{event_central:%D %I:%M %p %Z}")
event_mountain = event.astimezone(gettz("America/Denver"))
print(f"{event_mountain:%D %I:%M %p %Z}")
event_pacific = event.astimezone(gettz("America/Los_Angeles"))
print(f"{event_pacific:%D %I:%M %p %Z}")
event_utc = event.astimezone(gettz("Etc/UTC"))
print(f"{event_utc:%D %I:%M %p %Z}")

You might also like