You are on page 1of 5

# Propositional logic evaluator for discrete math for 2 - 3 variables

print("Propositional logic evaluator for discrete math")


variables =int(input("How many variables? "))
total_combinations =2**variables

combinations_list =[]
# store all the possible combinations

# generate the combinations


for i in range (total_combinations):
bin_equivalent =bin(i)[2:]
while len (bin_equivalent)<variables:
bin_equivalent ="0" +(bin_equivalent)
combinations_list.append(tuple(int(val) forval
inbin_equivalent))

# 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)

name ="Gabriel Kobe Sablan"


file =open ("newfile1.txt",'w')
file.write ("Hello, Twinkle !\n")
file.write ("Isn't this amazaing!\n")
file.write ("That we can create and write on text files \n")
file.write ("using python")
file.close()

file=open ("newfile2.txt",'w')
file.write ("This message was created using Python!")
file.close()

file =open ("newfile1.txt",'r')


data =file.read (12)
print (data)
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))

def generate_truthtable(number_of_variables =0):


if number_of_variables ==0:
return"You need to enter an interger"
else:
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_list

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()

The program will end in {i}


The program will end in {i}
The program will end in {i}
The program will end in {i}
The program will end in {i}
The program will end in {i}
The program will end in {i}
The program will end in {i}
The program will end in {i}
The program will end in {i}

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())

The current time is 01: 50 PM

from datetime import datetime as currenttime


from datetime import datetime as currentdate
print("The current time is", currenttime.now())
print("The current date is", currentdate.now())
The current time is 2022-05-25 13:51:39.200567
The current date is 2022-05-25 13:51:39.201565

SUPPLEMENTARY
1
bad_words_list =['stupid', 'lazy', 'short', 'ugly']
def filterbad_words(sentence, badword):
return sentence.replace(badword, "*" * len(badword))

message =input("Enter any sentence: ")


for i in range(0, len(bad_words_list)):
len(bad_words_list[i])
bad =bad_words_list[i]

if bad in message:filtering =filterbad_words(message, bad)


print(filtering)

Enter any sentence: you are lazy


you are not only stupid but lazy and **** for a short guy

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)

print('The answers are {0} and {1}'.format(solution1,solution2))

Enter number for a 123


Enter number for b 23
Enter number for c 12
The answers are (-0.09349593495934959-0.2980259816791703j) and (-
0.09349593495934959+0.2980259816791703j)

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.

You might also like