Skip to content

JuzerShakir/How-to-Python

Repository files navigation

Guide on how to code in Python.

For beginners on the road to expert!

Table of Contents


Installation

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.


1. Hello World

Q. Output 'Hello World' to the screen.

Code


Output.

Hello World

2. Hello User

Q. Greet 'Hello' to the user with it's name.

Code


Input:

Juzer Shakir

Output:

Hello Juzer Shakir

Back to Top


3. Compute Gross Pay

Q. Write a program to prompt user for hours and rate per hour to compute their gross pay.

Code


Input:

Enter Hours: 10
Enter Rate: 10

Output:

Pay: 1000.0

Back to Top


4. Gross Pay with Conditions

Q. Rewrite your pay computation (Exercise 3) to give the employee 1.5 times the hourly rate for hours worked above 40 hours.

Code


Scenario 1:

Input
Enter Hours: 10
Enter Rate: 10
Output:
Pay: 1000.0

Scenario 2:

Input
Enter Hours: 45
Enter Rate: 10
Output:
Pay: 475.0

Back to Top


Try Except

5. Gross Pay

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.

Code


Scenario 1:

Input
Enter Hours: five
Output:
Error, please enter numeric input

Scenario 2:

Input
Enter Hours: 65
Enter Rate: 8
Output:
Pay: 620.0

Back to Top


6. Grading System

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

Code


Scenario 1:

Input
Enter score: 1.1
Output:
Bad Score

Scenario 2:

Input
Enter score: 0.76
Output:
C

Back to Top


Functions

7. Gross Pay

Q. Rewrite your pay computation (Exercise 5) and create a function called 'computepay' which takes two parameters (hours and rate).

Code


Scenario 1:

Input
Enter Hours: 60
Enter Rate: eleven
Output:
Error, please enter numeric input

Scenario 2:

Input
Enter Hours: 40.5
Enter Rate: 9.5
Output:
Pay: 387.125

Back to Top


8. Grading System

Q. Rewrite grading program (Exercise 6) using function named 'computegrade'.

Code


Scenario 1:

Input
Enter score: 0.52
Output:
F

Scenario 2:

Input
Enter score: 0.83
Output:
B

Back to Top


Loop

9. Compute Input

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.

Code


Scenario 1:

Input:
Enter a number: juzer
Invalid input
Enter a number: one
Invalid input
Enter a number: twenty
Invalid input
Enter a number: done
Output:
Canonot compute string input. Only numeric input allowed.

Scenario 2:

Input:
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
Output:
Total of all 9 numeric inputs are 687.0 with an average of 76.33333333333333

Scenario 3:

Input:
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
Output:
Total of all 8 numeric inputs are 682.0 with an average of 85.25

Back to Top


10. Min and Max

Q. Similar to Exercise 9, only output a minimum and a maximum number inputted by the user.

Code


Scenario 1:

Input:
Enter a number: ww
Invald input.
Enter a number: nine
Invald input.
Enter a number: done
Output:
Minimum = None Maximum = None

Scenario 2:

Input:
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
Output:
Minimum = 1.0 Maximum = 80.0

Scenario 3:

Input:
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
Output:
Minimum = 5.0 Maximum = 110.0

Back to Top


11. String

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'


Code


Output.

0.8475

Back to Top


File

12. Print text from file

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.

Code


Scenario 1:

Input:
Enter a file name: short.txt
Output:
No such file exists in this directory with this file name: short.txt

Scenario 2:

Input:
Enter a file name: mbox-short.txt
Output:
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

Back to Top


13. Compute values within file

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.


Code


Scenario 1:

Input:
Enter a file name: short.txt
Output:
No such file exists in this directory with this file name: short.txt

Scenario 2:

Input:
Enter a file name: mbox-short.txt
Output:
This file does not contain spam confidence values to compute. Please input file name of appropriate file.

Scenario 3:

Input:
Enter a file name: mbox-long.txt
Output:
Average spam confidence: 0.7507185185185187

Back to Top


List

14. Splitting and Sorting

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.

Code


Scenario 1:

Input:
Enter a file name: domeo.txt
Output:
domeo.txt doesnt exist!

Scenario 2:

Input:
Enter a file name: romeo.txt
Output:
['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']

Back to Top


15. Email Count

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:'.


Code


Scenario 1:

Input:
Enter a file name: domeo.txt
Output:
domeo.txt file doesnt exist!

Scenario 2:

Input:
Enter a file name: romeo.txt
Output:
Please input desired file name for computation.

Scenario 3:

Input:
Enter a file name: mbox-long.txt
Output:
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

Back to Top


16. Dictionary

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:'.


Code


Scenario 1:

Input:
Enter file name: mbox.txt
Output:
mbox.txt file doesn't exist

Scenario 2:

Input:
Enter file name: mbox-long.txt
Output:
cwen@iupui.edu has most messages of 5

Back to Top


17. Tuples

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.

Code


Scenario 1:

Input:
Enter file name: 11
Output:
11 file doesn't exist

Scenario 2:

Input:
Enter file name: mbox-long.txt
Output:
04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1

Back to Top


Regular Expressions

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


18. Compute values within file

Q. Sum up all 'X-DSPAM-Confidence' values from mbox.txt file.

Code


Scenario 1:

Input:
Enter file name: dummy
Output:
No such file found! Try again!

Scenario 2:

Input:
Enter file name: mbox.txt
Output:
667.8322999999995

Back to Top


19. Highest Emails

Q. Write a program similar to Exercise 15 but regardless of whether the line starts from 'From' on mbox.txt file.

Code


Scenario 1:

Input:
Enter file name: dummy
Output:
No such file found! Try again!

Scenario 2:

Input:
Enter file name: mbox.txt
Output:
Numeber of email id's:  746
131 is highest number of mails done by cwen@iupui.edu email id.

Back to Top


20. Finding Number

Q. Extract all the numbers in a file and compute the sum of the numbers. File 1 and File 2


Code


Scenario 1:

Input:
Enter file name: ...
Output:
No such file found! Try again!

Scenario 2:

Input:
Enter file name: regex_sum_42.txt
Output:
445833

Scenario 3:

Input:
Enter file name: regex_sum_163439.txt
Output:
297017

Back to Top