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

Advertisements

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