Date post: | 15-Jul-2015 |
Category: |
Software |
Upload: | baabtracom-no-1-supplier-of-quality-freshers |
View: | 289 times |
Download: | 2 times |
Little Coder Program
by
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
Strings, Lists, Tuples, and MapsChapter 3
Strings
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.
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(‘‘‘)
Single quoted strings
>>>myName=‘baabtra.com’
>>>myAge=‘16’
>>>myEmail=‘[email protected]’
>>>myPlace=‘UAE’
Double quoted strings
>>>myName=“baabtra.com”
>>>myAge=“19”
>>>myEmail=“[email protected]”
>>>myPlace=“UAE”
Any difference between single quoted and
double quoted strings?
•
Any difference between single quoted and
double quoted strings?
•
No, Literally
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
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
Problems with single and double quoted strings
Consider the below string
>>> silly_string = ‘He said, "Aren't can't shouldn't wouldn't.“’
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
How to get rid of this?
How to get rid of this?There are 2 methods
By enclosing in ‘’’ quotes
Or
By using \ operator
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.“’
ListsLists Are More Powerful than Strings
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’]
Positions of List elements
Consider the below example
Fruit_shop=[‘oranges’, ’apples’, ’bananas’]
Positions of List elements
Consider the below example
Fruit_shop=[‘oranges’, ’apples’, ’bananas’]
Position 0
Positions of List elements
Consider the below example
Fruit_shop=[‘oranges’, ’apples’, ’bananas’]
Position 1
Positions of List elements
Consider the below example
Fruit_shop=[‘oranges’, ’apples’, ’bananas’]
Position 2
Printing lists
>>> fruit_shop=[‘oranges’, ’apples’, ’bananas’]
>>>print(fruit_shop)
Output
[‘oranges’,’apples’,’bananas’]
Printing lists
>>> fruit_shop=[‘oranges’, ’apples’, ’bananas’]
>>>print(fruit_shop[1])
Output
[’apples’]
Printing lists
>>> fruit_shop=[‘oranges’, ’apples’, ’bananas’]
>>>print(fruit_shop[2])
Output
[’bananas’]
Printing lists
>>> fruit_shop=[‘oranges’, ’apples’, ’bananas’]
>>>print(fruit_shop[0:1])
Output
[‘oranges’,’apples’]
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)
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’]
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’]
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
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’]
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’]
Tuples
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.”
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
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)
Maps (Dictionaries)
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’
]
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
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
Printing, Adding, Removing from Maps
>>>print(Favourite_stars[‘cricket’])
‘Sachin Tendulkar’
>>>favourite_stars[‘Tennis’ : ‘Federar’]
>>>del favourite_stars[‘athletic’]
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
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
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
Try this !
• Create a map/dictionary to store 10 English words an their
meanings
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 ’
]
Exercise !
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”
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
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
End of Chapter 3