Skip to content

Instantly share code, notes, and snippets.

@ohr-emet
ohr-emet / py4e_7_1.py
Created May 16, 2020 10:32
py4e - Ch 7 Files: Ex1 [convert to upper]
'''Write a program to read through a file and print the contents of the file (line by line) all in upper case. Executing the program will look as follows:
python shout.py
Enter a file name: mbox-short.txt
FROM [email protected] SAT JAN 5 09:14:16 2008
RETURN-PATH: <[email protected]>
RECEIVED: FROM MURDER (MAIL.UMICH.EDU [141.211.14.90])
BY FRANKENSTEIN.MAIL.UMICH.EDU (CYRUS V2.3.8) WITH LMTPA;
SAT, 05 JAN 2008 09:14:16 -0500
'''
@ohr-emet
ohr-emet / py4e_Ch6_Ex5.py
Created May 15, 2020 08:26
py4e - Ch 6 Strings: Ex5 [Find, convert to float]
#Take the following Python code that stores a string:
# str = 'X-DSPAM-Confidence:0.8475'
# Use find and string slicing to extract the portion of the string after the colon character and then use the float function to convert the extracted string into a floating point number.
data = input('Enter your text: ')
col_pos = data.find(':')
flt_find = data[col_pos+1:]
flt_find = float(flt_find)
print (flt_find)
@ohr-emet
ohr-emet / py4e_Ch6_Ex3.py
Created May 15, 2020 08:12
py4e - Ch 6 Strings: Ex3 [def function to count]
#Exercise 3: Encapsulate this code in a function named count, and generalize it so that it accepts the string and the letter as arguments
def count(word, letter):
count = 0
for character in word:
if character == letter:
count = count + 1
print (count)
@ohr-emet
ohr-emet / py4e_Ch6_Ex1.py
Last active May 21, 2020 12:56
py4e - Ch 6 Strings: Ex1 [print 'Banana' backwards]
#Exercise 1: Write a while loop that starts at the last character in the string and works its way backwards to the first character in the string, printing each letter on a separate line, except backwards.
fruit = 'banana'
index = len(fruit) - 1
while index >= 0 :
letter = fruit[index]
print(letter)
@ohr-emet
ohr-emet / py4e_5_2.py
Last active January 19, 2024 02:37
py4e 5.2 Assignment - [Solved, Working solution] [Only review after you have tried _everything_ ]
largest = -1
smallest = None
while True :
num = input('Enter a number: ')
if num == "done" :
break
try: