You are on page 1of 36

Python

What is …..
• Python is a powerful object-oriented programming language that is
frequently used for scripting.

• Some benefits of Python is ease of learning, open source license,


compatibility with platforms, shell coding, and automatic memory
management.
Vocabulary
• Lines
• Statement Separator is a semicolon
• Continuation line is a backslash

• Comments(#,’’’,”””,__doc__)
• Names and Tokens

• Names are case-sensitive.


• The characters permitted are a-z, A-Z, 0-9, and underscores.
• First character can be an underscore or a letter.
• Identifiers can be of any length.
• Special names and customized names typically begin and end in double
• underscores.
• Special name classes have single and double underscores.
• Single leading underscores indicates a private method or variable.
• Single trailing underscores prevent conflicts with Python keywords.
• Double leading underscores exist in a class definition to create name
• mangling (weak hiding). They are, however, rarely used.

The naming conventions are not rigid, however, the following rules are
applicable:
• Module and package names should be in lower case.
• Globals and constants should be in upper case.
• Class names should have bumpy caps with the first letter in upper
• case, for example, ThisIsNewC1ass.
• Method and function names should be in lower case with words
• separated by underscores.
• Local variables should be in lower case with an underscore
• between words or bumpy caps with first letter in lower case.
• Names and variables in Python do not have a type. Values have types.
• Blocks and Indentation:
• Python identifies block structures and nested block structures with indentation
instead of starting and ending brackets. An empty block uses the pass
statement. This statement performs no action and acts as a Python
placeholder.
Following are the benefits of using indentation to indicate a block structure:
• Reduces the requirement of a coding standard, as a developer only specifies
four spaces without hard tabs for indentation.
• Reduces inconsistency, as code from different sources follow the same
indentation style
• Reduces work, as only indentation is required instead of brackets as well as
indentation
• Reduces clutter by eliminating all curly brackets
• Gives a precise look to the reader
Dynamic Typing
• A developer is not required to declare the specific types of objects in use,
while writing a Python script.
Built In Objects:
Number Data Type in Python:

• Python supports integers, floating point numbers and complex numbers. They
are defined as int, float and complex class in Python.

• Integers and floating points are separated by the presence or absence of a


decimal point. 5 is integer whereas 5.0 is a floating point number.

• Complex numbers are written in the form, x + yj, where x is the real part and y
is the imaginary part.

• We can use the type() function to know which class a variable or a value
belongs to and isinstance() function to check if it belongs to a particular class.
• a=5

• # Output: <class 'int'>


• print(type(a))

• # Output: <class 'float'>


• print(type(5.0))

• # Output: (8+3j)
• c = 5 + 3j
• print(c + 3)

• # Output: True
• print(isinstance(c, complex))
• Integers can be of any length, a floating point number is accurate only up to
15 decimal places (the 16th place is inaccurate).
Coercion
• Operations like addition, subtraction coerce integer to float implicitly
(automatically), if one of the operand is float.
• >>> 1 + 2.0
• 3.0
• We can also use built-in functions like int(), float() and complex() to convert
between types explicitly. These function can even convert from strings.
• >>> int(2.3)
•2
• >>> int(-2.8)
• -2
• >>> float(5)
• 5.0
• >>> complex('3+5j')
• (3+5j)
Operations On Numbers(egs):
• 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 and unindexed. No duplicate
members.
• Dictionary is a collection which is unordered, changeable and
indexed. No duplicate members.
Python List
• List is a collection which is ordered and changeable. Allows duplicate
members.
• They are compound datatypes often referred to as sequences. List is one of
the most frequently used and very versatile datatype used in Python.
• Lists are just like the arrays, declared in other languages. Lists need not be
homogeneous always which makes it a most powerful tool in Python. A single
list may contain DataTypes like Integers, Strings, as well as Objects. Lists are
mutable, and hence, they can be altered even after their creation.
How to create a list?
• In Python programming, a list is created by placing all the items (elements)
inside a square bracket [ ], separated by commas.
• It can have any number of items and they may be of different types (integer,
float, string etc.).

• # empty list
• my_list = []
• # list of integers
• my_list = [1, 2, 3]
• # list with mixed datatypes
• my_list = [1, "Hello", 3.4]
• Also, a list can even have another list as an item. This is called nested list.
# nested list
my_list = ["mouse", [8, 4, 6], ['a']]
Access elements from a list( List Index)
• Index starts from 0. So, a list having 5 elements will have index from 0 to 4.
• Trying to access an element other that this will raise an IndexError. The index
must be an integer. We can't use float or other types, this will result into
TypeError.
• my_list = ['p','r','o','b','e']
• # Output: p
• print(my_list[0])
• # Output: o
• print(my_list[2])
• # Output: e
• print(my_list[4])
• # Error! Only integer can be used for indexing
• # my_list[4.0]
• # Nested List
• n_list = ["Happy", [2,0,1,5]]
• # Nested indexing
• # Output: a
• print(n_list[0][1])
• # Output: 5
• print(n_list[1][3])
Negative indexing
• Python allows negative indexing for its sequences. The index of -1 refers to
the last item, -2 to the second last item and so on.

• my_list = ['p','r','o','b','e']
• # Output: e
• print(my_list[-1])
• # Output: p
• print(my_list[-5])
Slice lists in Python
We can access a range of items in a list by using the slicing operator (colon).
my_list = ['p','r','o','g','r','a','m','i','z']

# elements 3rd to 5th


print(my_list[2:5])

# elements beginning to 4th


print(my_list[:-5])

# elements 6th to end


print(my_list[5:])

# elements beginning to end


print(my_list[:])
Change or add elements to a list
• List are mutable, meaning, their elements can be changed unlike string or tuple.
• We can use assignment operator (=) to change an item or a range of items.

• # Original values
• odd = [2, 4, 6, 8]
• # change the 1st item
• odd[0] = 1
• # Output: [1, 4, 6, 8]
• print(odd)
• # change 2nd to 4th items
• odd[1:4] = [3, 5, 7]
• # Output: [1, 3, 5, 7]
• print(odd)
We can add one item to a list using append() method or add several items using
extend() method.

odd = [1, 3, 5]
odd.append(7)
# Output: [1, 3, 5, 7]
print(odd)

odd.extend([9, 11, 13])


# Output: [1, 3, 5, 7, 9, 11, 13]
print(odd)
• We can also use + operator to combine two lists. This is also called
concatenation.
• The * operator repeats a list for the given number of times.

odd = [1, 3, 5]
# Output: [1, 3, 5, 9, 7, 5]
print(odd + [9, 7, 5])
#Output: ["re", "re", "re"]
print(["re"] * 3)
we can insert one item at a desired location by using the method insert() or
insert multiple items by squeezing it into an empty slice of a list.
• odd = [1, 9]
• odd.insert(1,3)
• # Output: [1, 3, 9]
• print(odd)
• odd[2:2] = [5, 7]
• # Output: [1, 3, 5, 7, 9]
• print(odd)
Delete or remove elements from a list:
We can delete one or more items from a list using the keyword del. It can even delete the list
entirely.
my_list = ['p','r','o','b','l','e','m']
# delete one item
del my_list[2]
# Output: ['p', 'r', 'b', 'l', 'e', 'm']
print(my_list)
# delete multiple items
del my_list[1:5]
# Output: ['p', 'm']
print(my_list)
# delete entire list
del my_list
# Error: List not defined
print(my_list)
We can also delete items in a list by assigning an empty list to a slice of
elements.

>>> my_list = ['p','r','o','b','l','e','m']


>>> my_list[2:3] = []
>>> my_list
['p', 'r', 'b', 'l', 'e', 'm']
>>> my_list[2:5] = []
>>> my_list
['p', 'r', 'm']
Python List Methods
Some Examples
pow2 = [2 ** x for x in range(10)]
# Output: [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
print(pow2)

pow2 = []
for x in range(10):
pow2.append(2 ** x)
• >>> pow2 = [2 ** x for x in range(10) if x > 5]
• >>> pow2
• [64, 128, 256, 512]
• >>> odd = [x for x in range(20) if x % 2 == 1]
• >>> odd
• [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
• >>> [x+y for x in ['Python ','C '] for y in ['Language','Programming']]
• ['Python Language', 'Python Programming', 'C Language', 'C
Programming']
List Membership Test
• We can test if an item exists in a list or not, using the keyword in.
• my_list = ['p','r','o','b','l','e','m']
• # Output: True
• print('p' in my_list)
• # Output: False
• print('a' in my_list)
• # Output: True
• print('c' not in my_list)
• Iterating Through a List
• Using a for loop we can iterate though each item in a list.

You might also like