Professional Documents
Culture Documents
combinations_list =[]
# store all the possible combinations
# this will generate a list with values [(0,0), (0,1), (1,0), (1,1)]
# for two values
# main program
expression =input("Enter the propositional logic expression: ")
# note: only the letters A, B, abnd C are allwoed to be used
# example: not (A and B) or (A and C)
if variables ==2:
print (" A B f")
forA, B incombinations_list:
evaluated_expression =eval (expression)
print (A,B, evaluated_expression)
elif variables ==3:
print (" A B C f")
forA, B, C incombinations_list:
evaluated_expression =eval (expression)
print (A, B, C, evaluated_expression)
file=open ("newfile2.txt",'w')
file.write ("This message was created using Python!")
file.close()
def generate_truthtable(number_of_variables):
total_combinations =2**number_of_variables
combinations_list =[]
for i in range (total_combinations):
bin_equivalent =bin(i)[2:]
while len (bin_equivalent)<number_of_variables:
bin_equivalent ="0" +bin_equivalent
combinations_list.append(tuple(int(val)forval
inbin_equivalent))
return combinations_listprint(generate_truthtable(3))
import math
def quadratic_formula(a,b,c):
if b**2-(4*a*c) <0: # involving imaginary numbers
x1 =(complex(-b,math.floor(math.sqrt(abs(b**2-(4*a*c))))))/2*a
x2 =(complex(-b,-1*math.floor(math.sqrt(abs(b**2-
(4*a*c))))))/2*a
return x1, x2
else:
x1 =(-b+math.sqrt(b**2-(4*a*c)))/(2*a)
x2 =(-b-math.sqrt(b**2-(4*a*c)))/(2*a)
# print (quadratic_formula(1,2,3))
print(quadratic_formula(1,2,3))
((-1+1j), (-1-1j))
import math
def angle_demo ():
angle =math.sin(math.pi/2)
# the default input is in radians
# angle sin (90) = 1 in degrees == sin(pi/2)=1 in radians
print (angle)
# to make it convenient, convert to radians
angel =math.sin(math.radians(90))
print(angle)
angle_demo()
1.0
1.0
import time
def pause():
for i in range (10,0,-1):
print("The program will end in {i}")
time.sleep(1)
pause()
def current_time():
t =time.strftime("%I: %M %p")
return t
print(current_time())
01: 50 PM
def current_date():
d =time.strftime("%b %d %Y")
return d
print(current_date())
May 25 2022
import datetime
print("The current time is",current_time())
SUPPLEMENTARY
1
bad_words_list =['stupid', 'lazy', 'short', 'ugly']
def filterbad_words(sentence, badword):
return sentence.replace(badword, "*" * len(badword))
2
importmathGRAVITY =9.81defprojectilemotion_solver():# Givenangle
=int(input('Enter angle in degrees: '))velocity =int(input('Enter
velocity in m/s: '))# converting degrees to radianspi =22/7radian
=angle*(pi/180)print ("The angle you entered in radians is: ",
radian)# getiing the rangerange
=((velocity**2)*(math.sin(2))*(radian))/(GRAVITY)print("The range or
distance travelled in the horizontal direction: ", ran# getting the
maximum heightheight =((velocity**2)*(math.sin(2))*(radian))/(2
*GRAVITY)print("The maximum height reached: ",
height)projectilemotion_solver()
3
import cmath
a =int(input('Enter number for a '))
b =int(input('Enter number for b '))
c =int(input('Enter number for c '))
d =(b**2) -(4*a*c)
solution1 =((-b)-cmath.sqrt(d))/(2*a)
solution2 =((-b)+cmath.sqrt(d))/(2*a)
QUESTIONS
Why do built-in functions exist? = allow you to use basic properties of strings and numbers
in your rules.
What is the advantages/disadvantages of placing code inside functions vs sequential codes.
= An advantage of using functions and procedures is that coding time is reduced.
What is the difference between a function and a module? = In programming, function refers
to a segment that groups code to perform a specific task. A module is a software component
or part of a program that contains one or more routines. That means, functions are groups
of code, and modules are groups of classes and functions.
What is the difference between a module and a package? = A module is a file containing
Python code in run time for a user-specific code. A package also modifies the user
interpreted code in such a way that it gets easily functioned in the run time.
CONCLUSION = This will hopefully allow me to better understand how to gain access to the
functionality available in the many third-party and built-in modules available in Python, if I
were to develope my own application, creating my own modules and packages will help me
organize and modularize my code, which makes coding, maintenance, and debugging
easier.