Chapter 2. LISTS & TUPLES.SEQUENCES 'Beginning Python From Novice to Professional'

>>> ed = ['Edward Snowden',42]
>>> steve = ['Steven Jobs',55]
>>> gven = ['Gven Stephany',35]
>>> barak = ['Barak Obama',56]
>>> database = [ed,steve,gven,barak]
>>> database
[['Edward Snowden', 42], ['Steven Jobs', 55], ['Gven Stephany', 35], ['Barak Obama', 56]]
>>>
_________________________________________________

Common Sequence Operations
These operations include indexing,
slicing, adding, multiplying, and checking for membership.

INDEXING

>>>print 'losos'[4]
s
>>> 'losos'[-1]
's'

>>> third = raw_input('Who? ')[3]
Who? Kozel
>>> third
'e'
____________________________________________

>>> endings = ['st', 'nd', 'rd'] + 17 * ['th'] \
+ ['st', 'nd', 'rd'] + 7 * ['th'] \
+ ['st']
>>> year = raw_input('Year: ')
Year: 2010
>>> month = raw_input('Month (1-12): ')
Month (1-12): 3
>>> day = raw_input('Day (1-31): ')
Day (1-31): 2
>>> month_number = int(month)
>>> day_number = int(day)
>>> month_name = months[month_number-1]
>>> ordinal = day + endings[day_number-1]
>>> print month_name + ' ' + ordinal + ', ' + year
March 2nd, 2010
______________________________________________
SLICING
first is inclusive and the
second is exclusive.

>>> tag = ''
>>> tag[8:12]
'fjfj'
>>> tag[8:13]
'fjfj"'
>>> tag[15:-3]
'k'
>>> tag[15:-6]
' '
>>> tag[15:-1]
'kfj'
>>> tag[15:-1]
'kfj'
>>> numbers = [1,2,3,4,5,6,7,8,9]
>>> numbers [2:6]
[3, 4, 5, 6]
>>> numbers [0:1]
[1]

>>> numbers [8:9]
[9]
>>> numbers [8:10] #reaching the last element
[9]
>>> numbers [-3:-1]
[7, 8]
>>> numbers [-3:0]
[]
>>> numbers [-3:] #reaching the last element
[7, 8, 9]

>>> numbers [:5]
[1, 2, 3, 4, 5]
>>> numbers [:]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
_____________________________________
>>> url = raw_input('Enter the url: ')
Enter the url: www.python.org
>>> domain = url[4:10]
>>> domain
'python'
>>> print domain
python
>>> print 'Domain name: '+ domain
Domain name: python

As a programm

url = raw_input ('Enter the url:')
domain = url[4:10]
print 'domain name: '+ domain

_____________________________________

>>> numbers = [1,2,3,4,5,6,7,8,9,10]
>>> numbers [0:10:1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers [0:10:2]
[1, 3, 5, 7, 9]
>>> numbers [::3]
[1, 4, 7, 10]
>>> numbers [:5]
[1, 2, 3, 4, 5]
>>> numbers [:5:-2]
[10, 8]
_____________________________________
Adding of sequences

[1,2,3] + [4,5,6]
[1, 2, 3, 4, 5, 6]
___________________________________________
Multiplication of sequences

>>> sequence
[None, None, None, None, None, None, None, None, None, None]
_____________________________________________________

MEMBERSHIP

>>> 'w'in permissions
True
>>> m in permissions
False
__________________
>>> subject = 'somebody'
>>> 'bo' in subject
True
___________________
>>> friends = ['ed','steve','gven']
>>> raw_input ('Enter your friend: ') in friends
Enter your friend: ed
True
_____________________________
SPAMFILTER

>>> subject = 'Superporno'
>>> 'porno' in subject
True
______________________________
Program

database = [
    ['albert', '1234'],
['dilbert', '4242'],
['smith', '7524'],
['jones', '9843']
]

username = raw_input('User name: ')
pin = raw_input('PIN code: ')
if [username, pin] in database: print 'Access granted'
____________________________________________

Length, Minimum, and Maximum

>>> numbers = (100,23,435)
>>> max (numbers)
435
>>> min (numbers)
23
>>> len (numbers)
3
>>> min (1,3,4)
1
>>> max (1,3,4)
4

Popular posts from this blog