python beginners

Conditional statements in python

A conditional statement is a Boolean expression that, if True, triggers the execution of a block of code. This is an excellent method for incorporating branching logic that regulates the flow of code into your application.

Three kinds of conditional statements include:

  • If
  • If-else
  • If-elif-else

if statement in python

If statements are the simplest conditional statement, yet they serve as the basis for conditional programming in Python. If statements are used to achieve a specific goal, and if that result is not achieved, the program is terminated. If clauses can be formulated as follows:

Execute the function if the condition is True. If not, you should proceed.

Here is an example of an if clause:


num = 10

if (num == 10): print ("The number is equal to 10")

if (num > 10) print ("The number is greater than 10")
      

Output = The number is equal to 10

if-else statement

The if-then-else statement provides programmers with even greater power. If the condition is met, the code will be executed; else, another code snippet will be executed. You can now conduct two distinct actions based on the condition's value, whether it is true or false.

Here’s an example:


num = 40

if num <= 30:
  print ("The number is less than or equal to 30")

else:
  print ("The number is greater than 30")
      

Output = The number is greater than 30

A problem in programming might have various outcomes; it is not simply a True or False situation. Here is where the if-then-elif-else expression excels. It allows us to simply establish several conditions, making it the most complete conditional statement available. The elif symbol stands for "else if" and indicates that if the previous condition fails, this one will be evaluated.

Here’s an example:


light = "Red"

if light == "Green":
  print ("Go")
  
elif light == "Yellow":
  print ("Caution")
  
elif light == "Red":
  print ("Stop")

else:
  print ("Incorrect light signal")

Output = Stop

functions

Functions are reusable pieces of code that aid in the organization and maintenance of your code, and make it easier to debug and develop code. Python contains two primary types of functions:

  • Built-in functions
  • User-defined functions

Functions have the following syntax:

def function name (parameters):

Breaking down the syntax:

With the def keyword, Python functions are declared. The name of a function can be anything, but it's a best practice to reflect the purpose of the function.

Finally, there are the inputs, which are the parameters of the function. They are used to pass data to the function; the values/variables passed into the parameters are known as arguments. Note that parameter inclusion is optional.

Let's now examine a function with components in the body:


def minimum (first, second):

if (first < second):
  print (first)
      
else:
  print (second)
      
num1 = 5
num2 = 10
      
minimum (num1, num2)
      

Output = 5

The body of the function contains the collection of operations that it will execute. This is consistently right-indented. The function will not execute and an error will result if it is not right-indented. As stated previously, this function may be re-used throughout your code.

The number and names of parameters must be specified when establishing a function. These names are specific to the function and will not impact variable naming elsewhere in the code. The parameters are surrounded by parenthesis and separated by commas.