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/10/05 13:33]
claudiu.dorobantu [Python basics]
aa:lab:introduction-to-python [2020/10/05 13:58] (current)
claudiu.dorobantu [Python basics]
Line 29: Line 29:
 </​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 48: Line 48:
 **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 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
  
Line 64: 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 3** 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.)
  
 **Remarks** **Remarks**
   * Classes are the usual means for storing heterogeneous data (e.g. for a 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.   * Classes are the usual means for storing heterogeneous data (e.g. for a 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> <code python>
-stud_info = ("​Mihai","​mihai@upb.ro",​["​AA","​PA","​SD","​Programare"​]) +stud_info = ("​Mihai",​ "​mihai@upb.ro",​ ["​AA",​ "​PA",​ "​SD",​ "​Programare"​]) 
-str = stud_info[0]+"'​s email is "​+stud_info[1]+str = stud_info[0] + "'​s email is " + stud_info[1]
 print(str) print(str)
 </​code>​ </​code>​
Line 81: Line 81:
 **Exercise 4** Write a function which takes a list of tuples containing student info and returns only those corresponding to students which have attended at least 3 lectures. **Exercise 4** Write a function which takes a list of tuples containing student info and returns only those corresponding to students which have attended at least 3 lectures.
  
-**Exercise 5** Write a function returns the number of unique characters from a list.+**Exercise 5** Write a function ​which returns the number of unique characters from a list.
  
 **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. **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.
Line 95: Line 95:
 **Exercise 8** 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"​.+For example, ''​find_patterns(["​ab",​ "​cd"​],​ "​abcdabcd"​)''​ should print out "​0,​2,​4,​6"​.
  
 **Remark**: **Remark**:
   * Matrices can be represented as lists of lists.   * Matrices can be represented as lists of lists.
 <code python> <code python>
-matrix = [[1,​2,​3],​[4,​5,​6],​[7,​8,​9]]+matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
 </​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 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 10** Write a function which adds two matrices.