You are on page 1of 12

data = input("Enter a word/s \n")

for index in range (len(data)):


print (index, data[index])

#counts the number of words


name = input ("Enter a name. \n")
count = len(name)
print (name, "has" ,count, "letters.")
print(name[1])
print (name[-2])

#Reads the data from range


data = "Computer Fundamentals and Programming"
print (data[5:10])
print (data[30:35])

#Reads if the letters you eneter is present in the word or not


data = input("Enter a string. \n")
sub = input("Enter a substring.\n")

if sub in data:
print("Valid")
else:
print("Invalid")

#Encryption
plainText = input ("Enter a one-word, lowercase message:")
distance = int (input("Enter the distance value:"))
code = ""
for ch in plainText:
ordvalue = ord(ch)
cipherValue = ordvalue + distance
if cipherValue > ord ("z"):
cipherValue = ord ("a") + distance - (ord("z") - ordvalue+1)
code += chr(cipherValue)
print(code)

#Decryption
plainText = input ("Enter the coded text:")
distance = int (input("Enter the distance value:"))
code = ""
for ch in plainText:
ordvalue = ord(ch)
cipherValue = ordvalue - distance
if cipherValue > ord ("z"):
cipherValue = ord ("a") - distance - (ord("z") - ordvalue+1)
code += chr(cipherValue)
print(code)

#Decimal to Binary
decimal = int(input("Enter a decimal integer:"))
if decimal == 0:
print(0)
else:
print("Quotient Remainder Binary")
bitstring = ""
while decimal > 0:
remainder = decimal % 2
decimal = decimal // 2
bitstring = str(remainder) + bitstring
print("%5d%8d%12s" % (decimal, remainder, bitstring))
print("The binary representation is", bitstring)

#Binary to Decimal
bitstring = input("Enter a string of bits:")
decimal = 0
exponent = len(bitstring) - 1
for digit in bitstring:
decimal = decimal + int(digit) * 2 ** exponent
exponent = exponent - 1
print ("The integer value is", decimal)

#Reads if the password contains 8 characters


password = input("Enter a password. \n")
(len(password))
if len(password) == 8:
print (password)
else:
print ("The password doesn't have 8 characters.")

#Password validation
password = input("Enter a password. ")
digits = 0
letters = 0

for ch in password:
if ch.isalpha():
letters = letters + 1
elif ch.isalnum():
digits = digits + 1
else:
print("Invalid password!")
exit()
break

if letters >= 8 and digits >= 2:


print("Valid password!")
else:
print("Invalid Password!")

#1 Program displays Valid SSN for a correct Social Security number or Invalid
SSN otherwise.
password = (input("Enter SSN.\n"))
data = password
digits = 0
print ((data[0:3]) + "-" + data[3:5] + "-" + data[5:9])
for d in password:
if d.isnumeric():
digits = digits + 1
else:
print("Invalid SSN!")
exit()
break
if digits == 9:
print("Valid SSN!")
else:
print("Invalid SSN!")

#to write a text file


textfile = open("Intro.txt", "w")
textfile.write("Name = Bang\n")
textfile.write("Section = A166\n")
textfile.write("Program = ECE\n")
textfile.close()

#to read text files


textfile = open("Intro.txt", "r")
strings = textfile.read()
print (strings)

#Enter a random number in a text file from 1-20


import random
textfile = open("programming.txt", "w")
for count in range (1,16):
number = random.randint(count)
textfile.write (str(number) +"\n")
textfile.close()
WEEK 2
#1
To_Buy = ["Scical", "chocolate", "Mouse", "Flashdrive","SD Card"]
print (To_Buy)
print (To_Buy[3])

#2
numbers = [5,4,3,2,1]
print (numbers)
print (numbers[1]+7)

#3
x = 10
var = [x, x+1, x+2, x+3, x+4, x+5]
print(var)

#Make range in list/horizontal format


integers = list(range(5,10+1))
print(integers)

#5
string_list = list("Bang")
print (string_list)

#Squared the range number in a list form


mult = list(range(0,10+1))
print(mult)
for i in mult.copy():
mult[i] = mult[i] ** 2
print (mult)

#6
ex = [8,5,4,7,9,2,5]
ex.insert(4,526)
ex.extend([880,90])
ex.append(9)
ex.pop (9)
ex.sort()
print (ex)

#7
y = list (range(3,31,3))
print(y)
for x in y.copy():
if x%2 != 0:
y.remove(x)
print(y)

#8
num = [1,2,3,4,5]
if 3 in [num.copy]:
print (len(num))
print("True")
else:
print("False")

#9
list = [34,45,67]
target =467
if target in list:
print(list.index(target))
else:
print(8)

#Find the median in the text file


filename = input("Enter s file name. \n")
f = open (filename, "r")
numbers = []
for line in f:
words = line.split()
for word in words:
numbers.append(float(word))

numbers.sort()
midpoint = len (numbers)//2
print("The median is:")
if len(numbers) % 2 ==1:
print(numbers[midpoint])
else:
print ((numbers[midpoint] + numbers [midpoint-1])/2)

#10
students = {}
students["name"] = input("Enter a name. \n")
students["program"] = input("Enter a grade level. \n")
print(students)

students.pop("name")
print(students)
WEEK 3
#1
def info():
print ("Bang")
print ("A166")
print("ECE")
info()

#1.1
def my_function(country = "Philippines"):
print("I am from" ,country)
print(my_function("Sweden"))
print(my_function("Italy"))
print(my_function("Singapore"))
my_function()

#2
x = "Hakdog"
def hehe():
y = "Welcome"
print (x)
print (y)
hehe()

#3
name = input("Enter name. \n")
def greet (name):
print ("Hello " + name)
greet ("Boang")
greet(name)
greet(name)

#4
def my_function(x):
return 5*x
print(my_function(3))
print(my_function(4))
print(my_function(9))

#4.1
no_1 = int(input("enter the first number: "))
no_2= int(input("enter the second number: "))
def math(x, y):
a = x + y
b = x - y
c = x * y
d = x / y
print("the sum of", x, "and", y, "is", a)
print("the difference of", x, "and", y, "is", b)
print("the product of", x, "and", y, "is", c)
print("the quotient of", x, "and", y, "is", d)
math(no_1,no_2)
#5
no_1 = int(input("Enter first number.\n"))
no_2 = int(input("Enter second number.\n"))
def add (x, y):
a = x + y
print ("The sum of " , x, "and", y, "is", a, ".")
add(no_1, no_2)

#6
num = int (input("Enter a number:"))
def multiply(x):
y = x * 5
return y

print (multiply(num))

#7
a = int (input("Enter first side. \n"))
b = int (input("Enter second side. \n"))
c = int (input("Enter third side. \n"))

def print_this(x, y, z):


a1 = x*x
b1 = y*y
c1 = z*z
if c1 == a1 + b1:
print ("This is a right triangle.")
else:
print ("This is not a right triangle.")

print_this(a, b, c)

#8
num1 = int (input("Enter first number. \n"))
num2 = int (input("Enter second number. \n"))
def sum(x, y):
add = x + y
return add

print (sum(num1, num2))


z = 10 * sum(num1, num2)
print (z)

#10
sec = int(input("Enter time in seconds. \n"))
def conv (x):
conv_min = x // 60
conv_sec = x%60
print ("Conversion:", conv_min, "minutes and",conv_sec,"seconds")
conv(sec)

#11
year = int(input("Enter number of months. \n"))
def conv (x):
conv_year = x // 12
conv_week = x%12
print ("Conversion:", conv_year, "year/s and", conv_week, "weeks.")
conv(year)
LAB 1
#1 Program displays Valid SSN for a correct Social Security number or Invalid
SSN otherwise.

password = (input("Enter SSN.\n"))


data = password
digits = 0
print ((data[0:3]) + "-" + data[3:5] + "-" + data[5:9])
for d in password:
if d.isnumeric():
digits = digits + 1
else:
print("Valid SSN!")
exit()
break
if digits == 9:
print("Valid SSN!")
else:
print("Invalid SSN!")

#2 Program that prompt the user to enter a file name and a string to be
removed.
filename = input("Enter a filename.\n")
textfile = open (filename, "r")
Read = textfile.read()
textfile.close()
string = input ("Enter the string to be removed.\n")
if string in Read:
Read = Read.replace (string, str (""))
textfile = open (filename, "a")
textfile.close()
print ("Done")

#3 Program prompts the user to enter a password and displays valid password
#Password must have at least eight characters.
#A password must consist of only letters and digits.
#A password must contain at least two digits.

password = input("Enter a password. ")


digits = 0
letters = 0

for ch in password:
if ch.isalpha():
letters = letters + 1
elif ch.isalnum():
digits = digits + 1
else:
print("Invalid password!")
exit()
break
if letters >= 8 and digits >= 2:
print("Valid password!")
else:
print("Invalid Password!")

#4 Replace the text file data


print ("Enter a filename: ")
filename = input()
fileopen = open(filename, 'r')
readstrings = fileopen.read()
fileopen.close()

print ("Enter the old string: ")


text = input()
print ("Enter the new string: ")
replace = input()

if text in readstrings:
readstrings = readstrings.replace(text,replace)
fileopen = open(filename, 'w')
fileopen.write(readstrings)
fileopen.close()
print("Done")
else:
print("Error!")
LAB 2
#Reads a list of scores and then assigns grades based on the following scheme:
def grade(score, best):
if score >= best - 10:
return 'A'
if score >= best - 20:
return 'B'
if score >= best - 30:
return 'C'
if score >= best - 40:
return 'D'
return 'F'
score = input("Enter scores: ")
scores = [int(s) for s in score.split()]
best = scores[0]
for i in range(len(scores)):
if scores[i] > best:
best = scores[i]

for i in range(len(scores)):
print("Student", i, "score is", scores[i], "and grade is",
grade(scores[i], best))

#3 Converts a Hexadecimal number using a set of associations found in a


Dictionary.
hextoBinarytable = {"0": "0000", "1": "0001", "2": "0010", "3": "0011", "4":
"0100", "5": "0101","6": "0110","7": "0111","8": "1000","9": "1001", "A":
"1010", "B": "1011", "C": "1100", "D": "1101", "E": "1110", "F": "1111"}
def convert (number, table):
binary = ""
for digit in number:
binary = binary + table[digit]
return binary

number = input ("Enter a hexadecimal value. \n")


item = convert(number, hextoBinarytable)
print (item)
LAB 3
#Celsius to Fahrenheit
temp = int (input("Enter temperature in Celsius. \n"))

def convert (x):


F = round((9/5) * (x) +32)
print ("Farenheit Equivalent:" ,F, "degrees fahrenheit")

convert(temp)

#10

amount_dep = int (input("Enter deposited amount. \n"))


annual_rate = int (input("Enter the annual rate of interest. \n"))
compounded = int (input("Enter number of times interest is compounded. \n"))
no_years = int (input("Enter the number of years. \n"))

def compute (p, r, m, t):


rate_perc = r/100
Future_Value = (p * (1 + (rate_perc/m)) ** (m*t))
print ("Balance after" ,t, "years:",round(Future_Value, 2))

compute(amount_dep, annual_rate, compounded, no_years)

You might also like