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 09:48] 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 1** Write a function which determines the maximum number from a list. | **Exercise 1** Write a function which determines the maximum number from a list. | ||
+ | |||
**Exercise 2** Write a function which prints EACH repeating character from a string. (Hint: strings are lists of characters). | **Exercise 2** Write a function which prints EACH repeating character from a string. (Hint: strings are lists of characters). | ||
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 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 80: | 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. | ||
- | Remark: | + | **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 7** 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 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. |