Python Complex Datatypes

Python Complex data types

  1. List
  2. Tuple
  3. Dictionary

List

  1. Group of Values belong different data type enclosed in [ ] bracket
  2. Value is called as item or Element
  3. Each Element will be retrieved based on the position.

List Operation

  1. Indexing – Retrieving only one item based on its position
  2. Slicing – Retrieving group of elements sequentially
  3. Repeation – Printing the List specified no. of times.

List Example:
# List creation
empList = [ 2001, ‘Shivani’,2005, ‘Sanjay’, 2007, ‘Babu’]
print (“employee Details : \n”, empList)

#Indexing
print(“2nd Position : “, empList[1])

#Slicing
print(“1st to 4th element : “, empList[0:4])

#printing from 2nd element to last element
print(“2nd to last element : “, empList[1:])

#Repeation
print(empList*2)

Tuple

  1. Elements are Enclosed in the round brackets ()
  2. Read only – Values can’t be modified.
  3. Supports all the List operations.

Example – Tuple
# Tuple creation
empTuple = ( 2001, ‘Shivani’,2005, ‘Sanjay’, 2007, ‘Babu’)
print (“employee Details : \n”, empTuple)

#specific element from Tuple
print(“2nd Position : “, empTuple[1])

#printing Range of element
print(“1st to 4th element : “, empTuple[0:4])

#printing from 3nd element to Tuple element
print(“3rd position to Last : “, empTuple[2:])

#printing the Tuple two times
print(empTuple*2)

Dictionary

  1. Elements are stored in Key-Value Format
  2. Key is unique and value can be duplicated
  3. Value will be Retrieved based on the key value.

Example – Dictionary
# Dictionary Creation
empDList = {}
empDList[1002]=”Sanjay”
empDList[1003]=’Sania’
empDList[1005]=’Sowmya’

print (“employee Details : \n”, empDList)
print(empDList.keys()) # prints all the keys
print(empDList.values()) # prints all the values

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

for loop in Python

for loop is a definite loop and works with all types of values like integer, float, string, list in python.
Syntax:
for in :
Statements

Example: for loop with number values
Example 1:
# For Loop – Prints the number from 1 to 10
for num in range(1,11):
print(num)
range() function is used to specify the range of values for integer and float datatype.

Example 2:
# For Loop – Prints the number from 1 to 10 in reverse order
for num in reversed (range(1,11,2)):
print(num)

Example 3:
# For loop with String value
strN=”Win Corporate”

for ch in strN:
print (“Current Letter :” + ch)
#prints one character at a time from starting to the end of the string

Example: 4
# For loop with List
Company = [‘Win’, ‘Corporate’, ‘Training’]
for c in Company:
print (“Current value :” + c)
# prints one item at a time from the list.

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

Python while loop

Python while Loop
while loop
It works as long as the given condition is true.
Syntax:
while ():
// Statements

Example 1:
# Printing the no.1 to 10
ctr=1 #initialization

print(“While Loop Demo”)
while (ctr<=10): #condition
print (ctr)
ctr = ctr +1 #incrementing part

Example 2:
#Printing the No.s from 1000 to 100

ctr=1000
print(“While Loop Demo”)
while (ctr >=100):
print (ctr)
ctr = ctr – 100
else:
print(“else part will execute when the condition becomes false”)

Note: Else is an optional and will execute when the condition given the while loop fails.

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

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

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