TPJ Python - Module 2 New
TPJ Python - Module 2 New
Python Lists
• Lists are used to store multiple items in a single
variable.
A slice goes up to, but will not include, the value at the second index. A slice
evaluates to a new list value.
Tuples
• Unchangeable / immutable
Tuples are unchangeable, meaning that we cannot change, add
or remove items after the tuple has been created.
• Allow Duplicates
Since tuples are indexed, they can have items with the same
value:
thistuple = ("apple", "banana", "cherry", "apple", "cherry")
• Tuple Items - Data Types
tuple1 = ('hello’,)
tuple2 = ('hello')
Basic Tuples Operations
Defining a Dictionary
print('Howdy!') print('Howdy!!!')
print('Hello there.')
print('Howdy!!!')
print('Howdy!')
print('Hello there.')
print('Howdy!!!')
hello() print('Hello there.')
hello() print('Howdy!')
hello() print('Howdy!!!')
print('Hello there.')
Howdy! Howdy!
Howdy!!! Howdy!!!
Howdy! Howdy!
Howdy!!! Howdy!!!
Howdy! Howdy!
Howdy!!! Howdy!!!
Hello, Alice
Hello, Bob
Parameter
def hi(name):
print('Hello, ' + name)
hi('Alice’)
hi('Bob’)
Function
Define
Parameter
def sayHello(name):
Call print('Hello, ' + name)
sayHello('Al') Argument
The return statement also denotes that the function has ended.
Any code after return is not executed.
In the above example, we have created a function named find_square().
The function accepts a number and returns the square of the number.
None Value
The None keyword is used to define a null value,
or no value at all.
None is not the same as 0, False, or an empty
string.
None is a data type of its own (NoneType) and
only None can be None.
Example
Keyword Arguments and the print() Function
In this new local scope, the local variable ham is set to 101,
and a local variable eggs—which is different from the one in
spam()’s local scope—is also created and set to 0.
Since the local scope for the call to spam() still exists, the
only eggs variable is the spam() function’s eggs variable,
which was set to 99.
Four rules to tell whether a variable is in a local
scope or global scope:
Hint
Input:
Enter value of a = 7
Enter value of b = 5
Expected output
Sum is = 12
Sub is = 2
Multiplication is = 35