Friday 29 June 2018

if-else statement


Python nested if statement

Python nested-if statement

Nested if statements in any programming language means an if statement inside another if statement. Python also allows us to nest if statements within if statements. i.e, we can write an if statement inside another if statement.

Syntax of nested-if statement:

if (condition 1):
   # Code to be executed when condition 1 is true
   if (condition 2):
      # Code to be executed when condition 2 is true
   # end of inner if Block
# end of outer if Block

Example of Python nested if statement

# Python program to demonstrate nested-if statement
# Program will check whether entered number is positive or negative or zero.

number = int(input("Enter a number: "))
if number >= 0:
    if number == 0:
        print("It's Zero")
    else:
        print("It's Positive number")
else:
    print("It's Negative number")

Output of program

Enter a number: 5
It's Positive number

if-elif-else ladder

The if statements are executed from the top to the bottom sequentially. When one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the last else statement written will be executed.

Syntax of if-elif-else

if (condition 1):
    statements for condition 1
elif (condition 2):
    statements for condition 2
.
.
else:
    statements for else

Example of Python if-elif-else ladder

number = int(input("Enter number in range of 1 to 5:"))
if (number == 1):
print ("It’s One")
elif (number == 2):
print ("It’s Two")
elif (number == 3):
print ("It’s Three")
elif (number == 4):
print ("It’s Four")
elif (number == 5):
print ("It’s Five")
else:
print ("It’s not in range of 1 to 5")

Output of program

Enter number in range of 1 to 5:3
It’s Three

* * * * *

<  Back to Home Page >



if statement

Python provides following decision making statements (also known as conditional statements):

  • if statement
  • if..else statement
  • nested - if statement

if statement


if statement is one of most commonly used conditional statement of Python. "if statement" is written by using the if keyword. if statement consists of a Boolean expression which is followed by one or more logical statements.

Example 1: Use of if statement to check greater number.

number1 = 101
number2 = 201

if number2 > number1:
    print ("number2 is greater than number1.")

if number1 > number2:
    print ("number1 is greater than number2.")


Output of Program

number2 is greater than number1.

Example 2: Python program to display Good Morning message based in given time.

time = int(input("Enter your time:"))

if time < 12:
    print ("Good Morning...")

print("Have a nice time...")

Output of Program

Enter your time:5
Good Morning...
Have a nice time...

Example 3: Python program to check whether given number  is positive or not.

number = int(input("Enter your number:"))

if number > 0:
    print ("It's positive number")
print("It's end of program")

Output of Program

Enter your time:5
It's end of program

#Note: If entered number is negative or zero then this program will print only "It's end of program" message.

continue statement


break statement

break statement

Python Operators

Python Operators

Like all computer programming languages, python also supports following basic operators. Using these operators, we can perform mathematical operations on variables.

Basic operators in Python includes

+ for addition operation
-  for subtraction operation
*  for multiplication operation
/  for division operation
%  for modulus operation
**  for exponent operation
//  for floor division operation

Examples of Python Operators

Let’s use these operators on following two variables:

num1 = 10
num2 = 5

Addition
num1 + num2 will result in 15

Subtraction
num1 - num2 will result in 5

Multiplication
num1 * num2 will result in 50

Division
num1/num2 will result in 2

Floor Division
num1//num2 will result in 2 (rounds down the answer to the nearest whole number)

Modulus
num1%num2 will result in 0 (It’s remainder of 10 divided by 5)

Exponent
num1**num2 will result in 100000 (10 to the power of 5)

Assignment Operator (=)


Like most of the programming languages, Python uses equal sign (=) as assignment operator.

number = 5

Above statement will assign value 5 to variable number.

There are a few more ways to use assignment operators in Python; examples are +=, -= and *=.

If we want to increment value of number by 2, we can write

number = number + 2

Python will first evaluate the expression on the right side i.e. number + 2 and assign the answer to the left side. Hence execution of above statement will result in number = 7 (assuming initial value of number is 5).

The same statement can also be written as under:

number += 2

The number += 2 is actually a short form of the statement number = number + 2

Similarly, if we want to do a subtraction, we can write number = number – 2 or number -= 2. The same works for most of the python operators described above.



* * * * *

Python Installation and Setup

Python Installation and Setup

Python interpreter is needed to run Python programs. Perform following steps to install python software in your system.


Step-1 Search "Python Download in Google"


Step-2 Download latest Python version from URL https://www.python.org/downloads/


Step-3 Install Downloaded - Python 3.7.4 applications (.exe file)


Step-4 Install Python 3.7.4 - Customize your installation


Step-5 Select basic configuration to install (select your installation drive and folder)



Step-6 Wait for message "Python Setup was successful"


Step-7 Go to Windows start menu and select: IDLE Python 3.7


Step-8 Start writing your Python code in IDLE (Python Interpreter)

First Python Practical using Python Shell

* * * * *



Acknowledgement: All product names (Google, Windows, Python and others), logos, and brands are property of their respective owners. The content shown on page is for demonstration only.

Decision Making

Decision Making

Basic Syntax

Welcome to Python Practicals

Let's explore Top Python Practicals

Click your Topic of Interest to Learn

Introduction to Python

Python is an easy to learn and very powerful programming language. It has efficient data structures and a simple approach to object-oriented programming. Python’s syntax together with its interpreted nature, make it an ideal language for scripting and rapid application development. 

Basics of Python

Python is an easy to learn and very powerful programming language.

Data Types

Python is very good in String Processing. 

Numbers

Numbers are one of the fundamental building block of any programming language.


Copyright © 2021 PythonPracticals.Blogspot.Com

All rights reserved.






Python Overview

Python Overview

If you are new to the Computer Programming, Python is a great place to start. One of the key features of Python is its simplicity which makes it ideal language for beginners to learn.

Python is one of the widely used high-level programming language invented by Guido van Rossum. It can be used for solving varieties of problems. Python is known for its simplicity and code readability. Programmer can develop application very rapidly using Python.


Python code resembles the English language. To interpret the Python codes, computer needs a special program known as the Python interpreter. Python interpreter is needed to code, test and execute the  Python programs.

Most programs in Python needs very few lines of code to solve complicated programming task compared to other languages. Fewer lives of code leads to fewer programming errors and reduces the overall development time. 



Python Data types

Data types