Welcome to the second part of the learning python self-paced course. On this page, we will learn about programming fundamentals. Below is a brief description of what you will learn and various tabs. Go through the different tabs below: Files, If/Else, Loops, Documentation, and Functions. Under the Sample Code subheading you will find a link to get to the code, so you can start learning hands-on.
There are fundamental programming techniques that span over every programming language. Code needs to interact with files either to retrieve data, create files, or to append them. If/else statements are used to categorize inputs. Loops are extremely useful at repeating a the same task multiple times. Reading and writing code documentation helps with understanding of a script. Additionally, creating functions helps condense code and make it easier to read.
In this section you will learn the syntax and intuition for the fundamentals mentioned above.
File handling is an important fundamental that allows you to create, append, and extract information from files.
In order to create a file, you must open it, modify it, and close it. For example:
myfile = open('myfile.txt', 'w') # Open file with 'w' which is to write the document
myfile.write('Hello World!') # Write something in the file.
myfile.close() # Always close the document!!
Instead of copying and pasting data into code or typing it all out, Python can load it automatically and assign it to a variable.
This sample code goes through more detail of the syntax and other functions. Go through it carefully and try modifying the code to see what happens. You can get the code from:
If and if/else statements are coding structures that allow you to run pieces of code only if a condition is met.
In order to create an if else statement, it starts with the word "if", followed by a statement that can be evaluated for being True or False, and then a colon. The next line should be indented and that will be implemented if the statement is True. For example:
x = 2
if x > 5: # If this statement is true then the following statement will run.
print('x is greater than 5') # If x > 5 then this will print.
else: # If the statement is false then it will move down to this part of the code.
print('x is equal or less than 5') # and this will print.
[output] x is equal or less than 5
If/else statements help you run code only when a condition is met. It adds decision making to your code.
This sample code goes through more detail of the syntax and other functions. Go through it carefully and try modifying the code to see what happens. You can get the code from:
Loops are iterating objects that can perform a task multiple times. Programing with loops can save a lot of time, since it requires less lines of code.
In order to create a loop there must be a statement that explains what is being looped through, followed by a colon (:). Then the next line must be indented and that will be implemented for a specific instance in the loop. The example below shows a list of fruits. In the for loop it will assign each element in fruits and rename it fruit. So in the first part of the loop, fruit equals apple, then it goes to the indented part of the code where it will print apple. Then the program will loop back to the top and pick up banana and then print that. The loop will keep going till it reaches the end of fruits.
fruits = ['apple','banana','pear']
for fruit in fruits: # Loops start with either for or while. There must be a colon at the end.
print(fruit) # The next line must be indented. Each string will print from the string fruits.
[output] apple
[output] banana
[output] pear
Loops are useful because you can repeat a task multiple times with only a few lines of code. For the above example, without a loop the code would look like this:
fruits = ['apple', 'banana', 'pear']
print(fruits[0])
print(fruits[1])
print(fruits[2])
This is more lines of code than a loop. Imagine if the list had 100 or more elements in it. A loop can handle the information faster and it only needs a few lines of code.
This sample code goes through more detail of the syntax and other functions. Go through it carefully and try modifying the code to see what happens. You can get the code from:
Documentation can be created in a few different ways in order to describe the code that you create and makes it easier to understand.
Documentation makes code easier to understand and maintain. Additionally, having documentation allows for easier reusability of the code for yourself and others.
This sample code goes through more detail of the syntax and other functions. Go through it carefully and try modifying the code to see what happens. You can get the code from:
In Python, you use functions to change data around, but you can also create your own! Custom functions give you a lot of flexibility and save a lot of coding time.
To create a function you have to start with the word def and then create a name for your function. The example below uses "add_two". Then you need parentheses with a variable(s) that you will use inside the function. Inside the function you'll have the code and something that says return if you want to output a result. For example:
def add_two(number):
"""
Add 2 to the given number and return the result.
"""
return number + 2
# How to use function
result = add_two(5)
print(result)
[output] 7
Functions are great for saving time and space with your programming. They help organize your code in a more coherent matter, so that you have defined functions first and then code that uses the functions. It also helps with the readability of your code. Functions can be saved in separate files and called in your code, which allows you to use them across different projects.
This sample code goes through more detail of the syntax and other functions. Go through it carefully and try modifying the code to see what happens. You can get the code from: