Python Introduction
Python Introduction
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
History of Python
History of Python
Guido van Rossum
Python is easy and powerful because
– It has functional programming language like C.
– It is an Object-oriented programming language like C++ (not from Java)
More libraries and packages for Python3 to make it more feature rich than
Python2
We'll use the Python3 version in the Lab.
Interpreter
The interpreter will transform the code from the first to the last word by word
If interpreter find one error it will stop working raising this error
Like Python
Compiler
The compiler will transform the whole code
If the compiler found any error it will continue and when it finish it will raise
all the errors
Like C#
Python Editors
Python IDLE
Pycharm
Eclipse
Aptana
Vistual Studio
Notepad++
And more
Comments
Running Python - points to remember
You can run a single line of Python statement from the Python shell
If you want to run multiple Python commands together, create a file, save it with the
extension .py
Python interpreter reads one Python statement at a time, line by line; starts from top
of the file and goes line by line
One line of Python is interpreted as one Python statement unless postfixed by
backslash \ or bracket []
If there is any error in the Python program, the interpreter throws error; fix it
and re-
run the program.
Multiple python statements in a single line - all separated by ;
Variable
Points to remember
Group of characters enclosed within ' '," ", ''' ''',""" """ called a string in
Python.
a = 40 means the value 40 stored in the same memory location where the
variable a points to. The address of this memory location is id(a).
The variable a can be of any data type. In Python, its not required to declare
what type of data the variable will refer to.
The value a variable points to keeps on changing - Python is dynamically
typed language.
The Print() command prints the output.
33 keywords & Reserved words
True, False, None
and, or, not, is
if, else, elif
while, for, break, continue, return, in, yield
try, except, finally, raise, in, yield
import, from, as, class, def, pass, global, nonlocal, lambda, del, with
All keywords start with lower case letter, except first three
OUTPUT:-
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue',
'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if',
'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return',
'try', 'while', 'with', 'yield']
Identifiers
Names you give to variables, functions, packages etc.
Rules to follow while naming identifiers
Valid characters in a name are:
– Numbers 0 to 9
– Small letters a to z
– Capital letters A to Z
– The underscore character _
~ + - Ccomplement, unary plus and minus (method names for the last two are +@
and -@)
OUTPUT
OUTPUT
General format: >>> a = b = 0
while <test1>: # loop test >>> while a < 10:
<statements1> # loop ... a += 3
else: body ... print(a)
<statemen # optional ...
ts2> else 3
# run if 6
loop didn't 9
break
12
# run if
while >>> while True:
becomes ... b += 3
false ... if b >= 10: break
... print(b)
3
6
9
>>> a = 0
>>> while a < 5: a +=1
>>> print a
5
>>> for i in [2, 5, 3]:
for <target> in <object>:
<statements> ... print(i**2)
else: # optional, didn't hit a break 4
<other statements> 25
9
>>> for j in range(5): print(j)
0
1
2
3
4
>>> range(3, 10, 2)
[3,5,7,9]
>>> d = {'this': 2, 'that': 7}
>>> for k, v in d.items():
... print('%s is %i'%(k, v))
this is 2
that is 7
Auxiliary Statements if while for
elif •
else • • •
break • •
continue • •
pass • • •
Note:- pass is valid anywhere a suite (single or multiple statements) is required (also
includes elif, else, class, def, Try, except, finally).
Function
Syntax:
Default Arguments
Function Description
Display attributes of object or the names of global variables if
dir([obj])
noparameter given
Display object's documentation string in a pretty-printed format
help([obj]) or enters interactive help if no parameter given
int(obj) Convert object to an integer
len(obj) Return length of object
open(fn, mode) Open file fn with mode ('r' = read, 'w' = write)
Return a list of integers that begin at start up to but not
range(start, stop, step) including stop in increments of step; start defaults to 0, and step
defaults to 1
Wait for text input from the user, optional prompt string can be
raw_input(str)
provided
str(obj) Convert object to a string
type(obj) Return type of object (a type object itself!)