Table of Contents

Lab 1 - Introduction to Python

What we need

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:

Installing a library (e.g. numpy) in Python is as simple as running the following command in the terminal:

pip install numpy

Setup

The Python version should be specified if there are multiple distributions installed (e.g. 2.7, 3.6, 3.7), according to the installed environment (Windows/Linux) and distribution:

# 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

Data types in Python

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

Numbers in Python can be integers or floating point (real) and also Decimal and Fraction.

Operations:

# 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

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:

Functions:

Index numbers start with 0. Strings are immutable, meaning that the content of a string cannot be changed. This does not work for a string:

s3[2] = "a"

Lists

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:

Index numbers start with 0. List operations are similar with string operations. Lists are a mutable type, it is possible to change their content:

list2[2] = 65

Functions:

Matrices

Matrices are lists of lists:

m = [ [1, 2], [3, 4] ]

Basic control statements

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

Decision structures. if else

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

T5 (1p) Write a simple program that asks for a number and prints if the number is even or odd

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

Repetitive structures. for in

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)

T6 (1p) Having a list of numbers, use a for loop to print the numbers one by one

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

Repetitive structures. while

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

Here is an example:

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

Here is an example:

counter = 1
while counter < 10:
   print(counter)
   counter = counter + 1
   if counter > 5:
      break

T8 (1p) Using a while loop, print the number of digits in an integer number, e.g. 12345 has 5 digits

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()

Resources