0% found this document useful (0 votes)
51 views6 pages

Python Programming. Kimberly

This document provides examples of using numeric data, lists, strings, tuples, arithmetic operators, bitwise operators, assignment operators, membership operators, identity operators, control flow statements (if/else, while, for), and exception handling (try/except) in Python. It demonstrates basic operations on various Python data types and common programming constructs.

Uploaded by

Regine Omboy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views6 pages

Python Programming. Kimberly

This document provides examples of using numeric data, lists, strings, tuples, arithmetic operators, bitwise operators, assignment operators, membership operators, identity operators, control flow statements (if/else, while, for), and exception handling (try/except) in Python. It demonstrates basic operations on various Python data types and common programming constructs.

Uploaded by

Regine Omboy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Numeric Data as Objects Lists

>>> import random


>>> some_var = 5 >>> alist = []
or >>> alist.append(4)
>>> some_var = int(5) >>> alist.append(55.89)
>>> alist.append('a short string')
>>> var_one = 5 >>> alist.append(random.random)
>>> var_two = 10
>>> var_one + var_two >>> alist[0]
15 4
>>> alist[1]
>>> type(some_var) 55.890000000000001
<type 'int'> >>> alist[2]
'a short string'
>>> float_var = 5.5 >>> alist[3]
>>> int_var = int(float_var) <built-in method random of Random
>>> print (int_var) object at 0x00A29D28>
5
>>> alist[3]()
>>> foo_hex = 0x2A7 0.87358651337544713
>>> print (foo_hex)
679 >>> alist[2]
'a short string'
>>> foo_hex = int("2A7",16) >>> alist[2] = 'a better string'
>>> print (astrfoo_hex) >>> alist[2]
679 'a better string
>>> int_var = 5 >>> list_name = []
>>> help(5) >>> list_name.append(0)
Help on int object: >>> list_name.append(1)
class int(object) >>> list_name
| int(x[, base]) -> integer [0, 1]
>>> var_one = list_name
>>> var_two = list_name

>>> var_one
[0, 1]
>>> var_two
[0, 1] >>> print astr1[0:4]
>>> list_name[0] = 9 This
>>> var_one
[9, 1] >>> print astr1[:4]
>>> var_two This
[9, 1]
>>> print astr1[4:]
is a short string.
>>> alist1 = [1,2,3,4,5]
>>> alist2 = [6,7,8,9,10] >>> print astr1[10:15]
>>> alist1 + alist2 short
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> str_cat = astr1 + " " + astr2
>>> alist2.index(8) >>> print str_cat
2 This is a short string. This is another
>>> alist1.reverse() short string.
>>> alist1
[5, 4, 3, 2, 1] >>> the_string = "This is the
string."
>>> slist = [8,22,0,5,16,99,14,- >>> the_string = the_string[0:4]
6,42,66] >>> the_string
>>> slist.sort() 'This'
>>> slist
[-6, 0, 5, 8, 14, 16, 22, 42, 66, 99] >>> print astr1.upper()
THIS IS A SHORT STRING.

String >>> print astr1.find('string')


16
>>> astr1 = 'This is a short string.'
>>> astr2 = "This is another short >>> print astr1.replace('string',
string." 'line')
>>> astr3 = "This string has This is a short line.
'embedded' single-quote chracters."
>>> astr4 = """This is an example >>> print astr1.rjust(30)
This is a short string.
... of a multi-line
... string. >>> print astr1.rjust(30,'.')
... """ .......This is a short string.
>>>
Tuples Arithmetic operators (floor
division)
>>> tuple2 = (1,2)
>>> tuple2 >>> 5/2
(1, 2) 2
>>> 5//2
>>> tuple4 = (9, 22.5, 0x16, 0) 2
>>> tuple4 >>> 5.0/2
(9, 22.5, 22, 0) 2.5
>>> tuple4[2] >>> 5.0//2
22 2.0
>>> tuple4[0]
9
Bitwise operators (Binary
>>> tuple2
(1, 2) Left or Right Shift)
>>> tuple4
(9, 22.5, 22, 0) >>> 2 << 1
>>> tuple6 = tuple2 + tuple4 4
>>> tuple6 >>> 2 << 2
(1, 2, 9, 22.5, 22, 0) 8
>>> 2 << 3
>>> tpl = (0, 0, 2, 2, 6, 0, 3, 2, 1, 0) 16
>>> tpl.count(0) >>> 16 >> 2
4 4
>>> tpl.count(2)
3 Assignment operators (add and
>>> tpl.count(6) assignment)
1
>>> a = 1
>>> a += 1
>>> a
2
Membership operators The if statement
if x in some_list: if <expression>:
DoSomething(x, some_list) statement
(more statements as necessary)
if x not in some_dict:
some_dict[x] = new_value if <expression>:
statement
(more statements as necessary)
Identity operators else:
statement
def GetFilePath(name): (and yet more statement if necessary)
global pathParse
if pathParse is None: if <expression>:
pathParse = FileUtil.PathParse() statement
file_path = pathParse(name) (more statements as necessary)
if len(file_path) > 1: elif <expression>:
return file_path statement
else: (more statements as necessary)
return None else:
statement
(and yet more statements if necessary)
>>> if some_var < 10:
print
... "Yes"
... print
"Indeed"
... else:
... print
... "Sorry"
... print
Yes "Nope"
Indeed
The while statements The for statement.
while <expression>: for some_var in <sequence>:
statement statement
(more statements as necessary) (more statements as necessary)
else: else:
statement statement
(and yet more statement if necessary) (and yet more statement if necessary)

>>> loop_ok = True >>> for i in range(0,5):


>>> loop_cnt = 10 ... print i
>>> while loop_ok: ...
0
print "%d Loop 1
... is OK" % 2
... loop_cnt 3
... loop_cnt -= 1 4
... if loop_cnt < 0:
loop_ok = False >>> alist = [1,2,3,4,5,6,7,8,9,10]
>>> for i in alist:
... else:
..
print "%d Loop . prin
... .. t i
no longer OK"
... .
% loop_cnt
10 Loop is OK 123456789
9 Loop is OK 10
8 Loop is OK >>> stuple = ("this","is","a","4-
7 Loop is OK tuple")
6 Loop is OK >>> for s in stuple:
5 Loop is OK ... print s
4 Loop is OK ...
3 Loop is OK this
2 Loop is OK is
1 Loop is OK a
0 Loop is OK 4-tuple
-1 Loop no longer OK
The try statement
try:
statement
(more statements as necessary)
except <exception, err_info>:
statement
(more statements as necessary)
else:
statement
(more statements as necessary)
finally:
statement
(and yet more statements if necessary)

try:
f = open(fname, "r")
except Exception, err:
print "File open failed: %s" % str(err)

You might also like