Nov 2023
2 hrs Python
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:
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.
I hope that is clear. So let's get into it.
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))
These are features that we use to control how the program will run. We'll use two:
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 !!")