This shows you the differences between two versions of the page.
ewis:laboratoare:01 [2021/03/09 21:19] alexandru.predescu |
ewis:laboratoare:01 [2021/03/17 17:07] (current) alexandru.predescu |
||
---|---|---|---|
Line 194: | Line 194: | ||
<note tip>Index numbers start with 0. List operations are similar with string operations. Lists are a mutable type, it is possible to change their content: <code python>list2[2] = 65</code></note> | <note tip>Index numbers start with 0. List operations are similar with string operations. Lists are a mutable type, it is possible to change their content: <code python>list2[2] = 65</code></note> | ||
+ | |||
+ | Functions: | ||
+ | |||
+ | * **len** returns the length of the list <code python>len(list1)</code> | ||
+ | * **append** is used for adding an element at the end of a list<code python>list1.append(64)</code> | ||
+ | * **reverse** is used for reversing the elements of the list<code python>list1.reverse()</code> | ||
+ | * **sort** is used for ordering the list<code python>list1.sort()</code> | ||
+ | * List comprehension is used to create a new list based on existing list elements<code>newlist = [x*10 for x in list1]</code> | ||
+ | * **range** can be used to create a list of numbers<code> | ||
+ | # create a list from given start/end values | ||
+ | list(range(1,10)) | ||
+ | |||
+ | # create a list from given number of elements | ||
+ | list(range(10)) | ||
+ | </code> | ||
+ | * other methods can be found [[https://www.w3schools.com/python/python_lists_methods.asp|here]] | ||
=== Matrices === | === Matrices === | ||
Line 223: | Line 239: | ||
**T5 (1p)** Write a simple program that asks for a number and prints if the number is even or odd | **T5 (1p)** Write a simple program that asks for a number and prints if the number is even or odd | ||
- | Tip: use the **input** function to get the user input: <code python>n=int(input("Enter a number")) | + | Hint: use the **input** function to get the user input: <code python>n=int(input("Enter a number")) |
</code> | </code> | ||
Use the **%** operator to check if the number is divisible by 2 | Use the **%** operator to check if the number is divisible by 2 | ||
Line 244: | Line 260: | ||
**T7 (1p)** Having a list of numbers, write a Python program to print **only** even numbers one by one | **T7 (1p)** Having a list of numbers, write a Python program to print **only** even numbers one by one | ||
- | Tip: Using lists, for loop, if else, **%** operator | + | Hint: Using lists, for loop, if else, **%** operator |
</note> | </note> | ||
Line 266: | Line 282: | ||
</note> | </note> | ||
- | You can force exit a repetitive loop (for, while) at any iteration by using break: | + | You can force exit a repetitive loop (for, while) at any iteration by using //break// |
<code python> | <code python> | ||
Line 281: | Line 297: | ||
print(counter) | print(counter) | ||
counter = counter + 1 | counter = counter + 1 | ||
- | if counter == 6: | + | if counter > 5: |
break | break | ||
</code> | </code> | ||
Line 289: | Line 305: | ||
**T8 (1p)** Using a while loop, print the number of digits in an integer number, e.g. 12345 has 5 digits | **T8 (1p)** Using a while loop, print the number of digits in an integer number, e.g. 12345 has 5 digits | ||
- | **T9 (1p)** Using a while loop, print the digits of a number one by one, e.g. 12345 has the following digits 1,2,3,4,5 | + | **T9 (1p)** Using a while loop, print the digits of a number one by one, e.g. 12345 has the following digits: 1,2,3,4,5 |
+ | |||
+ | Hint: Use the **%** operator to get the last digit of a number divided by 10<code>12345 % 10 = 5</code>Then perform integer division by 10 until there are no more digits<code>12345 // 10 = 1234</code> | ||
+ | |||
+ | **T10 (1p)** In T9, you (most probably) printed the digits in reverse order. Using a list and a for loop, print the digits in the correct order. You may use the following method to reverse a list | ||
+ | |||
+ | <code>list.reverse()</code> | ||
- | Tip: Use the **%** operator to get the last digit of a number divided by 10<code>12345 % 10 = 5</code>Then perform integer division by 10 until there are no more digits<code>12345 // 10 = 1234</code> | ||
</note> | </note> | ||
==== Resources ==== | ==== Resources ==== | ||
- | * {{:ewis:laboratoare:python_workflow.pdf|Python Workflow}} | + | * {{:ewis:laboratoare:python_workflow.pdf|Python Workflow: Installing and using Python}} |