This is an old revision of the document!
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++
Libraries are commonly installed using the pip package manager if they are not already included in the Anaconda distribution:
Installing a library (e.g. numpy) in Python is as simple as running the following command in the terminal:
pip install numpy
print("hello world")
python lab1.py
*The Python version should be specified if there are multiple distributions installed (e.g. 2.7, 3.6, 3.7) such as (depending on the installed environment and distribution):
python3 lab1.py python3.7 lab1.py py -3 lab1.py
The same applies for installing packages using pip:
pip install numpy pip3 install numpy pip3.7 install numpy py -3 -m pip install numpy
The current lab setup is using Linux (Ubuntu) and python 3.7.4 installed with the required packages. Use the following command variant for running python:
python3.7 script.py
For installing additional packages (e.g. numpy)
pip3.7 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
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
Tip: 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
T7 (2p) Having a list of numbers, write a Python program to print only even numbers one by one
Tip: Using lists, for loop, if else, % operator
While loops are used to iterate until a condition that is true becomes false.
while exp: # do something