You are on page 1of 26

Python Basics

By
Vivek Shukla
Topics
• Data Types
• Functions
• Anonymous Functions
• File Handling
• Modules and Packages
• Exception Handling
Data Types in Python
• The data types defined in Python are:
• Tuple
• List
• Dictionary
• Numbers
• Sets
Tuple
• Contain the collection of the items of different
data types.
• The items of the tuple are separated with a
comma (,) and enclosed in parentheses ().
• A tuple is a read-only data structure as we
can't modify the size and value of the items of
a tuple.
List

• list can contain data of different types


• The items stored in the list are separated with
a comma (,) and enclosed within square
brackets [].
• use slice [:] operators to access the data of the
list
Dictionary
• It is an ordered set of a key-value pair of
items.
• Key can hold any primitive data type whereas
value is an arbitrary Python object.
• The items in the dictionary are separated with
the comma and enclosed in the curly braces
{}.
Numbers
• Number stores numeric values.
• Python creates Number objects when a
number is assigned to a variable.
• Four Types:
• int
• long
• float
• complex
Set
• It is an unordered collection of unique items.
• Set is defined by values separated by comma
inside braces { }.
• Items in a set are not ordered.
• slicing operator [] does not work.
Function
• Definition and call
• Function is a group of related statements that
perform a specific task.
• It breaks our program into smaller and
modular chunks.
• Functions make programs more organized and
manageable
Scope
• A variable is only available from inside the region
it is created. This is called scope.
• Local- A variable created inside a function
belongs to the local scope of that function, and
can only be used inside that function.
• Global-A variable created in the main body of the
Python code is a global variable and belongs to
the global scope.
• Global variables are available from within any
scope, global and local.
Return
• Return statement is used to return values
from the function.
• It can’t be used outside of a Python function
• i.e. We can use the return statement in a
function only.
Arguments
• Define a function that takes variable number
of arguments.
• Define such functions using default, keyword
and arbitrary arguments
Lambda Function
• Anonymous function is a function that is
defined without a name.
• Defined using the lambda keyword instead of
def keyword.
• Syntax is:
lambda arguments: expression
Advance Function
• Map
• Applies a given function to each item of an
iterable (list, tuple etc.) and returns a list of the
results.
• Syntax:
• map(function, iterable, ...)
• function - map() passes each item of
the iterable to this function.
• iterable iterable which is to be mapped
Filter
• filter() method constructs an iterator from
elements of an iterable for which a function
returns true.
• filter(function, iterable)
• function - function that tests if elements of an
iterable returns true or false
• iterable - iterable which is to be filtered
Importing Modules
• Simply, a module is a file consisting
of Python code
• Make use of the functions in a module, you’ll
need to import the module with
an import statement.
Sys Module
• The sys module provides information about
constants, functions and methods of the
Python interpreter.
• dir(system) gives a summary of the available
constants, functions and methods.
• Using help(sys) provides valuable detail
information.
OS Module
• Provides functions for interacting with the
operating system.
• This module provides a portable way of using
operating system dependent functionality.
• The *os* and *os.path* modules include
many functions to interact with the file
system.
Packages
• A package is a collection of Python modules
• A package is a directory of Python modules
containing an additional __init__.py file.
• Ex: datetime
Exception Handling
• Syntax Error
• It arise when the Python parser is unable to
understand a line of code.
• syntax errors are typos, incorrect indentation,
or incorrect arguments.
• Logical errors – also called semantic errors,
logical errors cause the program to behave
incorrectly, but they do not usually crash the
program.
Runtime Error
• A problem that went undetected when the
program was parsed, but is only revealed
when the code is executed.
• Division by zero
• Trying to access a file which doesn’t exist
• Accessing a list element, dictionary value or
object attribute which doesn’t exist
Try and Except
• try() is used in Error and Exception Handling
• List of Exception Errors:
• IO Error : if file can’t be opened
• Keyboard Interrupt : when an un required key is
pressed by the user
• Value Error : when built-in function receives a
wrong argument
• EOF Error : if End-Of-File is hit without reading
any data
• Import Error :if it is unable to find the module
finally keyword

• finally block is always executed after leaving


the try statement. In case if some exception
was not handled by except block, it is re-raised
after execution of finally block.
• finally block is used to de-allocate the system
resources.
• One can use finally just after try without
using except block, but no exception is
handled in that case.

You might also like