Python Classroom Series – 29/May/2021

Iterating Multiple Sequences

  • Consider you have following lists
days = [ 'Monday', 'Tuesday', 'Wednesday']
fruits = [ 'apple', 'banana', 'orange']
drinks = [ 'tea', 'coffee', 'fresh lime juice']
  • Now you are supposed to print
day: Monday, drink tea, eat apple
day: Tuesday, drink coffee, eat banana
  • In Python we can iterate over multiple sequences in parallel by using zip function Preview
  • zip() function stops when the shortest sequences Refer Here

Exercise

  • Write a python program to store all the numbers from 1 to 100 in a list
  • Write a python program to store even numbers from 10 to 100 in a list Preview

List Comprehensions

  • Pythonic way to build a list is by list comprehensions. List comprehension in its simple format will look as
[ expression for item in iterable]
  • A list comprehension can include a conditional expression
[ expression for item in iterable if condition]

Preview

  • Create a python list with squares of numbers from 10 to 30
square_numbers = [ number**2 for number in range(10,31)]

Matrix Representation of List

  • Create a matrix representation of 2×2 matrix
1 2
1 2

Preview

  • Create a matrix representation
1 1
1 2
2 1
2 2
3 1
3 2

Preview

Dictionaries

  • A dictionary is similar to a list, but the order of item’s doesn’t matter.
  • In list items are selected by an offset (index) like 0 or 1, in dictionaries instead you specify a unique key to associate with each value.
  • Dictionaries are mutable and the contain collection of key-value elements
  • Create with {}
empty_dict = {}
  • The dictionary will have the following syntax
{
    key1: value1,
    key2: value2
    ...
    keyn: valuen
}
  • Lets create a sample dictionary
inst_name = { 'name': 'QualityThought', 'faculty': 'khaja', 'OnlineFacilty': True } 

Preview

  • Create with dict():
institute = dict(name='QualityThought', faculty='khaja')
print(institute)
  • Refer the python notebook for dictionary operations Refer Here

Iterating multiple Sequences

  • Consider you have following lists
days = [ 'Monday', 'Tuesday', 'Wednesday']
fruits = [ 'apple', 'banana', 'orange']
drinks = [ 'tea', 'coffee', 'fresh lime juice']
  • Now you are supposed to print
day: Monday, drink tea, eat apple
day: Tuesday, drink coffee, eat banana
  • In Python we can iterate over multiple sequences in parallel by using zip function Preview
  • zip() function stops when the shortest sequences Refer Here

Exercise

  • Write a python program to store all the numbers from 1 to 100 in a list
  • Write a python program to store even numbers from 10 to 100 in a list Preview

List Comprehensions

  • Pythonic way to build a list is by list comprehensions. List comprehension in its simple format will look as
[ expression for item in iterable]
  • A list comprehension can include a conditional expression
[ expression for item in iterable if condition]

Preview

  • Create a python list with squares of numbers from 10 to 30
square_numbers = [ number**2 for number in range(10,31)]

Matrix Representation of List

  • Create a matrix representation of 2×2 matrix
1 2
1 2

Preview

  • Create a matrix representation
1 1
1 2
2 1
2 2
3 1
3 2

Preview

Dictionaries

  • A dictionary is similar to a list, but the order of item’s doesn’t matter.
  • In list items are selected by an offset (index) like 0 or 1, in dictionaries instead you specify a unique key to associate with each value.
  • Dictionaries are mutable and the contain collection of key-value elements
  • Create with {}
empty_dict = {}
  • The dictionary will have the following syntax
{
    key1: value1,
    key2: value2
    ...
    keyn: valuen
}
  • Lets create a sample dictionary
inst_name = { 'name': 'QualityThought', 'faculty': 'khaja', 'OnlineFacilty': True } 

Preview

  • Create with dict():
institute = dict(name='QualityThought', faculty='khaja')
print(institute)
  • Refer the python notebook for dictionary operations Refer Here

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin