For beginners on the road to expert!
- Installing Python
- Hello World
- Hello User
- Compute Gross Pay
- Compute Gross Pay with Conditions
- Try Except
- Functions
- Loop
- Strings
- File
- List
- Dictionary
- Tuples
- Regular Expressions
In order to execute python files, we need to install python. Install the latest version of Python here. Once its successfully installed, you're now ready to code and execute commands or files in python.
Q. Output 'Hello World' to the screen.
Output.
Hello World
Q. Greet 'Hello' to the user with it's name.
Input:
Juzer Shakir
Output:
Hello Juzer Shakir
Q. Write a program to prompt user for hours and rate per hour to compute their gross pay.
Input:
Enter Hours: 10
Enter Rate: 10
Output:
Pay: 1000.0
Q. Rewrite your pay computation (Exercise 3) to give the employee 1.5 times the hourly rate for hours worked above 40 hours.
Scenario 1:
Enter Hours: 10
Enter Rate: 10
Pay: 1000.0
Scenario 2:
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
Q. Rewrite your pay program (Exercise 4) using try and except so that your program handles non-numeric input gracefully by printing a message and exiting the program.
Scenario 1:
Enter Hours: five
Error, please enter numeric input
Scenario 2:
Enter Hours: 65
Enter Rate: 8
Pay: 620.0
Q. Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error message.
If the score is between 0.0 and 1.0, print a grade using the following table:
Score | Grade |
---|---|
>=0.9 | A |
>=0.8 | B |
>=0.7 | C |
>=0.6 | D |
< 0.6 | F |
Scenario 1:
Enter score: 1.1
Bad Score
Scenario 2:
Enter score: 0.76
C
Q. Rewrite your pay computation (Exercise 5) and create a function called 'computepay' which takes two parameters (hours and rate).
Scenario 1:
Enter Hours: 60
Enter Rate: eleven
Error, please enter numeric input
Scenario 2:
Enter Hours: 40.5
Enter Rate: 9.5
Pay: 387.125
Q. Rewrite grading program (Exercise 6) using function named 'computegrade'.
Scenario 1:
Enter score: 0.52
F
Scenario 2:
Enter score: 0.83
B
Q. Write a program which repeatedly reads numbers until the user inputs "done". Once "done" is entered, print out the total, count, and average of the input numbers. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number.
Scenario 1:
Enter a number: juzer
Invalid input
Enter a number: one
Invalid input
Enter a number: twenty
Invalid input
Enter a number: done
Canonot compute string input. Only numeric input allowed.
Scenario 2:
Enter a number: 97
Enter a number: 176
Enter a number: 32
Enter a number: forty
Invalid input
Enter a number: 40
Enter a number: 64
Enter a number: 21
Enter a number: 99
Enter a number: 58
Enter a number: hundred
Invalid input
Enter a number: 100
Enter a number: done
Total of all 9 numeric inputs are 687.0 with an average of 76.33333333333333
Scenario 3:
Enter a number: 97
Enter a number: 11
Enter a number: 41
Enter a number: 86
Enter a number: 191
Enter a number: 80
Enter a number: 23
Enter a number: 153
Enter a number: done
Total of all 8 numeric inputs are 682.0 with an average of 85.25
Q. Similar to Exercise 9, only output a minimum and a maximum number inputted by the user.
Scenario 1:
Enter a number: ww
Invald input.
Enter a number: nine
Invald input.
Enter a number: done
Minimum = None Maximum = None
Scenario 2:
Enter a number: 70
Enter a number: 80
Enter a number: 37
Enter a number: 49
Enter a number: lalala
Invald input.
Enter a number: 56
Enter a number: 1
Enter a number: 6
Enter a number: don
Invald input.
Enter a number: done
Minimum = 1.0 Maximum = 80.0
Scenario 3:
Enter a number: 17
Enter a number: 30
Enter a number: 61
Enter a number: 102
Enter a number: 5
Enter a number: 110
Enter a number: 45
Enter a number: 69
Enter a number: 87
Enter a number: done
Minimum = 5.0 Maximum = 110.0
Q. 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.
str = 'X-DSPAM-Confidence: 0.8475'
Output.
0.8475
Q. Write a program to read through a file mbox-short.txt and print the contents of the file (line by line) all in upper case.
Scenario 1:
Enter a file name: short.txt
No such file exists in this directory with this file name: short.txt
Scenario 2:
Enter a file name: mbox-short.txt
FROM STEPHEN.MARQUARD@UCT.AC.ZA SAT JAN 5 09:14:16 2008
RETURN-PATH: <POSTMASTER@COLLAB.SAKAIPROJECT.ORG>
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
X-SIEVE: CMU SIEVE 2.3
RECEIVED: FROM MURDER ([UNIX SOCKET])
BY MAIL.UMICH.EDU (CYRUS V2.2.12) WITH LMTPA;
SAT, 05 JAN 2008 09:14:16 -0500
Q. Write a program to read through the file mbox-long.txt and look for lines of the form:
X-DSPAM-Confidence: 0.8475
When you encounter a line that starts with "X-DSPAM-Confidence:" pull apart the line to extract the floating-point number from that line. Count number of occurances on the file and then compute the total of the spam confidence values from these lines. Also print out the average spam confidence values.
Scenario 1:
Enter a file name: short.txt
No such file exists in this directory with this file name: short.txt
Scenario 2:
Enter a file name: mbox-short.txt
This file does not contain spam confidence values to compute. Please input file name of appropriate file.
Scenario 3:
Enter a file name: mbox-long.txt
Average spam confidence: 0.7507185185185187
Q. Write a program to open the file romeo.txt and read it line by line.
For each line, split the line into a list of words using the 'split' function. For each word, check to see if the word is already in a list. If the word is not in the list, add it to the list. When the program completes, sort and print the resulting words in alphabetical order.
Scenario 1:
Enter a file name: domeo.txt
domeo.txt doesnt exist!
Scenario 2:
Enter a file name: romeo.txt
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']
Q. Write a program to read through a mail log from mbox-long.txt file, line starting with 'From '. You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out a count at the end.
Hint: make sure not to include the lines that start with 'From:'.
Scenario 1:
Enter a file name: domeo.txt
domeo.txt file doesnt exist!
Scenario 2:
Enter a file name: romeo.txt
Please input desired file name for computation.
Scenario 3:
Enter a file name: mbox-long.txt
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
zqian@umich.edu
rjlowe@iupui.edu
zqian@umich.edu
rjlowe@iupui.edu
cwen@iupui.edu
cwen@iupui.edu
gsilver@umich.edu
gsilver@umich.edu
zqian@umich.edu
gsilver@umich.edu
wagnermr@iupui.edu
zqian@umich.edu
antranig@caret.cam.ac.uk
gopal.ramasammycook@gmail.com
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
louis@media.berkeley.edu
ray@media.berkeley.edu
cwen@iupui.edu
cwen@iupui.edu
cwen@iupui.edu
There were 27 lines in the file with 'From' as the first word
Q. Write a program to read through a mail log from mbox-long.txt file, line starting with 'From '. Build a histogram using a dictionary to count how many messages have come from each email address.
Look through the dictionary using loop to find who has the most messages and print how many messages the person has.
Note: make sure not to include the lines that start with 'From:'.
Scenario 1:
Enter file name: mbox.txt
mbox.txt file doesn't exist
Scenario 2:
Enter file name: mbox-long.txt
cwen@iupui.edu has most messages of 5
Q. Write a program that counts the distribution of the hour of the day for each of the messages in the mbox-long.txt file. You can pull the hour from the 'From ' line by finding the time string and then splitting that string into parts using the colon character.
Once you have accumulated the counts for each hour, print out the counts, one per line, sorted by hour.
Scenario 1:
Enter file name: 11
11 file doesn't exist
Scenario 2:
Enter file name: mbox-long.txt
04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1
Regular expressions cheat sheet:
Regular Expressions | Description |
---|---|
^ | Matches the beginning of a line |
$ | Matches the end of the line |
. | Matches any character |
\s | Matches Whitespace |
\S | Matches any non-whitespace character |
* | Repeats a character zero or more times |
*? | Repeats a character zero or more times (Non-Greedy) |
+ | Repeats a character one or more times |
+? | Repeats a character one or more times (Non-Greedy) |
[aeiou] | Matches a single character in the listed set |
[^XYZ] | Matches a single character not in the listed set |
[a-z0-9] | The set of characters can include a range |
( | Indicates where string extraction is to start |
) | Indicates where string extraction is to end |
For more information on Regular Expression, visit here
Q. Sum up all 'X-DSPAM-Confidence' values from mbox.txt file.
Scenario 1:
Enter file name: dummy
No such file found! Try again!
Scenario 2:
Enter file name: mbox.txt
667.8322999999995
Q. Write a program similar to Exercise 15 but regardless of whether the line starts from 'From' on mbox.txt file.
Scenario 1:
Enter file name: dummy
No such file found! Try again!
Scenario 2:
Enter file name: mbox.txt
Numeber of email id's: 746
131 is highest number of mails done by cwen@iupui.edu email id.
Q. Extract all the numbers in a file and compute the sum of the numbers. File 1 and File 2
Scenario 1:
Enter file name: ...
No such file found! Try again!
Scenario 2:
Enter file name: regex_sum_42.txt
445833
Scenario 3:
Enter file name: regex_sum_163439.txt
297017