DEV Community

Evans Mutwiri
Evans Mutwiri

Posted on

Python 101: The Basics

Prerequisites
To get the best out of this article you should be familiar with the following concepts:

  1. Python as a programming language , programming techniques supported python programming language and it’s programming language category.

  2. Variables, defining and using variables in python programming language.

  3. Comments, keywords, identifiers, rules to follow when defining identifiers literals.

  4. Built-in functions in python programming language like print( … )

  5. Operators in python programming language.

  6. PE8 rules and standards in Python programming language.

Control flow and decision making in Python

Conditional statements are also known as decision-making statements. We need to use these conditional statements to execute the specific block of code if the given condition is true or false.

In Python we can achieve this using:

#1) -if else statements

It checks whether a condition is true or not. If the condition is true the code inside the "if" block is executed otherwise it's not. Else block provide an alternative code to be executed if the first is not.

If ( CONDITION == TRUE ):
     Block to execute
else:
     Block to execute otherwise
Enter fullscreen mode Exit fullscreen mode

Let's see and discuss some examples of if statements:

print('Example: 1')

num = 5

if (num < 10):
    print('Num is smaller than 10')
else:
    print('Num is not smaller than 10')


print('Example: 2')

a = 7
b = 0

if(a):
    print('true')
else:
    print('false')

print('Example: 3')

if('python' in ['Java', 'Python', 'Dart']):
    print('Python found in the List')
else:
    print('Python not found')
Enter fullscreen mode Exit fullscreen mode

In Example 1 we check the statement whether our variable is less than 10. If the statement is true the print method following it is executed.
Remember to use the (:) operator at the end of the if statement, because whatever the code you write after the colon operator will be a part of “if block” and indentation is very important.

In Example 2, we are not looking for any condition. In any programming language, a positive number is considered a true value so our program produced output 'true'.

In Example 3 we check the list of languages to check whether Python is in it.

//Example 4
passing_Mark = 60
my_Score = 67
if(my_Score >= passing_Score):
    print('Congratulations! You passed the exam')
    print("You are passed in the exam")        
else:
    print('Sorry! You failed the exam, better luck next time')
Enter fullscreen mode Exit fullscreen mode

Example 4

#2) -elif statements

"elif" statement is used to check multiple conditions only if the given condition is false. The difference between elif and else statements is that elif checks for a condition while else does not check for a condition.

if (condition):
    #Set of statement to execute if condition is true
elif (condition):
    #Set of statements to be executed when if condition is false and elif condition is true
else:
    #Set of statement to be executed when both if and elif conditions are false
Enter fullscreen mode Exit fullscreen mode
//Example of elif statement

num = int(input("Enter number: "))
if (num > 0):
    print('Number is positive')
elif (num < 0):
    print('Number is negative')
else:
    print('Number is Zero')
Enter fullscreen mode Exit fullscreen mode

In this example we try something new by taking user input. By default the input is a string so we have to type cast it to integer to perform the operation.
The program will check if num is greater than 0(condition),
if not we check the condition in elif(num is less than 0),
if both are not true only then is the else statement reached.

Note: Python if statement evaluates a boolean expression to true or false, if the condition is true then the statement inside the if block will be executed in case the condition is false then the statement present inside the else block will be executed only if you have written the else block otherwise it will do nothing.


Python Loops – For, While

In Python, statements are executed in a sequential manner ie. one line after another. However, you may want to repeat some lines until a certain condition is met. That's where loops come through for you.

Top comments (0)