|
| 1 | +# Day 2 - Python Loops, functions, modules and libraries |
| 2 | + |
| 3 | +Welcome to the second day of Python, and today we will cover some more concepts: |
| 4 | +- Loops |
| 5 | +- Functions |
| 6 | +- Modules and libraries |
| 7 | +- File I/O |
| 8 | + |
| 9 | +## Loops (for/while): |
| 10 | + |
| 11 | +Loops are used to repeatedly run a block of code. |
| 12 | + |
| 13 | +### for loop |
| 14 | + |
| 15 | +Using the `for` loop, a piece of code is executed once for each element of a sequence (such as a list, string, or tuple). Here is an example of a for loop that prints each programming language in a list: |
| 16 | + |
| 17 | +``` python |
| 18 | +languages = ['Python', 'Go', 'JavaScript'] |
| 19 | + |
| 20 | +# for loop |
| 21 | +for language in languages: |
| 22 | + print(language) |
| 23 | +``` |
| 24 | + |
| 25 | +Output |
| 26 | +``` |
| 27 | +Python |
| 28 | +Go |
| 29 | +JavaScript |
| 30 | +``` |
| 31 | + |
| 32 | +### while loop |
| 33 | + |
| 34 | +The `while loop` is used to execute a block of code repeatedly as long as a condition is True. Here's an example of a while loop that prints the numbers from 1 to 5: |
| 35 | + |
| 36 | +``` python |
| 37 | +i = 1 |
| 38 | +n = 5 |
| 39 | + |
| 40 | +# while loop from i = 1 to 5 |
| 41 | +while i <= n: |
| 42 | + print(i) |
| 43 | + i = i + 1 |
| 44 | +``` |
| 45 | + |
| 46 | +Output: |
| 47 | +``` |
| 48 | +1 |
| 49 | +2 |
| 50 | +3 |
| 51 | +4 |
| 52 | +5 |
| 53 | +``` |
| 54 | + |
| 55 | +## Functions |
| 56 | +Functions are reusable chunks of code with argument/parameters and return values. |
| 57 | +Using the `def` keyword in Python, you can define a function. In your programme, functions can be used to encapsulate complex logic and can be called several times. |
| 58 | +Functions can also be used to simplify code and make it easier to read. Here is an illustration of a function that adds two numbers: |
| 59 | + |
| 60 | +``` python |
| 61 | +# function has two arguments num1 and num2 |
| 62 | +def add_numbers(num1, num2): |
| 63 | + sum = num1 + num2 |
| 64 | + print('The sum is: ',sum) |
| 65 | +``` |
| 66 | + |
| 67 | +``` python |
| 68 | +# calling the function with arguments to add 5 and 2 |
| 69 | +add_numbers(5, 2) |
| 70 | + |
| 71 | +# Output: The sum is: 9 |
| 72 | +``` |
| 73 | + |
| 74 | +## Understanding Modules and Importing Libraries: |
| 75 | +A module is a file in Python that contains definitions and statements. Modules let you arrange your code and reuse it across many apps. |
| 76 | +The Standard Library, a sizable collection of Python modules, offers a wide range of capabilities, such as file I/O, regular expressions, and more. |
| 77 | +Additional libraries can be installed using package managers like pip. |
| 78 | +You must import a module or library using the import statement in order to use it in your programme. Here is an illustration of how to load the math module and calculate a number's square root using the sqrt() function: |
| 79 | + |
| 80 | +``` python |
| 81 | +import math |
| 82 | + |
| 83 | +print(math.sqrt(16)) # 4.0 |
| 84 | +``` |
| 85 | + |
| 86 | +## File I/O |
| 87 | +File I/O is used to read and write data to and from files on disk. |
| 88 | +The built-in Python function open() can be used to open a file, after which you can read from and write to it using methods like read() and write(). |
| 89 | +To save system resources, you should always close the file after you are done with it. |
| 90 | +An example of reading from a file and printing its content: |
| 91 | + |
| 92 | +``` python |
| 93 | +f = open("90DaysOfDevOps.txt", "r") |
| 94 | +print(f.read()) |
| 95 | +f.close() |
| 96 | +``` |
| 97 | + |
| 98 | +## Exception Handling: |
| 99 | + |
| 100 | +Exceptions are runtime errors that happen when your programme runs into unexpected circumstances, such dividing by zero or attempting to access a list element that doesn't exist. |
| 101 | +Using a try/except block, you can manage exceptions in Python. The try block's code is run, and if an exception arises, the except block's code is run to handle the exception. |
| 102 | + |
| 103 | +``` python |
| 104 | +try: |
| 105 | + f = open("90DaysOfDevOps.txt") |
| 106 | + try: |
| 107 | + f.write("Python is great") |
| 108 | + except: |
| 109 | + print("Something went wrong when writing to the file") |
| 110 | +``` |
| 111 | + |
| 112 | +## Conclusion |
| 113 | + |
| 114 | +That is it for today, I will see you tomorrow in Day 3 of Python! |
0 commit comments