Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
aa:lab:introduction-to-python [2020/10/05 13:38] claudiu.dorobantu [Useful data structures: dictionaries and tuples] |
aa:lab:introduction-to-python [2020/10/05 13:58] (current) claudiu.dorobantu [Python basics] |
||
---|---|---|---|
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 74: | Line 74: | ||
* 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 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. |