Python Module 1
Python Module 1
Python Module 1
Python Introduction- Features, Identifiers, Reserved words, Indentation, Comments, Built-in Data
types and their Methods: Strings, List, Tuples, Dictionary, and Set - Type Conversion- Operators.
Execution of a Python, Program, Writing Our First Python Program, Statements Precedence of
Operators.
Python Collections (Arrays)
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
Tuple
• Tuple Items - Data Types
• Tuple items can be of any data type:
• Example
• String, int and boolean data types:
• tuple1 = ("apple", "banana", "cherry")
tuple2 = (1, 5, 7, 9, 3)
tuple3 = (True, False, False)
• A tuple can contain different data types:
• Example
• A tuple with strings, integers and boolean values:
• tuple1 = ("abc", 34, True, 40, "male")
Tuple
• type()
• From Python's perspective, tuples are defined as objects with the
data type 'tuple':
• <class 'tuple'>
• Example
• What is the data type of a tuple?
• mytuple = ("apple", "banana", "cherry")
print(type(mytuple))
• The tuple() Constructor
• It is also possible to use the tuple() constructor to make a tuple.
• Example
• Using the tuple() method to make a tuple:
• thistuple = tuple(("apple", "banana", "cherry")) # note the
double round-brackets
print(thistuple)
Python Collections (Arrays)
• There are four collection data types in the Python
programming language:
• List is a collection which is ordered and changeable. Allows
duplicate members.
• Tuple is a collection which is ordered and unchangeable.
Allows duplicate members.
• Set is a collection which is unordered, unchangeable*, and
unindexed. No duplicate members.
• Dictionary is a collection which is ordered** and
changeable. No duplicate members.
• When choosing a collection type, it is useful to understand
the properties of that type. Choosing the right type for a
particular data set could mean retention of meaning, and, it
could mean an increase in efficiency or security.
tuple
• Access Tuple Items
• You can access tuple items by referring to the index
number, inside square brackets:
• ExampleGet your own Python Server
• Print the second item in the tuple:
• thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
• Negative Indexing
• Negative indexing means start from the end.
• -1 refers to the last item, -2 refers to the second last item
etc.
• Print the last item of the tuple:
• thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])
Tuple
• Range of Indexes
• You can specify a range of indexes by specifying where
to start and where to end the range.
• When specifying a range, the return value will be a new
tuple with the specified items.
• Example
• Return the third, fourth, and fifth item:
• thistuple =
("apple", "banana", "cherry", "orange", "kiwi", "me
lon", "mango")
print(thistuple[2:5])
• Note: The search will start at index 2 (included) and
end at index 5 (not included).
Tuple
• Example
• This example returns the items from the beginning to, but NOT
included, "kiwi":
• thistuple =
("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[:4])
• Range of Negative Indexes
• Specify negative indexes if you want to start the search from the end
of the tuple:
• Example
• This example returns the items from index -4 (included) to index -1
(excluded)
• thistuple =
("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[-4:-1])
Tuple
Example
Assign the rest of the values as a list called "red":
fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")
(green, yellow, *red) = fruits
print(green)
print(yellow)
print(red)
center() Returns a centered string isascii() Returns True if all characters in the string are
ascii characters
count() Returns the number of times a specified
value occurs in a string
format() Formats specified values in a string isprintable() Returns True if all characters in the string are
printable
format_map() Formats specified values in a string isspace() Returns True if all characters in the string are
whitespaces
rjust() Returns a right justified version of upper() Converts a string into upper
the string case
rpartition() Returns a tuple where the string is
parted into three parts zfill() Fills the string with a
specified number of 0 values
rsplit() Splits the string at the specified at the beginning
separator, and returns a list
difference_update() Removes the items in this set that are also included in another, specified set
symmetric_difference_update() inserts the symmetric differences from this set and another
Output :-
& Binary AND Operator copies a bit to the result if it exists in both operands (a & b) (means 0000 1100)
^ Binary XOR It copies the bit if it is set in one operand but not both. (a ^ b) = 49 (means 0011 0001)
and Logical AND If both the operands are true then condition becomes true. (a and b) is true.
not Logical NOT Used to reverse the logical state of its operand. Not(a and b) is false.
Python Membership Operators
Python's membership operators test for membership in a
sequence, such as strings, lists, or tuples. There are two
membership operators.
d=5 Value of (a + b) * c / d is 90
Value of ((a + b) * c) / d is 90
e=0 Value of (a + b) * (c / d) is 90
e = (a + b) * c / d #( 30 * 15 ) / 5 Value of a + (b * c) / d is 50