Python is a widely used high-level interpreted programming language for general-purpose programming. Python 3.x will be used which presents some changes in terms of syntax and lots of improvements from Python 2.x, while the two major versions are not backward compatible.
Anaconda is a distribution of Python with other packages needed for Data Mining.
Visual Studio Code (VS Code) is a code editor with support for most programming languages. Alternative: you can use any text editor e.g. Notepad++ (not recommended) or a specialized python IDE e.g. PyCharm.
Libraries are commonly installed using the pip package manager if they are not already included in the Anaconda distribution:
pip install numpy
print("hello world")
python lab1.py
# on linux python3 lab1.py python3.7 lab1.py # on windows py -3 lab1.py
The same applies for installing packages using pip:
# on linux pip install numpy pip3 install numpy pip3.7 install numpy # on windows py -3 -m pip install numpy
Python is an interpreted dynamically-typed language. Data type specification is not required when creating a variable (but can be specified in modern Python). Creating a variable is as simple as:
a = 10
a: int = 10
*Why would types be useful in Python?
Numbers in Python can be integers or floating point (real) and also Decimal and Fraction.
Operations:
4+5=9, 4.3+4.2=8.5
4-5=-1, 4.3-8.2=-3.9
5/2=2.5
5//2=2
5%2=1
3**4=81
# create a numeric variable a = 10 b = 20 # print the value print(a) # addition s = a + b print(s) # fractions from fractions import Fraction print(Fraction(16, -10)) # decimal from decimal import * print(Decimal(1) / Decimal(7)) print(1/7)
Strings are used to represent text or a sequence of characters. Python can operate with strings which can be expressed in several ways
# declaring strings s1 = 'Python' s2 = "Programing" # declaring unicode strings: us1 = u"Ciprian Truică" us2 = u'Ciprian Truică'
T1 (1p) Using s1 and s2, print the following string: “Python Programming”
T2 (1p) Print the length of the string
T3 (1p) Replace “Python” with “Fun” using replace function
T4 (1p) Split the string in two words using split function
Operations:
s3 = s1 + " " + s2
s4 = "alibec" * 3
s3[5]
len(s3)
s3[2:4]
s3[:n]
s3[len(s3)-n:] s3[-n:]
Functions:
len(s3)
s3.replace("Python", "Fun")
s3.split(" ")
s3[2] = "a"
Python knows several compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets.
list1 = [1, 4, 9, 25] list2 = [36, 49, 65, 81, 100]
Operations:
list3 = list1 + list2
list4 = list1 * 3
list3[5]
len(list3)
list3[2:4]
list3[:n]
list3[len(list3)-n:] list3[-n:]
list2[2] = 65
Functions:
len(list1)
list1.append(64)
list1.reverse()
list1.sort()
newlist = [x*10 for x in list1]
# create a list from given start/end values list(range(1,10)) # create a list from given number of elements list(range(10))
Matrices are lists of lists:
m = [ [1, 2], [3, 4] ]
In computer programming, a statement is a syntactic unit of a programming language that expresses some action to be carried out. Examples of statements in Python include if else, for in, while
If else statements are used to allow programmers to ask questions and then, based on the result, perform different actions.
if exp1: # do something elif exp2: # do something else else: # do something else
Hint: use the input function to get the user input:
n=int(input("Enter a number"))
Use the % operator to check if the number is divisible by 2
For loops are used to iterate though a list of values, useful when performing repeated actions with a defined number of iterations.
for elem in list1: # do something # print the element in the list print(elem)
T7 (1p) Having a list of numbers, write a Python program to print only even numbers one by one
Hint: Using lists, for loop, if else, % operator
While loops are used to iterate until a condition that is true becomes false. The condition is evaluated before the execution of the block.
while exp: # code block # do something
counter = 1 while counter < 10: print(counter) counter = counter + 1
You can force exit a repetitive loop (for, while) at any iteration by using break
while exp: # do something if condition: break
counter = 1 while counter < 10: print(counter) counter = counter + 1 if counter > 5: break
T9 (1p) Using a while loop, print the digits of a number one by one, e.g. 12345 has the following digits: 1,2,3,4,5
Hint: Use the % operator to get the last digit of a number divided by 10
12345 % 10 = 5
Then perform integer division by 10 until there are no more digits
12345 // 10 = 1234
T10 (1p) In T9, you (most probably) printed the digits in reverse order. Using a list and a for loop, print the digits in the correct order. You may use the following method to reverse a list
list.reverse()