Python Basics - Part 2

Python Basics - Part 2

Continue your epic journey in python ๐ŸŽ‰

ยท

2 min read

Welcome back pro's! If you have not checked out part 1 of this python series please do so: https://cstorm.hashnode.dev/python-basics-part-1
Consider liking the post if you found it helpful, it helps a lot ๐Ÿ˜ƒ

Conditional Statements

If else

This statement checks the condition present in the if and in case it is true, the operations under if will be executed but in case the condition is false then the operations under else will be executed.

Syntax: if condition:
            statements
        else:
            statements

In case you have more than 2 cases to check, you can use elif to check another condition.

Syntax: if condition 1:
            statements
        elif condition 2:
            statements
        else:
            statements

Loops

While Loop

This loop is used to perform repetitive tasks until a condition is met. For example printing numbers from 1 to 100 etc.

Syntax: i = 1
        while i<101:    #condition 
            print(i)     #statements
            i = i+1      #Similar as  i+=1

For Loop

This loop can be used to iterate over a list, string or a certain range. There is no fixed syntax it depends on the purpose of the loop.

Examples: #For printing numbers in a specific range
         for i in range(0,5):    #range is from 0-5
              print(i)           #prints 0,1,2,3,4
         word = "Hello"
         #For printing the characters present in a string variable
         for words in word:
              print(words)
'''Output: H 
           e
           l
           l
           o'''

Nested Loops

Nested loops refer to a loop being inside another loop, that is a loop being "nested" inside another loop.

Example:

while condition:
   while condition: 
       statements

Loop Control Statements

  1. Break: This statement is used to exit from the loop

  2. Continue: It is used to skip the current iteration of the loop.

  3. Pass: Used as a null statement and also acts as a placeholder for future codes.

That's it for this post! Please like the post and subscribe to the newsletter to not miss any new contents I post :)

-Coding Storm

The storm is coming โšก

https://www.instagram.com/cstorm.official/

ย