Régis N.

Nov 2023

Within Tech | Python programming

2 hrs Python

Intro

Hello Everyone! So today we;re going to get out of boring things like introductions and get into programming very well. Today, we're going to cover the essential parts of coding that every techer knows! These are:

  • Control statements
  • Functions
  • Before that :

    As a flash back, last time we discussed on a function that takes two numbers and return their sum,

    # filename: sum.py
    print( 1 + 2 );
    

    I hope everyone slightly or full remembers that because that's what we're going to expand today.


    Things to keep in mind while programming
  • Program runs step by step as you've ordered it to do. (That's a fact)
  • In programming, you avoid to write same things repeatedly(✨ We gonna see how interesting this is)
  • Make sure you have fun 😂 while doing it.
  • I hope that is clear. So let's get into it.



    Functions.

    These are lines of code that you realise they will be used may times in the program and write them once but in a reusable manner.
    Let's take an example from our work last time:

    # filename: sum.py
    print( 1 + 2 );
    

    what is we want to add many number our program ?

    # filename: sum.py
    print( 1 + 2 );
    print( 43 + 44 );
    print( 33 + 4 );
    print( 445 + 898 );
    print( 100000000 + 545454555589 );
    print( 1000 + 232 );
    
    

    It doesn't look cool, we're repeating and we've said that we sould not repeat.
    So, what is the solution? v Functions

    def sum(a,b):
        c = a + b;  # c is the sum
        return c # The we return it (the sum)
    
    ## Then we use it:
    print(sum(1,2))
    print(sum(43,44))
    


    Control statements:

    These are features that we use to control how the program will run. We'll use two:

  • if-else (looks scary 😱, Don't worry)
  • Examples:

    1. A program that checks if people are allowed to drink beer.

    def check_age(age):
        if(age < 18):
            print ("You are not allowed to drink alcohol, go drink fiesta")
        if(age >= 18):
            print ("You're allowed, enjoy !!")
    

    A better version of it.

    def check_age(age):
        if(age < 18):
            print ("You are not allowed to drink alcohol, go drink fiesta")
        else:
            print ("You're allowed, enjoy !!")