Python Exercise Practice Handouts
Python Exercise Practice Handouts
Output
Python Variables: x = 15 15
In Python variables are created the moment we assign y = “The City School” The City School
Value to it: For output: >>>
A variable can have a short name (like x and y) or a more print(x)
descriptive name (age, carname, total volume). print(y)
Rules for Python variables:
A variable name must start with a letter or the underscore
character. A variable name cannot start with a number. A
variable name can only contain alpha-numeric characters
and underscores (A-z, 0-9, and). Variable names are case-
sensitive (age, Age and AGE are three different variables)
Comments:
x = 23 #This is an Integer value
Comments are just a dead piece of code which y = “Hello, 7T” #This is an string value
Can be used for our references only. Comments start with
a #, And Python will render the rest of the line as a comment: #This is a comment
Python Numbers:
There are three numeric types in Python:
• int
• float
• complex
Variables of numeric types are created when
we assign a value to them:
Let’s suppose that we want to display the term percentage of a student for a subject by using averages in exam marks
and course work.
For this example, we will also look into the user interaction using INPUT feature of python to take data from the user
end.
2. In the 2nd line, we have declared a variable named Subject with user INPUT to take subject name form user.
3. In the 3rd line, we have declared a variable name Exam while setting its data type to float.
4. In the 3rd line, we have declared a variable for course work with the name of CW while setting its data type to float.
5. In the 5th line, we declare the variable name Percentage while setting its value to a Mathematical calculation of
Exam+CW divided by 2 to get the average percentage of the Subject.
6. In the 6th line, we have printed the string value along with the values of the Subject variable and percentage
variable. Perform the same exercise step by step for practice
LIST in Python:
An example of a list could be:
In Python, you can store your data into variables, but you can
Del command is used to delete a list element as mentioned in the example below:
To add an item to the end of the list, use the append() method.
These conditions can be used in several ways, most commonly in "if statements" and loops.
IF
In this example, we use two variables, a and b, which are used as part of the if statement to test whether b is greater
than a. As a is 33, and b is 200, we know that 200 is greater than 33, and so we print to screen that "b is greater than a".
Indention is necessary. If we do not use the indention as mentioned in the example, python will give an error.
The Elif keyword is python’s way of saying "if the previous conditions were not true, then try this condition".
In this example a is equal to b, so the first condition is not true, but the elif condition is true, so we print to screen that
"a and b are equal".
Else
The else keyword catches anything which isn't caught by the preceding conditions
In this example a is greater than b, so the first condition is not true, also the Elif condition is not true, so we go to the
else condition and print to screen that "a is greater than b". We can also use the else without using Elif.
Conditional Operators & Logical Operators:
A for loop is used for repeating over a sequence (that is either a list or a string).
‘FOR’ keyword will be must use when we are working with a for loop.
For example, we have a list of students and we want to display the student with the highest marks without using the
max() function. We will use the following code:
1. In the 1st line of this code, we have created a list StdMrks with four values stored in it.
2. In the 2nd line, we have declared a variable name MaxMarks with an integer value of 0.
3. In the 3rd line, we use for loop while declaring another variable “i” with the range of four. This means this loop will
run 4 times. Every time FOR loop runs, it will increase the value of the “i” variable.
4. In the 4th line, we have set a condition to check that if StdMrks[i] variable is greater than MaxMrks (declared 0 in 2nd
line) variable then change the value of MaxMarks[i] to StdMrks value. (StdMrks[i] variable is going to change its value
every time the loop runs and change the index of StdMrks. This process is also known as unary increment). Using
indention is compulsory or python will not consider the For loop elements and give an error.
5. In the 5th line, MaxMrks is setting its value equal to the current value of StdMarks only if the condition in the previous
line is true.
6. In the 6th line, we have just printed the value of MaxMrks along with a string sentence.
Python Functions
In Python, a Function is a group of related statements that perform a specific task. Functions help break our program
into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and
manageable.
Furthermore, it avoids repetition and makes code reusable. Function names cannot have spaces in between. Instead of
spaces use _ underscore to connect the words. In Python, a function is defined using the def keyword and for executing
the function we can use the function name along with parenthesis ().
We have already experienced this code earlier but now we have converted the same into a function and now it can be
recalled and reused whenever required in the program.