Wednesday 30 October 2019

python if else

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

  • if statement
  • if..else statement
  • nested - if statement
Let's learn if..else statement of Python programming.

if..else statement


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

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

number1 = 201
number2 = 101

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


Output of Program

number1 is greater than number2.

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...")
else:
    print ("Have a nice time...")

Output of Program

Enter your time:5
Good Morning...


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")
else:
    print ("It's not positive number")

Output of Program

Enter your time:5
It's positive number

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

Sunday 27 October 2019

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

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.

Python program to display Good Morning message based in given time

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...

Note: If user enters time greater than 12 then program will display only "Have a nice time..." message.

Python program to check odd even number

Python program to check odd even number


Following Python program will read a number from user and will display whether it is odd or even.

To check whether given number is ODD or EVEN, program uses modulus operator (%) and divides given number by 2. If the answer of modulus operator is 0; it means given number is  completely divisible by 2, hence we can display that "It's even number". If answer of modulus operator is not zero, it means number is Odd.


#Python program to check odd-even number.

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

if (number%2 == 0):
    print("It's even number")
else:
    print("It's odd number")


Output of program

Enter your number:101
It's odd number


* * * * *

Sunday 20 October 2019

Python Tuple

Python Tuple


Tuple is an ordered sequence of items. Tuple in python are very similar to  List. The only difference between Tuple and List is that tuples are immutable. Values cannot be modified in Tuples once created.

Tuples are used to create a data which we don't want to change after it's creation. Values inside tuples are like protected data and are usually faster than list as it cannot change dynamically.

Tuples are defined using parentheses (). Items inside tuples are separated by commas. Example of tuple is shown below:


>>> myTuple = (101,'PythonPracticals', 'KPatel')
>>> print(myTuple)
(101, 'PythonPracticals', 'KPatel')


We can refer individual elements of Tuple as shown below:

>>> print(myTuple[0])
101

We can use the slicing operator [ ] to extract items from Tuple but we cannot change its value. For example:


# Using Slicing Operator with Tuple

>>> print(myTuple[0:2])
(101,'PythonPracticals')

# Editing value of Tuple

>>> myTuple[0]=105
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    myTuple[0]=105
TypeError: 'tuple' object does not support item assignment




* * * * *

< Back to Home Page >