CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

08 April, 2022

Python If-else statements

 Programing Coderfunda     April 08, 2022     Python     No comments   

 

Python If-else statements

Decision making is the most important aspect of almost all the programming languages. As the name implies, decision making allows us to run a particular block of code for a particular decision. Here, the decisions are made on the validity of the particular conditions. Condition checking is the backbone of decision making.

In python, decision making is performed by the following statements.

StatementDescription
If StatementThe if statement is used to test a specific condition. If the condition is true, a block of code (if-block) will be executed.
If - else StatementThe if-else statement is similar to if statement except the fact that, it also provides the block of the code for the false case of the condition to be checked. If the condition provided in the if statement is false, then the else statement will be executed.
Nested if StatementNested if statements enable us to use if ? else statement inside an outer if statement.

Indentation in Python

For the ease of programming and to achieve simplicity, python doesn't allow the use of parentheses for the block level code. In Python, indentation is used to declare a block. If two statements are at the same indentation level, then they are the part of the same block.

Generally, four spaces are given to indent the statements which are a typical amount of indentation in python.

Indentation is the most used part of the python language since it declares the block of code. All the statements of one block are intended at the same level indentation. We will see how the actual indentation takes place in decision making and other stuff in python.

The if statement

The if statement is used to test a particular condition and if the condition is true, it executes a block of code known as if-block. The condition of if statement can be any valid logical expression which can be either evaluated to true or false.

Python If-else statements

The syntax of the if-statement is given below.

  1. if expression:  
  2.     statement  

Example 1

  1. num = int(input("enter the number?"))  
  2. if num%2 == 0:  
  3.     print("Number is even")  

Output:

enter the number?10
Number is even

Example 2 : Program to print the largest of the three numbers.

  1. a = int(input("Enter a? "));  
  2. b = int(input("Enter b? "));  
  3. c = int(input("Enter c? "));  
  4. if a>b and a>c:  
  5.     print("a is largest");  
  6. if b>a and b>c:  
  7.     print("b is largest");  
  8. if c>a and c>b:  
  9.     print("c is largest");  

Output:

Enter a? 100
Enter b? 120
Enter c? 130
c is largest

The if-else statement

The if-else statement provides an else block combined with the if statement which is executed in the false case of the condition.

If the condition is true, then the if-block is executed. Otherwise, the else-block is executed.

Python If-else statements

The syntax of the if-else statement is given below.

  1. if condition:  
  2.     #block of statements   
  3. else:   
  4.     #another block of statements (else-block)   

Example 1 : Program to check whether a person is eligible to vote or not.

  1. age = int (input("Enter your age? "))  
  2. if age>=18:  
  3.     print("You are eligible to vote !!");  
  4. else:  
  5.     print("Sorry! you have to wait !!");  

Output:

Enter your age? 90
You are eligible to vote !!

Example 2: Program to check whether a number is even or not.

  1. num = int(input("enter the number?"))  
  2. if num%2 == 0:  
  3.     print("Number is even...")  
  4. else:  
  5.     print("Number is odd...")  

Output:

enter the number?10
Number is even

The elif statement

The elif statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them. We can have any number of elif statements in our program depending upon our need. However, using elif is optional.

The elif statement works like an if-else-if ladder statement in C. It must be succeeded by an if statement.

The syntax of the elif statement is given below.

  1. if expression 1:   
  2.     # block of statements   
  3.   
  4. elif expression 2:   
  5.     # block of statements   
  6.   
  7. elif expression 3:   
  8.     # block of statements   
  9.   
  10. else:   
  11.     # block of statements  
Python If-else statements

Example 1

  1. number = int(input("Enter the number?"))  
  2. if number==10:  
  3.     print("number is equals to 10")  
  4. elif number==50:  
  5.     print("number is equal to 50");  
  6. elif number==100:  
  7.     print("number is equal to 100");  
  8. else:  
  9.     print("number is not equal to 10, 50 or 100");  

Output:

Enter the number?15
number is not equal to 10, 50 or 100

Example 2

  1. marks = int(input("Enter the marks? "))  
  2. f marks > 85 and marks <= 100:  
  3.    print("Congrats ! you scored grade A ...")  
  4. lif marks > 60 and marks <= 85:  
  5.    print("You scored grade B + ...")  
  6. lif marks > 40 and marks <= 60:  
  7.    print("You scored grade B ...")  
  8. lif (marks > 30 and marks <= 40):  
  9.    print("You scored grade C ...")  
  10. lse:  
  11.    print("Sorry you are fail ?")  
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • What is WordPress
      What is WordPress WordPress is a free and open-source  C ontent  M anagement  S ystem (CMS). It is an online site based on PHP and MySQL. ...
  • JqueryUI Tutorial
    JqueryUI Tutorial    JqueryUI is the most popular front end frameworks currently. It is sleek, intuitive, and powerful mobile first fr...
  • Laravel - Application Structure
    The application structure in Laravel is basically the structure of folders, sub-folders and files included in a project. Once we create a ...
  • Laravel - Installation
    For managing dependencies, Laravel uses   composer . Make sure you have a Composer installed on your system before you install Laravel. In...
  • Python Tutorial
      Python   is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (69)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • July (4)
  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

Loading...

Laravel News

Loading...

Copyright © CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda