Python if-elif-else Statement

if-elif-else statement
Multiple Conditions in the Program.
Ex:

# Example – if-elif-else statement

age = int(input(“Enter Your age: “))

if age =13 and age =19 and age =60 and age <=100:
print("Senior Citizen")

else:
print("Guess")

Win Corporate Training
website: http://www.wincorptrg.com
View My Profile

Advertisements

Python if statement

Python – Conditional Statement

Def: Conditional Statement is used to give condition in the program.  Based on the  condition’s output only a part of the program will be executed.
Ex: if statement
Types:

  1. if
  2. if-else
  3. if-elif-else

if statement
condition is set using Comparison operator and when output is true it executes the statements given.
Ex:
# Example – if statement
age = int(input(“Please enter an integer: “))
if age >= 18:
print(“Can vote in Election”)

if-else statement
else part will executed when the condition is false.
Ex:
# Example – if else statement
age = int(input(“Please enter an integer: “))
if age >= 18:
print(“Can vote in Election”)
else:
print(“Can’t vote in Election”)

Win Corporate Training
website: http://www.wincorptrg.com
View My Profile

Python String

String is a group of one or more characters enclosed in Single or Double Quote.
Example: –

  • “Win”
  • ‘Corporate’
  • “A”
  • “W001”

String Operation

Concatenation – Joining the Two Text or String values using “+” Symbol.
Ex: fname=”Sanjay” + ” K”

String Length – Finding the No. of characters in the String
Ex:len(“Win”) = 3

Indexing – Returns the character from the specific position from the String
Ex:
sname=”Sanjay”
sname[0]=”S”

Slicing – Returns group of Characters from the specified position.
Ex:
sname=”Sanjay”
sname[0:3]=”San”

Repeation – prints the string value specified no. of times.
Ex: n=”Sanjay” * 20 [Sanjay will be printed 20 times]

#String Operation in Python
sname=”Sanjay is good boy”

fname=”Sanjay” + ” K”
print(“Concatenation : ” + fname)

print(“Indexing :” + sname[0])

print(“Slicing”, sname[3:6])

print(‘Number of characters’)
print(len(sname))

n=”*” * 20
print(“Repeation : ” + n)

Win Corporate Training
website: http://www.wincorptrg.com
View My Profile

Calculation in Python

We can perform all types of calculation and Comparison in Python.

Expression are used to perform calculations in Python.

Expression – Combination of  Numbers and Operator. Operator Decides the type of calculation performed by the Expression.
Example: 3+4=7

Types of Operator:

  1. Arithmetic Operator
  2. Comparison Operator
  3. Logical Operator

Arithmetic Operator – Performs the simple Mathematical Calculation.

# Accept nos from user and find its sum
x=int(input(‘Enter a number : ‘))
y=int(input(‘Enter a number1 : ‘))

#adding nos
sum=x+y

#print output
print(‘Sum is =’, sum)

Like Addition, Subtraction, Multiplication and Division can be performed.

Win Corporate Training
website: http://www.wincorptrg.com
View My Profile

Variable and Datatype in Python

What is Variable?

  • Named memory location used for storing the value in the entered by the user.
  • Stores only one value at a time

What is Datatype?
Datatype determines type of value to be stored.

Description Java Python
Variable Declaration Mandatory Not Mandatory
Datatype int,float,double,char,boolean,long,String str,int,float
Example String sname=”Sanjay” sname=”Sanjay”

Example: Python Variable Declaration
# Variable in python
empno=2002
empName=”Sanjay”
salary=2500.00

print(“Employee details:”)
print(“Employee No. : “, empno)
print(“Employee Name=”, empName)
print(“Salary=”, salary)

Note:
Python supports Dynamic Typing means based on the value stored in the Variable datatype will be automatically assigned to the variable.
type() function is used to Find the variable’s datatype at Run time.
Example: print(“empno – datatype :”, type(empno))

Next Part : Python Calculation

Win Corporate Training
website: http://www.wincorptrg.com
View My Profile

Python – First Program

Python First Program

#First program – Comment
print(“Hello World”)

Comment – #

  1. Provides the Description about the program
  2. Code will not be executed

print  Function

  1. Prints the output at the Console.
  2. printing statements can be given within the Single or Double Quote.

Steps to Create the Program:

  1. Open the Python IDLE [ Integrated Development Learning Environment]
  2.  Type the program at the Command Prompt.
winp1

Python IDLE

 

 

 

 

How to create a file File

  1. File -> New in Python IDLE
  2. Type the program in the file and Save the file with extnesion .py.
  3. Press F5 – Function key to run the program.

winp2

 

 

 

4. View output at the command prompt.

Next Part : Python Variable and Datatype

Win Corporate Training
website: http://www.wincorptrg.com
View My Profile