baabtra.com little coder chapter - 3

53
L i t t l e C o d e r P r o g r a m by

Upload: baabtracom-no-1-supplier-of-quality-freshers

Post on 15-Jul-2015

289 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Baabtra.com little coder   chapter - 3

Little Coder Program

by

Page 2: Baabtra.com little coder   chapter - 3

What you’ve learned?

What is python and why we are using python

How to setup python environment in your or your friends Computer

Python Instruction to print something

How to perform Arithmetic operations using python

What is variables, how can we use variables for better programming?

Printing variables with messages

Page 3: Baabtra.com little coder   chapter - 3

Strings, Lists, Tuples, and MapsChapter 3

Page 4: Baabtra.com little coder   chapter - 3

Strings

Page 5: Baabtra.com little coder   chapter - 3

Strings

• In programming terms, we usually call text as a string. So simply

string is a collection of letters.

• So your name is a string, your address, your email id and you may

find more examples for strings.

Page 6: Baabtra.com little coder   chapter - 3

Creating strings

• In python we can create strings in three ways

– By enclosing letters in single quotes (‘)

– By enclosing letters in double quotes(“)

– By enclosing letters in 3 single quotes(‘‘‘)

Page 7: Baabtra.com little coder   chapter - 3

Single quoted strings

>>>myName=‘baabtra.com’

>>>myAge=‘16’

>>>myEmail=‘[email protected]

>>>myPlace=‘UAE’

Page 8: Baabtra.com little coder   chapter - 3

Double quoted strings

>>>myName=“baabtra.com”

>>>myAge=“19”

>>>myEmail=“[email protected]

>>>myPlace=“UAE”

Page 9: Baabtra.com little coder   chapter - 3

Any difference between single quoted and

double quoted strings?

Page 10: Baabtra.com little coder   chapter - 3

Any difference between single quoted and

double quoted strings?

No, Literally

Page 11: Baabtra.com little coder   chapter - 3

Any difference between single quoted and

double quoted strings?

• But you should always use the same closing quote with which you

started. For example, below will produce an error

>>>myVar= “Hello ‘

No, Literally

Page 12: Baabtra.com little coder   chapter - 3

SyntaxError: EOL while scanning string literal

Any difference between single quoted and

double quoted strings?

• But you should always use the same closing quote with which you

started. For example, below will produce an error

>>>myVar= “Hello ‘

No, Literally

Page 13: Baabtra.com little coder   chapter - 3

Problems with single and double quoted strings

Page 14: Baabtra.com little coder   chapter - 3

Consider the below string

>>> silly_string = ‘He said, "Aren't can't shouldn't wouldn't.“’

Page 15: Baabtra.com little coder   chapter - 3

Consider the below string

>>> silly_string = ‘He said, "Aren't can't shouldn't wouldn't.“’

SyntaxError: invalid syntax

• In this case, the start of the string is the single quotation mark

before He, and the end of the string, as far as Python is

concerned, is the single quote after the n in Aren, which is not as

we expected. And thus produces an error

Page 16: Baabtra.com little coder   chapter - 3

How to get rid of this?

Page 17: Baabtra.com little coder   chapter - 3

How to get rid of this?There are 2 methods

By enclosing in ‘’’ quotes

Or

By using \ operator

Page 18: Baabtra.com little coder   chapter - 3

By enclosing in ‘’’ quotes

>>> silly_string = ‘’’He said, "Aren't can't shouldn't wouldn't.“’’’

By using / operator

>>> silly_string = ‘He said, "Aren\‘t can\'t shouldn\'t wouldn\'t.“’

Page 19: Baabtra.com little coder   chapter - 3

ListsLists Are More Powerful than Strings

Page 20: Baabtra.com little coder   chapter - 3

Lists

• Lists are used when we want to store a list of items in a variable

• Eg: consider if we want to store different fruit names in a variable,

we can make use of lists as below

– Fruit_shop=[‘oranges’, ’apples’, ’bananas’]

Page 21: Baabtra.com little coder   chapter - 3

Positions of List elements

Consider the below example

Fruit_shop=[‘oranges’, ’apples’, ’bananas’]

Page 22: Baabtra.com little coder   chapter - 3

Positions of List elements

Consider the below example

Fruit_shop=[‘oranges’, ’apples’, ’bananas’]

Position 0

Page 23: Baabtra.com little coder   chapter - 3

Positions of List elements

Consider the below example

Fruit_shop=[‘oranges’, ’apples’, ’bananas’]

Position 1

Page 24: Baabtra.com little coder   chapter - 3

Positions of List elements

Consider the below example

Fruit_shop=[‘oranges’, ’apples’, ’bananas’]

Position 2

Page 25: Baabtra.com little coder   chapter - 3

Printing lists

>>> fruit_shop=[‘oranges’, ’apples’, ’bananas’]

>>>print(fruit_shop)

Output

[‘oranges’,’apples’,’bananas’]

Page 26: Baabtra.com little coder   chapter - 3

Printing lists

>>> fruit_shop=[‘oranges’, ’apples’, ’bananas’]

>>>print(fruit_shop[1])

Output

[’apples’]

Page 27: Baabtra.com little coder   chapter - 3

Printing lists

>>> fruit_shop=[‘oranges’, ’apples’, ’bananas’]

>>>print(fruit_shop[2])

Output

[’bananas’]

Page 28: Baabtra.com little coder   chapter - 3

Printing lists

>>> fruit_shop=[‘oranges’, ’apples’, ’bananas’]

>>>print(fruit_shop[0:1])

Output

[‘oranges’,’apples’]

Page 29: Baabtra.com little coder   chapter - 3

Adding items into list

• To add items to a list, we use the append() function. A function is

a chunk of code that tells Python to do something.

>>> fruit_shop=[‘oranges’, ’apples’, ’bananas’]

>>> fruit_shop.append(‘grapes’)

>>> fruit_shop.append(‘pineapples’)

>>>print(fruit_shop)

Page 30: Baabtra.com little coder   chapter - 3

Adding items into list

• To add items to a list, we use the append() function. A function is

a chunk of code that tells Python to do something.

>>> fruit_shop=[‘oranges’, ’apples’, ’bananas’]

>>> fruit_shop.append(‘grapes’)

>>> fruit_shop.append(‘pineapples’)

>>>print(fruit_shop)

>>[‘oranges’, ’apples’, ’bananas’, ‘grapes’, ‘pineapples’]

Page 31: Baabtra.com little coder   chapter - 3

Removing Items from a List

• We used del key word to remove an item from a list

>>> fruit_shop=[‘oranges’, ’apples’, ’bananas’]

>>> del fruit_shop[2]

>>> print(fruit_shop)

>>> [‘oranges’, ’apples’]

Page 32: Baabtra.com little coder   chapter - 3

Try this !

• Create a list variable to store the subjects you study in your school

• Create a list variable to store your hobbies

• Create a list variable to store your favorite movie names

Page 33: Baabtra.com little coder   chapter - 3

Try this !

• Create a list variable to store the subjects you study in your school

>>>mySubjects=[‘science’,’maths’,’English’]

• Create a list variable to store your hobbies

>>>myHobbies=[‘reading’,’Listening to music’,’playing’]

• Create a list variable to store your favorite movie names

>>>favMovies=[‘harrypotter’,’spiderman’,’hulk’]

Page 34: Baabtra.com little coder   chapter - 3

Try this !

• Create a list variable to store the subjects you study in your school

>>>mySubjects=[‘science’,’maths’,’English’]

• Create a list variable to store your hobbies

>>>myHobbies=[‘reading’,’Listening to music’,’playing’]

• Create a list variable to store your favorite movie names

>>>favMovies=[‘harrypotter’,’spiderman’,’hulk’]

Page 35: Baabtra.com little coder   chapter - 3

Tuples

Page 36: Baabtra.com little coder   chapter - 3

Tuples

• A tuple is like a list that uses parentheses, as in this example:

>>> fibs = (0, 1, 1, 2, 3)

>>> print(fibs[3])

2

“The main difference between a tuple and a list is that a tuple

cannot change once you’ve created it.”

Page 37: Baabtra.com little coder   chapter - 3

Try this !

• Create a tuple to store all the even numbers below 10

• Create a tuple to store all the odd numbers below 10

• Create a tuple to store all the prime numbers below 25

Page 38: Baabtra.com little coder   chapter - 3

Try this !

• Create a tuple to store all the even numbers below 10

>>>evenNumbers=(0,2,4,6,8)

• Create a tuple to store all the odd numbers below 10

>>>oddNumbers=(1,3,5,7,9)

• Create a tuple to store all the prime numbers below 20

>>>primeNumbers=(1,2,3,5,7,11,13,17,19)

Page 39: Baabtra.com little coder   chapter - 3

Maps (Dictionaries)

Page 40: Baabtra.com little coder   chapter - 3

Maps

• In Python, a map (also referred to as a dictionary) is a collection

of things, like lists and tuples. The difference between maps and

lists or tuples is that each item in a map has a key and a

corresponding value.

Favourite_stars= [

‘football’ : ’David Beckam’,

‘cricket’ : ’Sachin Tendulkar’,

‘Athlete’ : ’Usain Bolt’

]

Page 41: Baabtra.com little coder   chapter - 3

Maps

• In Python, a map (also referred to as a dictionary) is a collection

of things, like lists and tuples. The difference between maps and

lists or tuples is that each item in a map has a key and a

corresponding value.

Favourite_stars= [

‘football’ : ’David Beckam’,

‘cricket’ : ’Sachin Tendulkar’,

‘Athlete’ : ’Usain Bolt’

]

This is what we call as key

Page 42: Baabtra.com little coder   chapter - 3

Maps

• In Python, a map (also referred to as a dictionary) is a collection

of things, like lists and tuples. The difference between maps and

lists or tuples is that each item in a map has a key and a

corresponding value.

Favourite_stars= [

‘football’ : ’David Beckam’,

‘cricket’ : ’Sachin Tendulkar’,

‘Athlete’ : ’Usain Bolt’

]

This is what we call as

Value

Page 43: Baabtra.com little coder   chapter - 3

Printing, Adding, Removing from Maps

>>>print(Favourite_stars[‘cricket’])

‘Sachin Tendulkar’

>>>favourite_stars[‘Tennis’ : ‘Federar’]

>>>del favourite_stars[‘athletic’]

Page 44: Baabtra.com little coder   chapter - 3

Printing, Adding, Removing from Maps

>>>print(Favourite_stars[‘cricket’])

‘Sachin Tendulkar’

>>>favourite_stars[‘Tennis’ : ‘Federar’]

>>>del favourite_stars[‘athletic’]

We can print using the key

Page 45: Baabtra.com little coder   chapter - 3

Printing, Adding, Removing from Maps

>>>print(Favourite_stars[‘cricket’])

‘Sachin Tendulkar’

>>>favourite_stars[‘Tennis’ : ‘Federar’]

>>>del favourite_stars[‘athletic’]

Adding element is easy, You can

just give the key and value

Page 46: Baabtra.com little coder   chapter - 3

Printing, Adding, Removing from Maps

>>>print(Favourite_stars[‘cricket’])

‘Sachin Tendulkar’

>>>favourite_stars[‘Tennis’ : ‘Federar’]

>>>del favourite_stars[‘athletic’]Items can be removed using del

keyword

Page 47: Baabtra.com little coder   chapter - 3

Try this !

• Create a map/dictionary to store 10 English words an their

meanings

Page 48: Baabtra.com little coder   chapter - 3

Try this !

• Create a map/dictionary to store 10 English words an their

meanings

Eng_words= [

‘debate’ : ’ to engage in argument or discussion’,

‘engage’ : ‘become involved’,

‘weapons’ : ‘any instrument for use in attack ’

]

Page 49: Baabtra.com little coder   chapter - 3

Exercise !

Page 50: Baabtra.com little coder   chapter - 3

Exercise !

• Strings

1. Create a string ‘person’ to store the string ‘HarryLime’

2. Make a string to store the below paragraph and fill the blank with

variable ‘person’

“ but I get the impression that really he'd rather not be bothered. Has

never really grown up and perhaps that accounts for the way he

worshipped Lime.' I wrote there that phrase 'in normal circumstances'

because I met him first at _______'s funeral”

Page 51: Baabtra.com little coder   chapter - 3

Exercise !

• Lists

1. Make a list of your 3 of you favorite hobbies and give the list the

variable name games. Print the variable after creating

2. Now make a list of 5 of your favorite foods and name the variable

foods. Print the variable after creating

3. Add 2 more hobbies to the list ‘games’

4. Remove last two foods from variable ‘foods’

5. Finally, print both variables

Page 52: Baabtra.com little coder   chapter - 3

Exercise !

• Tuples

1. Make a tuple to store five of your super hero names in it. Name the

variable ‘superHeroes’

2. Try to remove last super hero from the tuple

3. If you get any error for 2nd question note down it, and understand

why it happened

• Dictionaries

1. Create a dictionary to store the details of your family. ‘Key’ must be

the relation and value must be their name

2. Print the dictionary

Page 53: Baabtra.com little coder   chapter - 3

End of Chapter 3