UNIT-I by Chiru Sai
UNIT-I by Chiru Sai
Python Basics
Definition
https://www.python.org
/
Python Features
‘‘‘
--------------------------
--------------------------
’’’
Python Variables
Example:
x = 10
y = "cse"
contd..
● Python allows you to assign values to multiple variables in one
line:
line: x = y = z = "Orange"
as elif if or yield
user = int(user)
Python Operators
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Division
// Floor Division
** Exponentiation
Comparison Operators
Operator Operator Name
== Equal to
!= Not Equal to
Logical Operators
Logical operators are used to combine comparison statements
Operator Description
= Is Equal to
+= Plus Equals
Identity operators are used to compare the objects, not if they are
equal, but if they are actually the same object, with the same
memory location
Operator Description
Operator Description
s AND
| OR
^ XOR
~ NOT
if expression:
if_suite
if-else Statement
if expression:
if_suite
else:
else_suite
else-if Statement
Python has an "else-if" statement named elif which has the following
syntax:
if expression1:
if_suite
elif expression2:
elif_suite Surprise: There is no switch
else: or case statement in
Python.
else_suite
while Loop
while expression:
while_suite
for_suite
break Statement
● The break statement in Python terminates the current loop and
resumes execution at the next statement, just like the
traditional break found in C.
● The most common use for break is when some external
condition is triggered (usually by testing with an if statement),
requiring a hasty exit from a loop.
● The break statement can be used in both while and for loops.
continue Statement
● The continue statement rejects all the remaining statements in the
current iteration of the loop and moves the control back to the
top of the loop.
● The while loop is conditional, the conditional expression is
checked for validity before beginning the next iteration of the
loop
● The for loop is iterative, a determination must be made as
to whether there are any more arguments to iterate over
pass Statement
IDENTITY object's identifier can be obtained using the id() built-in function.
This value is as close as you will get to a "memory address" in
Python.
An object's type indicates what kind of values an object can hold,
what operations can be applied to such objects, and what behavioral
TYPE
rules these objects are subject to. You can use the type() built-in
function to reveal the type of a Python object.
(empty list)
Other Built-in Types
● Type
● None
● File
● Function
● Module
● Class
● Class Instance
● Method
Types and type() built-in Function
type(object)
● The type() built-in function takes object and returns its type
object.
None
● Code
● Frame
● Traceback
● Slice
● Ellipsis
● Xrange
Code Objects
● Code objects are executable pieces of Python source that are
byte-compiled, usually as return values from calling the compile()
built-in function.
● Such objects are appropriate for execution by either exec() or
by the eval() built-in function.
contd..
● Value Comparison
● Object Identity Comparison
● Boolean
Standard Type Built-in Functions
Python also provides some builtin functions that can be applied to all
the basic object types:
cmp()
repr()
str()
type()
"Basic," indicating that these are the standard or core types that Python
provides
"Built-in," due to the fact that types these come default with Python.
● Storage Model
● Update Model
● Access Model
Storage Model
● direct
● sequence
● Mapping
contd...
direct numbers
mapping dictionaries
Summary : Categorizing the Standard Types
Data Type Storage Model Update Model Access Model
● Boolean
● char or byte
● Pointer
● int vs. short vs. long
● float vs. double
Numbers
Introduction to Numbers
● Numbers provide literal or scalar storage and direct access.
● Numbers are also an immutable type, meaning that changing or
updating its value results in a newly allocated object.
anInt = anInt + 1
aFloat =
contd..
How to Remove Numbers
(Plain) Integers
● The first thing we need to say about Python long integers is to not
get them confused with long integers in C or other compiled
languages.
● Python long integers are limited only by the amount of
(virtual) memory in your machine.
● In other words, they can be very L-O-N-G longs.
contd..
299792458l 0xDECADEDEADBEEFBADFEEDDEAL
contd..
Floating Point Real Numbers
Complex Numbers
attribute description
Some convert from one numeric type to another while others are more
operational, performing some type of calculation.
Conversion
The int(), long(), float(), and complex() built-in functions are used
to convert from any numeric type to another.
contd..
Operational
coerce( num1, num2 ) converts num1 and num2 to the same numeric
type and returns the converted pair as a tuple
Not Available in Python 3
divmod( num1, num2 ) division-modulo combination returns (num1 /
num2, num1 % num2) as a tuple. For floats and
complex, the quotient is rounded down
(complex uses only real component of
quotient)
pow( num1, num2, mod =1) raises num1 to num2 power, quantity modulo
mod if provided
Base Representation
function operation
The next family of Python types are those whose items are ordered
and sequentially accessible via index, known as sequences.
● Strings
● Lists
● Tuples
contd..
contd..
Sequence Operator Functionality
Conversion
Function Operation
Function Operation
len (seq) returns length (number of items) of seq
format_string % (arguments_to_convert)
Format Operator Conversion Symbols
Format Symbol Conversion
%c character
%o octal integer
%x or %X hexadecimal integer
%e or %E exponential notation
Symbol Functionality
- left justification
Method Description
brackets [ ]
contd..
Slicing works similar to strings; use the square bracket slice operator
[ ] along with the index or indices.
You can update single or multiple elements of lists by giving the slice
on the left-hand side of the assignment operator.
To remove a list element, you can use either the del statement
or remove() method
List Type Built-in Methods
List Method Operation
● Stack
● Queue
Tuples
The only visible difference between tuples and lists is that tuples use
parentheses and lists use square brackets.
Slicing works similar to lists: Use the square bracket slice operator
([ ]) along with the index or indices.
Like numbers and strings, tuples are immutable which means you
cannot update them or change values of tuple elements.
contd..
How to Remove Tuple Elements and Tuples
key:value
contd..
How to Create and Assign Dictionaries
To access dictionary elements, you use the square brackets along with
the key to obtain its value.
contd..
How to Update Dictionaries
get(key, default=None) for key, returns value or None if key not in dictionary