This shows you the differences between two versions of the page.
ewis:laboratoare:01 [2020/02/21 10:11] alexandru.predescu |
ewis:laboratoare:01 [2021/03/17 17:07] (current) alexandru.predescu |
||
---|---|---|---|
Line 9: | Line 9: | ||
**Anaconda** is a distribution of Python with other packages needed for Data Mining. | **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++ | + | **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: | + | Libraries are commonly installed using the **pip** package manager if they are not already included in the Anaconda distribution: |
* **NumPy** is the fundamental package for scientific computing with Python. | * **NumPy** is the fundamental package for scientific computing with Python. | ||
Line 23: | Line 23: | ||
* **Matplotlib** is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. | * **Matplotlib** is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. | ||
- | Installing a library (e.g. numpy) in Python is as simple as running the following command in the terminal: | + | <note>Installing a library (e.g. numpy) in Python is as simple as running the following command in the terminal: |
- | <code>pip install numpy</code> | + | <code>pip install numpy</code></note> |
+ | |||
+ | <hidden>* Install Anaconda e.g. https://www.anaconda.com/distribution/ (already installed)</hidden> | ||
==== Setup ==== | ==== Setup ==== | ||
- | * Install Anaconda e.g. https://www.anaconda.com/distribution/ (already installed) | + | * Install Python 3.6.8 ([[https://www.python.org/downloads/release/python-368/|download]]) |
* Create a new file and save it as e.g. lab1.py | * Create a new file and save it as e.g. lab1.py | ||
* Open with e.g. Notepad++ | * Open with e.g. Notepad++ | ||
Line 35: | Line 37: | ||
* Open the terminal in the current folder (Shift+Right click>Open command window here) and run the program from the command line e.g. <code>python lab1.py</code> | * Open the terminal in the current folder (Shift+Right click>Open command window here) and run the program from the command line e.g. <code>python lab1.py</code> | ||
- | <note important>Python uses indentation to parse code blocks instead of curly brackets {} as in other programming languages. The correct indentation means using the same convention, either tabs or spaces and not both</note> | + | <hidden><note important>Python uses indentation to parse code blocks instead of curly brackets {} as in other programming languages. The correct indentation means using the same convention, either tabs or spaces and not both</note></hidden> |
+ | |||
+ | <note>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: | ||
+ | |||
+ | <code> | ||
+ | |||
+ | # on linux | ||
+ | python3 lab1.py | ||
+ | python3.7 lab1.py | ||
+ | |||
+ | # on windows | ||
+ | py -3 lab1.py | ||
+ | |||
+ | </code> | ||
+ | |||
+ | The same applies for installing packages using pip: | ||
+ | |||
+ | <code> | ||
+ | |||
+ | # on linux | ||
+ | pip install numpy | ||
+ | pip3 install numpy | ||
+ | pip3.7 install numpy | ||
+ | |||
+ | # on windows | ||
+ | py -3 -m pip install numpy | ||
+ | |||
+ | </code></note> | ||
+ | |||
+ | <hidden><note important> | ||
+ | |||
+ | 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: | ||
+ | |||
+ | <code>python3.7 script.py</code> | ||
+ | |||
+ | For installing additional packages (e.g. numpy) | ||
+ | |||
+ | <code>pip3.7 install numpy</code> | ||
- | *The Python version should be specified if there are multiple distributions installed (e.g. 2.7, 3.6) such as: <code>py -3 lab1.py</code> | + | </note></hidden> |
==== Data types in Python ==== | ==== Data types in Python ==== | ||
Line 75: | Line 114: | ||
# fractions | # fractions | ||
from fractions import Fraction | from fractions import Fraction | ||
+ | |||
print(Fraction(16, -10)) | print(Fraction(16, -10)) | ||
Line 97: | Line 137: | ||
# declaring unicode strings: | # declaring unicode strings: | ||
- | us1=u"Ciprian Truică" | + | us1 = u"Ciprian Truică" |
- | us2=u'Ciprian Truică' | + | us2 = u'Ciprian Truică' |
</code> | </code> | ||
Line 131: | Line 171: | ||
* **split** is used for splitting a string into substrings based on a given token/delimiter <code python>s3.split(" ")</code> | * **split** is used for splitting a string into substrings based on a given token/delimiter <code python>s3.split(" ")</code> | ||
- | <note tip>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: <code python>s3[2]="a"</code></note> | + | <note tip>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: <code python>s3[2] = "a"</code></note> |
=== Lists === | === Lists === | ||
Line 154: | Line 194: | ||
<note tip>Index numbers start with 0. List operations are similar with string operations. Lists are a mutable type, it is possible to change their content: <code python>list2[2] = 65</code></note> | <note tip>Index numbers start with 0. List operations are similar with string operations. Lists are a mutable type, it is possible to change their content: <code python>list2[2] = 65</code></note> | ||
+ | |||
+ | Functions: | ||
+ | |||
+ | * **len** returns the length of the list <code python>len(list1)</code> | ||
+ | * **append** is used for adding an element at the end of a list<code python>list1.append(64)</code> | ||
+ | * **reverse** is used for reversing the elements of the list<code python>list1.reverse()</code> | ||
+ | * **sort** is used for ordering the list<code python>list1.sort()</code> | ||
+ | * List comprehension is used to create a new list based on existing list elements<code>newlist = [x*10 for x in list1]</code> | ||
+ | * **range** can be used to create a list of numbers<code> | ||
+ | # create a list from given start/end values | ||
+ | list(range(1,10)) | ||
+ | |||
+ | # create a list from given number of elements | ||
+ | list(range(10)) | ||
+ | </code> | ||
+ | * other methods can be found [[https://www.w3schools.com/python/python_lists_methods.asp|here]] | ||
=== Matrices === | === Matrices === | ||
Line 167: | Line 223: | ||
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// | 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 === | + | === 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 else statements are used to allow programmers to ask questions and then, based on the result, perform different actions. | ||
Line 181: | Line 237: | ||
<note> | <note> | ||
- | **T5 (2p)** Write a simple program that asks for a number and prints if the number is even or odd | + | **T5 (1p)** Write a simple program that asks for a number and prints if the number is even or odd |
- | Tip: use the **input** function to get the user input: <code python>n=int(input("Enter a number")) | + | Hint: use the **input** function to get the user input: <code python>n=int(input("Enter a number")) |
</code> | </code> | ||
Use the **%** operator to check if the number is divisible by 2 | Use the **%** operator to check if the number is divisible by 2 | ||
</note> | </note> | ||
- | === for in === | + | === 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 loops are used to iterate though a list of values, useful when performing repeated actions with a defined number of iterations. | ||
Line 195: | Line 251: | ||
for elem in list1: | for elem in list1: | ||
# do something | # do something | ||
+ | # print the element in the list | ||
+ | print(elem) | ||
</code> | </code> | ||
<note> | <note> | ||
- | **T6 (2p)** Having a list of numbers, use a for loop to print the numbers one by one | + | **T6 (1p)** Having a list of numbers, use a for loop to print the numbers one by one |
- | **T7 (2p)** Having a list of numbers, write a Python program to print all even numbers one by one | + | **T7 (1p)** 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 | + | Hint: Using lists, for loop, if else, **%** operator |
</note> | </note> | ||
- | === while === | + | === Repetitive structures. while === |
- | While loops are used to iterate until a condition that is true becomes false. | + | While loops are used to iterate until a condition that is true becomes false. The condition is evaluated before the execution of the block. |
<code python> | <code python> | ||
while exp: | while exp: | ||
+ | # code block | ||
# do something | # do something | ||
</code> | </code> | ||
+ | |||
+ | <note tip>Here is an example: | ||
+ | <code python> | ||
+ | counter = 1 | ||
+ | while counter < 10: | ||
+ | print(counter) | ||
+ | counter = counter + 1 | ||
+ | </code> | ||
+ | </note> | ||
+ | |||
+ | You can force exit a repetitive loop (for, while) at any iteration by using //break// | ||
+ | |||
+ | <code python> | ||
+ | while exp: | ||
+ | # do something | ||
+ | if condition: | ||
+ | break | ||
+ | </code> | ||
+ | |||
+ | <note tip>Here is an example: | ||
+ | <code python> | ||
+ | counter = 1 | ||
+ | while counter < 10: | ||
+ | print(counter) | ||
+ | counter = counter + 1 | ||
+ | if counter > 5: | ||
+ | break | ||
+ | </code> | ||
+ | </note> | ||
+ | |||
+ | <note> | ||
+ | **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<code>12345 % 10 = 5</code>Then perform integer division by 10 until there are no more digits<code>12345 // 10 = 1234</code> | ||
+ | |||
+ | **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 | ||
+ | |||
+ | <code>list.reverse()</code> | ||
+ | |||
+ | </note> | ||
+ | |||
+ | ==== Resources ==== | ||
+ | |||
+ | * {{:ewis:laboratoare:python_workflow.pdf|Python Workflow: Installing and using Python}} | ||