Python Basics
Python Basics
By - Tarun Kumar
(ekmrtrn)
Contents
• Python Files
• File Operation
• Directory
• Exception
• Exception Handling
Official Ericsson Pow erpoint Template | Ericsson Internal | LME-11:002291 Uen, Rev A | 2011-06-21 | Page 4
Python Introduction
Official Ericsson Pow erpoint Template | Ericsson Internal | LME-11:002291 Uen, Rev A | 2011-06-21 | Page 6
Python Introduction
Python Statement :
• Instructions that a Python interpreter can execute are called statements.
For example, a = 1 is an assignment statement. if statement, for
statement, while statement, etc. are other kinds of statements which will
be discussed later.
Python Indentation :
• Most of the programming languages like C, C++, and Java use braces
{ } to define a block of code. Python, however, uses indentation.
• A code block (body of a function, loop etc.) starts with indentation and
ends with the first unindented line. The amount of indentation is up to
you, but it must be consistent throughout that block.
• Generally, four whitespaces are used for indentation and are preferred
over tabs.
Official Ericsson Pow erpoint Template | Ericsson Internal | LME-11:002291 Uen, Rev A | 2011-06-21 | Page 7
Python Introduction
Python Variables :
• A variable is a named location used to store data in the memory. It is
helpful to think of variables as a container that holds data that can be
changed later in the program. For example, number = 22
Official Ericsson Pow erpoint Template | Ericsson Internal | LME-11:002291 Uen, Rev A | 2011-06-21 | Page 9
Python Introduction
Python List
• List is an ordered sequence of items. It is one of the most used datatype
in Python and is very flexible. All the items in a list do not need to be of
the same type
• Declaring a list is pretty straight forward. Items separated by commas are
enclosed within brackets [ ].
Python Tuples
• Tuple is an ordered sequence of items same as a list. The only difference is
that tuples are immutable. Tuples once created cannot be modified.
• Tuples are used to write-protect data and are usually faster than lists as
they cannot change dynamically.
• It is defined within parentheses ( ) where items are separated by commas.
Official Ericsson Pow erpoint Template | Ericsson Internal | LME-11:002291 Uen, Rev A | 2011-06-21 | Page 10
Python Introduction
Python Strings
• String is sequence of Unicode characters. We can use single quotes or
double quotes to represent strings. Multi-line strings can be denoted
using triple quotes, ’ ’ ’ or “ “ “
Python Tuples
• Tuple is an ordered sequence of items same as a list. The only difference is
that tuples are immutable. Tuples once created cannot be modified.
• Tuples are used to write-protect data and are usually faster than lists as
they cannot change dynamically.
• It is defined within parentheses ( ) where items are separated by commas.
Official Ericsson Pow erpoint Template | Ericsson Internal | LME-11:002291 Uen, Rev A | 2011-06-21 | Page 11
Python Introduction
Python Set
• Set is an unordered collection of unique items. Set is defined by values
separated by comma inside braces { } . Items in a set are not ordered.
Python Dictionary
• Dictionary is an unordered collection of key-value pairs.
• It is generally used when we have a huge amount of data. Dictionaries are
optimized for retrieving data. We must know the key to retrieve the value.
• In Python, dictionaries are defined within braces {} with each item being a
pair in the form key:value. Key and Value can be of any type.
Official Ericsson Pow erpoint Template | Ericsson Internal | LME-11:002291 Uen, Rev A | 2011-06-21 | Page 12
Python Introduction
Python Output using print() function
• We can use the print() function to output data to the standard output
device(screen).
Python Import
• A module is a file containing Python definitions and statements. Python
Modules have a filename and end with the extension .py
• Definitions inside a module can be imported to another module or the
interactive interpreter in Python.We use the import keyword to do this.
Official Ericsson Pow erpoint Template | Ericsson Internal | LME-11:002291 Uen, Rev A | 2011-06-21 | Page 13
Python Introduction
Python Operators
• Operators are special symbols in Python that carry out arithmetic or
logical computation. The value that the operator operates on is called the
operand.
• Types of Operators:
• Arithmetic Operators
• Comparison Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Special Operators
• Identity Operators
• Membership Operators
Official Ericsson Pow erpoint Template | Ericsson Internal | LME-11:002291 Uen, Rev A | 2011-06-21 | Page 14