I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )

13
INTRODUCTION TO PYTHON- GETTING STARTED (CONT)

description

C ONTINUE.. For strings, it is easy to iterate over each character: >>> foo ='abc‘ >>> for c in foo: print c a b c The range ( ) function has been often seen with len ( ) for indexing into a string. Example: >>> foo='abc' >>> for i in range (len(foo)): print foo[i],'(%d)'%i a (0) b (1) c (2) Display both elements and their corresponding index value:

Transcript of I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )

Page 1: I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )

INTRODUCTION TO PYTHON-GETTING STARTED (CONT)

Page 2: I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )

CONTINUE..>>> for eachnum in [0,1,2]:

print eachnum

012 Eachnum contains the integer value. Python provides the range ( ) built-in function to generate such a listExample:>>> for eachnum in range (3):

print eachnum

012>>>

Page 3: I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )

CONTINUE.. For strings, it is easy to iterate over each character:

>>> foo ='abc‘>>> for c in foo:

print cabc

The range ( ) function has been often seen with len ( ) for indexing into a string. Example:

>>> foo='abc'>>> for i in range (len(foo)):

print foo[i],'(%d)'%i

a (0)b (1)c (2)

Display both elements and their corresponding index value:

Page 4: I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )

BUILT-IN FUNCTIONSFunction Description dir([obj]) Display attributes of object or the names of global

variables if no parameter given.help([obj]) Display object’s documentation string in a preetty-

printed format or enters interactive help if no parameter given

int (obj) Convert object to an integerlen (obj) Return length of objectopen(fn,mode) Open file fn with mode ‘r’=read, ‘w’=writeRange([start,]stop[,step])

Return a list of integers that begin at start up to but not including stop in increments of step; start defaults to 0 and step defaults to 1

raw_input(str) Wait for text input from the user; optional prompt string can be provided

str(obj) Convert object to a stringtype (obj) Return type of object ( a type object itself)

Page 5: I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )

STATEMENTS AND SYNTAX Python statements are in general delimited

by NEWLINEs- means one statement per line. Single statements can be broken up into

multiple lines by using the backslash (\) . Placed the (\) before a NEWLINE to continue

the current statement onto the next line.>>> #check condition>>> if (weather_is_hot ==1) and \ (shark_warning_==0):

Page 6: I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )

MULTIPLE STATEMENTS GROUPS AS SUITES(:) Groups of individual statements making up a

single code block are called “suites” in Python.

Compound /complex statements such as if, while are those require a header line and a suite.

Header line begin the statement (with the keyword and terminate with a colon (:), and are followed by one /more lines that make up the suite.

A combination of a header line and a suite as a clause.

Page 7: I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )

VARIABLE ASSIGNMENT Assignment operators (=) anInt =-12aString=‘cart’aFloat =-3.1234alist= [3.124,’ abcd’,8.82]

Augmented assignment The equal sign can be combined with an

arithmetic operation and the resulting value reassigned to the existing variable.

Augmented assignment refer to the use of operators, which imply both an arithmetic operation as well as assignments.

Page 8: I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )

CONTINUE..Example :+= ,-=,*=, /=,%=,**= , <<=,>>=,&=,^=,|=Thus , x = x+1 x+=1

Python does not support pre/post increment nor pre/post-decrement such as x++, --x

Multiple assignment It is possible to assign multiple objects to multiple

variablesExample:>>> x=y=z=1>>> x1>>> y1>>> z1

Page 9: I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )

CONTINUE.. “Multuple “assignment –an alternative way to

assign multiple variables . Not an official python term, used it because

when assigning variables this way, the objects on both sides of the equal sign are tuples, a python standard type.

Example:>>> x,y,z =1,2,'a string'>>> x1>>> y2>>> z'a string'

Page 10: I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )

CONTINUE.. ‘multuple” assignments , not required a

temporary variable to swap the values of two variables.

Example:>>> #swapping variables in Python>>> x,y =1,2>>> x1>>> y2>>> x,y=y,x>>> x2>>> y1

Page 11: I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )

IDENTIFIERS Are the set of valid strings that are allowed

as name in a computer language. In here, there are keywords-names that

form a construct of the language It is a reserved words that may not be used

for any other purpose.Example : and, as, assert, break, class,

continue, def,del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, with, yield, none

Page 12: I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )

IDENTIFIERS-CONTINUE The rules for Python identifiers are as follow:

First character must be letter or underscore (_) Any additional character can be alphanumeric or

underscore Case sensitive :CASE != Case

Page 13: I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )

BUILT-INS Built-ins- python additional set of identifiers,

it is not a reserved words but it is not recommended to use since it is treated as “reserved for the system” It is a member of the _builtins_module