Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
aa:lab:introduction-to-python [2020/09/29 13:02]
pdmatei
aa:lab:introduction-to-python [2020/10/05 13:58] (current)
claudiu.dorobantu [Python basics]
Line 13: Line 13:
 l.append([]) l.append([])
  
-print l+print(l)
 </​code>​ </​code>​
 **Remarks** **Remarks**
   * in Python we often use side-effects to modify data   * in Python we often use side-effects to modify data
-  * Python is not strongly-typed. ​As a consequence, lists may have different '​types'​ of elements+  * Python is strongly-typed. ​However, lists may have different '​types'​ of elements
  
 <code python> <code python>
-for i in range(0,​len(l)):​ +for i in range(0, len(l)): 
- print l[i]+ print(l[i])
  
-print range(0,​len(l))+print(list(range(0, len(l))))
  
 for elem in l: for elem in l:
- print elem+ print(elem)
 </​code>​ </​code>​
 **Remarks** **Remarks**
-  * List traversal may be achieved using indexing. Indexes are integers taken from a range. ''​range(0,​len(l))''​ is a list in itself.+  * List traversal may be achieved using indexing. Indexes are integers taken from a range. ''​range(0,​ len(l))''​ is a list in itself.
   * Lists (and other data structures) can be iterated using ''​in''​   * Lists (and other data structures) can be iterated using ''​in''​
  
Line 37: Line 37:
  
 <code python> <code python>
-def func (l1, l2):+def func(l1, l2):
  l1.append(3)  l1.append(3)
  return l1 + l2  return l1 + l2
 x = [1] x = [1]
 y = [2] y = [2]
-print func(x,y) +print(func(x, y)
-print x+print(x)
 </​code>​ </​code>​
  
 **Remarks** **Remarks**
   * although it is possible to add type annotations,​ in Python a function'​s signature only consists of the number of parameters and their names   * although it is possible to add type annotations,​ in Python a function'​s signature only consists of the number of parameters and their names
-  * objects are generally ​are passed as reference (hence, when printing ''​x'',​ we see the list ''​[1,​3]''​) (for details see: [[https://​docs.python.org/​3/​reference/​datamodel.html | Python data model]])+  * objects are generally passed as reference (hence, when printing ''​x'',​ we see the list ''​[1,​ 3]''​) (for details see: [[https://​docs.python.org/​3/​reference/​datamodel.html | Python data model]])
   * ''​+''​ denotes list concatenation   * ''​+''​ denotes list concatenation
  
-**Exercise ​X** +**Exercise ​1** Write a function which determines the maximum number ​from a list.
-Write a function which prints EACH repeating character ​from a string. (Hint: strings are lists of characters).+
  
-==== Useful data structures: dictionaries and sets ====+**Exercise 2** Write a function which prints EACH repeating character from a string. (Hint: strings are lists of characters). 
 + 
 +==== Useful data structures: dictionaries and tuples ​====
  
 Dictionaries are another useful data structure. A dictionary is a ''<​key>​ : <​value>''​ mapping. Unlike lists, keys may be of any type (integers, strings, or any other datatype). Dictionaries are another useful data structure. A dictionary is a ''<​key>​ : <​value>''​ mapping. Unlike lists, keys may be of any type (integers, strings, or any other datatype).
Line 63: Line 64:
  
 if "​X"​ in d: if "​X"​ in d:
- print "d[X] is defined in the dictionary"​+ print("d[X] is defined in the dictionary"​)
 if not "​Y"​ in d: if not "​Y"​ in d:
-        print "d[Y] is not defined in the dictionary"​+        print("d[Y] is not defined in the dictionary"​)
 </​code>​ </​code>​
  
-**Exercise ​X** Write a function returns the number of repetitions of each character from a text. (Hint: use dictionaries to store the number of repetitions.)+**Exercise ​3** Write a function ​which returns the number of repetitions of each character from a text. (Hint: use dictionaries to store the number of repetitions.)
  
-**Exercise X** Write function returns ​the number ​of unique characters from list.+**Remarks** 
 +  * Classes are the usual means for storing heterogeneous data (e.g. for student, his name, email, list of attended lectures, etc.). However it is sometimes easier to use tuples instead. The example below illustrates ​the creation ​of a tuple and accessing parts of it. 
 +<code python>​ 
 +stud_info = ("​Mihai",​ "​mihai@upb.ro", ["​AA",​ "​PA",​ "​SD",​ "​Programare"​]) 
 +str = stud_info[0] + "'​s email is " + stud_info[1] 
 +print(str) 
 +</​code>​
  
-Remark: +**Exercise 4** Write function which takes a list of tuples containing student info and returns only those corresponding to students which have attended at least lectures.
-  ​a simpler way to ensure uniqueness is to use sets. +
-  ​* a set may be created from a list:  ''​s = set([1,2,3,​1])''​ +
-  * and in turn, a list may be created from a set: ''​l = list(s)''​+
  
-**Exercise ​X** Write a function which takes pattern and a text and prints all indexes where an occurrence of pattern in text are found.+**Exercise ​5** Write a function which returns the number of unique characters from list.
  
-Remark:+**Exercise 6** Write a function which takes a pattern and a text and prints all indexes where an occurrence of pattern in text are found. 
 + 
 +**Remark**:
   * lists can be sliced in Python using the following syntax: ''​l[start_index:​end_index]''​ Test it to see how slicing is performed.   * lists can be sliced in Python using the following syntax: ''​l[start_index:​end_index]''​ Test it to see how slicing is performed.
  
-**Exercise ​X** Modify the previous implementation to use slicing.+**Exercise ​7** Modify the previous implementation to use slicing.
  
-Remark:+**Remark**:
   * in Python we can use arbitrarily nested functions   * in Python we can use arbitrarily nested functions
  
-**Exercise ​X** Write a function which searches for a list of patterns in a text.+**Exercise ​8** Write a function which searches for a list of patterns in a text.
 <code python> <code python>
-def find_patterns (pattern_list,​ text):+def find_patterns(pattern_list,​ text):
  # checks if pattern is found at position index in text  # checks if pattern is found at position index in text
- def inner_search (pattern,​index):​+ def inner_search(pattern,​ index):
 </​code>​ </​code>​
 +For example, ''​find_patterns(["​ab",​ "​cd"​],​ "​abcdabcd"​)''​ should print out "​0,​2,​4,​6"​.
  
-Remark: +**Remark**
-  * Python supports functional-style programming to some extent.+  * Matrices can be represented as lists of lists.
 <code python> <code python>
-def plus1(x): +matrix = [[1, 2, 3], [456], [7, 8, 9]]
- return x + 1 +
-print map(plus1,[1,2,3]+
-print map(lambda x:x+1, [1,2,3])+
 </​code>​ </​code>​
 +
 +**Exercise 9** Write a function which returns the elements from the principal diagonal of a matrix, as a list. Example: ''​diag(matrix) = [1, 5, 9]''​.
 +
 +**Exercise 10** Write a function which adds two matrices.
 +
 +**Exercise 11** Write a function which implements matrix transposition.
 +
 +**Exercise 12** Write a function which multiplies two matrices.
 +