Differences

This shows you the differences between two versions of the page.

Link to this comparison view

ewis:laboratoare:02 [2021/03/17 16:38]
alexandru.predescu [Functions]
ewis:laboratoare:02 [2023/03/15 17:40] (current)
alexandru.predescu [Data Structures]
Line 24: Line 24:
 </​code>​ </​code>​
  
-**T1 (2p)** Having a list of numbers, write a function to calculate the sum of all elements in the list.+**T1 (1p)** Having a list of numbers, write a function to calculate the sum of all elements in the list.
 Tip: List given as input, using a for loop Tip: List given as input, using a for loop
 </​note>​ </​note>​
Line 79: Line 79:
         new_elem = elem / s         new_elem = elem / s
         new_list.append(new_elem)         new_list.append(new_elem)
 +        ​
 +    # TODO: same task with list comprehension ​   ​
 +    ​
     return new_list     return new_list
     ​     ​
Line 85: Line 88:
 **T2.1 (1p)** Use the functions to compute the arithmetic mean, weighted arithmetic mean, variance, standard deviation and L2-norm normalization of the list **T2.1 (1p)** Use the functions to compute the arithmetic mean, weighted arithmetic mean, variance, standard deviation and L2-norm normalization of the list
  
-**T2.2 (1p)** Implement the functions using list comprehension,​ without using for loops+**T2.2 (2p)** Implement the functions using list comprehension,​ without using for loops
  
 </​note>​ </​note>​
Line 95: Line 98:
 <​note>​ <​note>​
  
-//​module1.py//​+**//​module1.py//​**
  
 <code python> <code python>
 def hello_name(name):​ def hello_name(name):​
     print("​Hello " + name)     print("​Hello " + name)
-    return 
 </​code>​ </​code>​
  
-//main.py//+**//main.py//**
  
 <code python> <code python>
Line 109: Line 111:
 from module1 import hello_name from module1 import hello_name
 hello_name("​Alex"​) hello_name("​Alex"​)
 +
 # import all resources from a module # import all resources from a module
 from module1 import * from module1 import *
 hello_name("​Alex"​) hello_name("​Alex"​)
 +
 # import the module # import the module
 import module1 import module1
 module1.hello_name("​Alex"​) module1.hello_name("​Alex"​)
 +
 # import the module using an alias # import the module using an alias
 import module1 as m import module1 as m
Line 120: Line 125:
 </​code>​ </​code>​
  
-**T3 (2p)** Create two Python files (.py) as in the example. Run the program //main.py// and check the output in the console+**T3 (2p)** Create two Python files (.py) in the same project folder, ​as shown in the example. Run the program //main.py// and check the output in the console
  
 </​note>​ </​note>​
Line 130: Line 135:
 === Lists === === Lists ===
  
-List is one of the most frequently used and very versatile datatype used in Python. As opposed to more traditional arrays, used to store elements having the same data-type, Python lists do not have such constrain. Lists are discussed in the previous lab ([[ewis:​laboratoare:​01|]]),​ showing how to create, read, update and delete (CRUD) contained elements using basic operators. In Python, the List type also contains some useful methods: append, extend, insert, remove, pop, clear, index, count, sort, reverse, copy+List is one of the most frequently used and very versatile datatype used in Python. As opposed to more traditional arrays, used to store elements having the same data-type, Python lists do not have such constrain. Lists are discussed in the previous lab ([[ewis:​laboratoare:​01|]]),​ showing how to create, read, update and delete (CRUD) contained elements using basic operators. In Python, the List type also contains some useful methods: append, extend, insert, remove, pop, clear, index, count, sort, reverse, copy.
  
 <​note>​ <​note>​
Line 136: Line 141:
 my_list = [1, 2, 3, 4, 5] my_list = [1, 2, 3, 4, 5]
 print(my_list) print(my_list)
 +
 # add an element to the list # add an element to the list
 my_list.append(6) my_list.append(6)
 print(my_list) print(my_list)
 +
 # combine lists # combine lists
 my_other_list = [7, 8, 9, 10] my_other_list = [7, 8, 9, 10]
 my_list.extend(my_other_list) my_list.extend(my_other_list)
 print(my_list) print(my_list)
 +
 # sort list (descending) # sort list (descending)
 my_list.sort(reverse=True) my_list.sort(reverse=True)
 print(my_list) print(my_list)
 +
 +# check if an element is found in the list
 +# (python specific, normally used with sets or dictionaries)
 +print(1 in my_list)
 +print(10 in my_list)
 </​code>​ </​code>​
 </​note>​ </​note>​
Line 155: Line 168:
  
 <​note>​ <​note>​
-Create, access, check membership+**Create, access, check membership**
  
 <code python> <code python>
Line 170: Line 183:
 </​code>​ </​code>​
  
-Modify+**Modify**
  
 <code python> <code python>
Line 194: Line 207:
 </​code>​ </​code>​
  
-Operations (set theory)+**Operations (set theory)**
  
 <code python> <code python>
Line 230: Line 243:
 **T4 (4p)** The set is a collection of unordered (and unique) elements. Use this to your advantage to remove duplicates from a list **T4 (4p)** The set is a collection of unordered (and unique) elements. Use this to your advantage to remove duplicates from a list
  
-Tip+Hint
-  *you can create a set from a list +  *you can create a set from a list: ''​s = set([1,​2,​3])''​ 
-  *you can create a list from a set+  *you can create a list from a set: ''​v = list({1, 2, 3})''​
  
 </​note>​ </​note>​
Line 265: Line 278:
 # looping through a dictionary: # looping through a dictionary:
 for key in d: for key in d:
- print(key) ​ # print only key +   print(key) ​ # print only key 
-    print(key, d[key]) ​ # print key and value d[key]+   ​print(key, d[key]) ​ # print key and value d[key]
  
  
Line 284: Line 297:
 else: else:
     print('​not found'​)     print('​not found'​)
 +
 +</​code>​
 +
 +<code python>
  
 # counting the number of times a letter appears in a string: # counting the number of times a letter appears in a string:
Line 308: Line 325:
 **T5 (4p - bonus)** You have a list of numbers. Write a program to show the frequency of each number as the number of times that number is found in the list. **T5 (4p - bonus)** You have a list of numbers. Write a program to show the frequency of each number as the number of times that number is found in the list.
  
-Tip: +Hint
- +  * Use dictionaries to keep track of each element
-  *Use dictionaries to keep track of each element+
 </​note>​ </​note>​
  
 **Resources**:​ **Resources**:​
  
-https://www.youtube.com/watch?​v=WGlMlS_Yydk +  * [[http://mathworld.wolfram.com/L2-Norm.html|L^2-Norm]] 
- +  * [[http://​mathworld.wolfram.com/​StandardDeviation.html|Standard Deviation]] 
-http://​mathworld.wolfram.com/​L2-Norm.html+  * [[https://​www.youtube.com/​watch?​v=WGlMlS_Yydk|Apriori Algorithm (Associated Learning) - Fun and Easy Machine Learning]]
  
-http://​mathworld.wolfram.com/​StandardDeviation.html 
  
  
  
ewis/laboratoare/02.1615991904.txt.gz · Last modified: 2021/03/17 16:38 by alexandru.predescu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0