Lab02 Python2
Lab02 Python2
1. Python Indentation
Indentation refers to the spaces at the beginning of a code line. The indentation in Python is very
important, and it indicate a block of code.
- Example:
- You have to use the same number of spaces in the same block of code, otherwise Python will
give you an error:
- Python relies on indentation (whitespace at the beginning of a line) to define scope in the code.
If statement, without indentation (will raise an error):
- The elif keyword is pythons way of saying "if the previous conditions were not true, then try
this condition":
- The else keyword catches anything which isn't caught by the preceding conditions:
- The and/or keyword is a logical operator, and is used to combine conditional statements:
- You can have if statements inside if statements, this is called nested if statements:
3. Python Functions
A function is a block of code which only runs when it is called. You can pass data, known as
parameters, into a function. A function can return data as a result.
To call a function, use the function name followed by parenthesis, and then parameters can be
passed inside the parentheses.
- List items are indexed and you can access them by referring to the index number (the first item
has index [0], the second item has index [1]):
- To add an item to the end of the list, use the append() method:
- range() function: returns a sequence of integer numbers, starting from 0 by default, and
increments by 1 (by default), and stops before a specified number.
o Syntax: range(start, stop, step)
o Parameter:
▪ start: [ optional ] start value of the sequence, default is 0
▪ stop: next value after the end value of the sequence
▪ step: [ optional ] denoting the difference between any two numbers in the
sequence, default is 1
o Return: Returns a range type object.
https://cs.stanford.edu/people/nick/py/python-range.html
- in Keyword
o used to check if a value is present in a sequence (list, range, string etc.).
o used to iterate through a sequence in a for loop
- To loop through a set of code a specified number of times, we can use the range() function:
for i in range(5):
print("Hello world!")
- Nested Loops
A nested loop is a loop inside a loop. The "inner loop" will be executed one time for each
iteration of the "outer loop":
- Practice examples:
Exercise 1
Write a Python program to print odd numbers between 50 and 100.
Exercise 2
Write a Python program to print the integer numbers which are divisible by 7 and multiple of 5,
between 1500 and 2700 (both included).
Exercise 3
Write a Python program to prints all the integer numbers from 0 to 20 except 3 and 16. Note : Use
'continue' statement.
Exercise 4
Write a Python program to to count the number of even and odd numbers from a series of
numbers.
Sample numbers : numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Expected Output :
Number of even numbers : 5
Number of odd numbers : 4
Exercise 5
Write a Python program to calculate the following expression:
1 2 3 99
𝑀 = + + + ⋯+
2 3 4 100
6. Python Tuples
- Tuples are used to store multiple items in a single variable.
- A tuple is a collection which is ordered and unchangeable.
- Tuples are written with round brackets.
https://www.w3schools.com/python/python_tuples.asp
array1 = np.array([10,20,30,40,50])
array2 = np.arange(5)
print(array1 + array2)
print(array1 - array2)
print(array1 * array2)
Trinh Hung Cuong - Applied Calculus for IT - 501031 11/
13
Ton Duc Thang University
Faculty of Information Technology
print(array1*2)
print(array2**2)
Source: https://www.pluralsight.com/guides/overview-basic-numpy-operations
import numpy as np
https://numpy.org/doc/stable/reference/generated/numpy.arange.html
Exercise 6
Write a NumPy program to create an array with integer values ranging from 12 to 38.
Exercise 7
Write a NumPy program to print common values between two arrays.
8. References
- Python Tutorial on the W3schools website: https://www.w3schools.com/python/default.asp
- Python Tutorial on the Tutorials Point website:
https://www.tutorialspoint.com/python/index.htm
-- THE END --