Python Unit 1 Part 1

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 47

Python Programming

UNIT - I
 Python Basics
 Python Objects-
– Python Objects, Other Built-in Types, Internal Types, Standard Type Operators, Standard
Type Built-in Functions, Categorizing the Standard Types, Unsupported Types.
 Numbers-
– Introduction to Numbers, Integers, Floating Point Real Numbers, Complex Numbers,
Operators, Built-in Functions, Related Modules
 Sequences- Strings, Lists, and Tuples
 Mapping and Set Types
Python Basics [chapter-3]
 Statements and Syntax
 Variable Assignment
 Identifiers and Keywords
 Basic Style Guidelines
 Memory Management
 First Python Programs
Statements and Syntax
 Some rules and certain symbols are used with regard to statements in
Python:
– Hash mark ( # ) indicates Python comments
– NEWLINE ( \n ) is the standard line separator (one statement per line)
– Backslash ( \ ) continues a line
Statements and Syntax
 Some rules and certain symbols are used with regard to statements in
Python:
– Hash mark ( # ) indicates Python comments
– NEWLINE ( \n ) is the standard line separator (one statement per line)
– Backslash ( \ ) continues a line
Statements and Syntax
 Some rules and certain symbols are used with regard to statements in
Python:
– Hash mark ( # ) indicates Python comments
– NEWLINE ( \n ) is the standard line separator (one statement per line)
– Backslash ( \ ) continues a line
– Semicolon ( ; ) joins two statements on a line
Statements and Syntax
 Some rules and certain symbols are used with regard to statements in
Python:
– Hash mark ( # ) indicates Python comments
– NEWLINE ( \n ) is the standard line separator (one statement per line)
– Backslash ( \ ) continues a line
– Semicolon ( ; ) joins two statements on a line
– Colon ( : ) separates a header line from its suite
 Statements (code blocks) grouped as suites
– Suites delimited via indentation
– Python files organized as modules
Python Basics [chapter-3]
 Statements and Syntax
 Variable Assignment
 Identifiers and Keywords
 Basic Style Guidelines
 Memory Management
 First Python Programs
Variable Assignment
1. Assignment Operator
2. Augmented Assignment
3. Multiple Assignment
Variable Assignment
1. Assignment Operator
– The equal sign ( = ) is the main Python assignment
operator.
Variable Assignment
1. Assignment Operator
– The equal sign ( = ) is the main Python assignment operator.
– In C, you know that assignments are treated as expressions.
 This is not the case in Python, where assignments do not have inherent values.
Variable Assignment
1. Assignment Operator
– The equal sign ( = ) is the main Python assignment operator.
– In C, you know that assignments are treated as expressions.
 This is not the case in Python, where assignments do not have inherent values.
– Chaining together assignments is okay, in python
Variable Assignment
1. Assignment Operator
– When we assign an expression, the expression on the right is generally calculated first
and assigned the label on the left:
 a=2+4
//This means this is fine:
 a=2
 a=a+4
Variable Assignment
1. Assignment Operator
– Note that for functions, there is a difference between
 a = dir # Use "a" as an alias for "dir"
 a() # which we can then call.

and
 a = dir() # Run "dir" and get some result back from it,
 print(a) # assigning the result to "a" we can then print.
Note:- Python does not support pre-/post-increment nor pre-
/post-decrement operators such as x++ or --x.

Variable Assignment
2. Augmented Assignment
– the equal sign can be combined with an arithmetic operation and the resulting value
reassigned to the existing variable. Known as augmented assignment, statements such
as ...
 x=x+1
//... can now be written as ...
 x += 1
Variable Assignment
3. Multiple Assignment

"Multuple" Assignment
Variable Assignment
3. Multiple Assignment
Python Basics [chapter-3]
 Statements and Syntax
 Variable Assignment
 Identifiers and Keywords
 Basic Style Guidelines
 Memory Management
 First Python Programs
Python Basics [chapter-3]
 Statements and Syntax
 Variable Assignment
 Identifiers and Keywords
 Basic Style Guidelines
 Memory Management
 First Python Programs
Python Basics [chapter-3]
 Basic Style Guidelines
– Python Style Guide(s)
 Guido van Rossum wrote up a Python Style Guide ages ago. It has since been replaced by no fewer
than three PEPs: 7 (Style Guide for C Code), 8 (Style Guide for Python Code), and 257 (DocString
Conventions). These PEPs are archived, maintained, and updated regularly.
 "Pythonic," which describes the Python way of writing code, organizing logic, and object behavior.
 There is also another PEP, PEP 20, which lists the Zen of Python, starting you on your journey to
discover what Pythonic really means.
 import this from your interpreter. Here are some links :
– www.python.org/doc/essays/styleguide.html
– www.python.org/dev/peps/pep-0007/
– www.python.org/dev/peps/pep-0008/
– www.python.org/dev/peps/pep-0020/
– www.python.org/dev/peps/pep-0257/
Python Basics [chapter-3]
 Basic Style Guidelines
1. Module Structure and Layout
2. Create Tests in the Main Body
Python Basics [chapter-3]
 Basic Style Guidelines
1. Module Structure and Layout
 Modules are simply physical ways of logically organizing all your Python code. Within each file,
you should set up a consistent and easy-to-read structure. One such layout is the following:
# (1) startup line (Unix)
# (2) module documentation
# (3) module imports
# (4) variable declarations
# (5) class declarations
# (6) function declarations
# (7) "main" body
Typical Python file structure

Figure : illustrates the


internal structure of a typical
module.
1. Startup line
– Generally used only in Unix environments, the startup line allows for script execution by name only
(invoking the interpreter is not required).

2. Module documentation
– Summary of a module's functionality and significant global variables; accessible externally as
module.
doc .

3. Module imports
– Import all the modules necessary for all the code in current module; modules are imported once
(when
this module is loaded); imports within functions are not invoked until those functions are called.

4. Variable declarations
– Declare here (global) variables that are used by multiple functions in this module. We favor the use of
local variables over globals, for good programming style mostly, and to a lesser extent, for improved
performance and less memory usage.
5. Class declarations
– Any classes should be declared here. A class is defined when this module is imported and the class
statement executed. Documentation variable is class. doc .

6. Function declarations
– Functions that are declared here are accessible externally as module.function(); function is defined
when this module is imported and the def statement executed. Documentation variable is
function.
doc .

7. "main" body
– All code at this level is executed, whether this module is imported or started as a script; generally does
not include much functional code, but rather gives direction depending on mode of execution.
Python Basics [chapter-3]
 Basic Style Guidelines Main2.py
2. Create Tests in the Main
Body

Main1.py
Main2.py

Main1.py
Python Basics [chapter-3]
 Statements and Syntax
 Variable Assignment
 Identifiers and Keywords
 Basic Style Guidelines
 Memory Management
 First Python Programs
Python Basics [chapter-3]
 Memory Management
1. Variable Declarations (or Lack Thereof)
2. Dynamic Typing
3. Memory Allocation
4. Reference Counting
5. Garbage Collection
Python Basics [chapter-3]
 Memory Management
1. Variable Declarations (or Lack Thereof)
 In most compiled languages, variables must be declared before they are used.
 In fact, C is even more restrictive: variables have to be declared at the beginning of a code
block and before any statements are given.
 Other languages, like C++ and Java, allow "on-the-fly" declarations,
– i.e., those which occur in the middle of a body of code but these name and type declarations are
still required before the variables can be used.
 In Python, there are no explicit variable declarations. Variables are "declared" on first
assignment. Like most languages, however, variables cannot be accessed until they are
(created and) assigned:
 Memory Management
2. Dynamic Typing
– In Python, the type and memory space for an object are determined
and allocated at runtime
3. Memory Allocation
– Python simplifies application writing because the complexities of memory
management have been pushed down to the interpreter.
– The belief is that you should be using Python to solve problems with and not
have to worry about lower-level issues that are not directly related to your
solution.
 Memory Management
4. Reference Counting
– To keep track of objects in memory, Python uses the simple technique of reference
counting.
– An internal tracking variable, called a reference counter, keeps track of how many
references are being made to each object, called a refcount.
– When an object is created, a reference is made to that object, and when it is no longer
needed,
 i.e., when an object's refcount goes down to zero, it is garbage-collected.
– 1. Incrementing the Reference Count
– 2. Decrementing the Reference Count
1. Incrementing the Reference Count
– The refcount for an object is initially set to 1 when an object is created
and (its reference) assigned.
– New references to objects, also called aliases,
 occur when additional variables are assigned to the
– same object
– passed as arguments to invoke other bodies of code such as functions
– methods, or
– class instantiation, or
– assigned as members of a sequence or
– mapping.
• In summary, an object's refcount is increased
when
• It (the object) is created
x = 3.14
• Additional aliases for it are created
y = x
• It is passed to a function (new local
reference)
foobar(x)
• It becomes part of a container object
Fig: An object with two references
myList = [123, x, 'xyz']
2. Decrementing the Reference Count
– When references to an object "go away," the refcount is decreased.
– A reference also goes away when a variable is reassigned to another
object.
– Other ways in which an object's reference count goes down include
explicit removal of a reference using the del statement
– when an object is removed from a container (or if the reference count
to that container itself goes to zero).
• In summary, an object's refcount is decreased
when:
• A local reference goes out of scope, i.e., when
foobar() terminates
• Aliases for that object are explicitly destroyed
del y # or del x
• An alias is reassigned to another object (taking
on a new reference)
x = 123
It is explicitly removed from a container object
myList.remove(x)
• The container itself is deallocated
del myList # or goes out-of-scope
Python Basics [chapter-3]
 Memory Management
1. Variable Declarations (or Lack Thereof)
2. Dynamic Typing
3. Memory Allocation
4. Reference Counting
5. Garbage Collection
5. Garbage Collection
–Memory that is no longer being used is
reclaimed by the system using a mechanism known as
garbage collection.
–The interpreter keeps track of reference counts as above,
 but it is up to the garbage collector to deallocate the memory.
–The garbage collector is a separate piece of code that looks for
objects with reference counts of zero.
–It is also responsible to check for objects
with a reference count greater than zero that need to
be deallocated.
5. Garbage Collection
– Python's garbage collector is actually a combination of reference
counting and the periodic invocation of a cyclic garbage collector.
– When an object's refcount reaches zero, the interpreter pauses to
deallocate it and all objects that were reachable only from that object.
– In addition to this reference counting, the garbage collector also notices
if a large number of objects have been allocated (and not deallocated
though reference counting).
 In such cases, the interpreter will pause to try to clear out any
unreferenced cycles.

You might also like