In [1]:
range(0, 9)
Out[1]:
In [2]:
for i in range(0, 9):
print(i)
In [4]:
list(range(0, 9))
Out[4]:
In [5]:
int('1')
Out[5]:
In [6]:
str(1)
Out[6]:
In [7]:
int('a')
In [8]:
list('abcsd')
Out[8]:
In [9]:
tuple('kathmandu')
Out[9]:
In [10]:
list(('255', '255', '255', '0'))
Out[10]:
In [11]:
tuple([255, 255, 254, 0])
Out[11]:
In [12]:
tuple({'abc': 123})
Out[12]:
In [13]:
dict(('name', 'hari'))
In [14]:
list({'abc': 123})
Out[14]:
In [15]:
for item in {'a': 1}:
print(item)
In [16]:
dict([('name', 'hari')])
Out[16]:
In [ ]:
In [19]:
lst = []
for i in range(0, 50):
if i % 5 == 0:
lst.append(i ** 2)
In [20]:
lst
Out[20]:
In [21]:
# list comprehension
[i**2 for i in range(0, 50) if i % 5 == 0]
Out[21]:
In [23]:
lst = [i**2 if i % 2 == 0 else i for i in range(0, 9)]
In [24]:
lst
Out[24]:
In [26]:
i = 1
i**2 if i % 2 == 0 else i
Out[26]:
In [27]:
i = 2
i**2 if i % 2 == 0 else i
Out[27]:
In [28]:
# set comprehension
{i for i in range(0, 19) if i % 2 == 0 or i % 3 == 0}
Out[28]:
In [29]:
# dictionary comprehension
{i:i**2 for i in range(0, 19) if i % 2 == 0 or i % 3 == 0}
Out[29]:
In [ ]:
In [30]:
fp = open('untitled.txt', 'r')
In [31]:
fp
Out[31]:
In [32]:
open('notafile.txt', 'r')
In [33]:
dir(fp)
Out[33]:
In [34]:
fp.read()
Out[34]:
In [35]:
fp.read()
Out[35]:
In [36]:
fp.seek(0)
Out[36]:
In [37]:
fp.read()
Out[37]:
In [38]:
fp.seek(0)
Out[38]:
In [39]:
fp.readline()
Out[39]:
In [40]:
line = fp.readline()
In [41]:
line
Out[41]:
In [42]:
line = line.strip()
In [43]:
line
Out[43]:
In [44]:
line = line.strip(',')
In [45]:
line
Out[45]:
In [46]:
items = line.split(',')
In [47]:
items
Out[47]:
In [48]:
import csv
In [67]:
fp.seek(0)
Out[67]:
In [50]:
headers = 'euname,modified,linked_country,iso3,iso2,grc,isonum,country,imperitive\n'
In [51]:
headers = headers.strip().split(',')
In [52]:
headers
Out[52]:
In [68]:
reader = csv.DictReader(fp)
In [69]:
reader
Out[69]:
In [64]:
dir(reader)
Out[64]:
In [70]:
reader.fieldnames
Out[70]:
In [71]:
countries = list(reader)
In [72]:
countries
Out[72]:
In [73]:
afg = None
for row in countries:
if row['iso3'] == 'AFG':
afg = row
In [74]:
afg
Out[74]:
In [76]:
csvfile = open('untitled.txt', 'r')
In [77]:
csvfile
Out[77]:
In [78]:
csvreader = csv.DictReader(csvfile)
In [79]:
country_list = []
for country in csvreader:
if country['iso3'] and country['country']:
country_tup = (country['iso3'], country['country'])
country_list.append(country_tup)
In [80]:
country_list
Out[80]:
In [81]:
fp.close()
In [82]:
csvfile.close()
In [ ]:
In [93]:
with open('newfile.csv', 'w') as csvfile:
fieldnames = ['FirstName', 'LastName', 'Age']
csvwriter = csv.DictWriter(csvfile, fieldnames=fieldnames)
csvwriter.writeheader()
csvwriter.writerow({'FirstName': 'J', 'LastName': 'k', 'Age': 20})
csvwriter.writerow({'LastName': 'M', 'FirstName': 'n', 'Age': 24})
csvwriter.writerows([
{'LastName': 'A', 'FirstName': '', 'Age': 24},
{'LastName': 'B', 'FirstName': 'L', 'Age': 25},
{'LastName': 'B', 'FirstName': 'H', 'Age': 27}
])
In [ ]:
In [ ]:
Dogs
|
-------------------
| |
Domestic Wild
------------------
| |
Semi Wild Feral ...
Transport
|
-----------------------------------
| | |
Land Water Air | -------------- ........... | | Buses Train ...
In [98]:
class Animal:
age = None
In [109]:
class Dog(Animal):
pass
In [100]:
german_shepherd = Dog()
In [101]:
german_shepherd
Out[101]:
In [102]:
german_shepherd = Dog()
In [103]:
german_shepherd
Out[103]:
In [106]:
german_shepherd.age = 22
In [107]:
german_shepherd.age
Out[107]:
In [110]:
class Dog(Animal):
age = 18
In [111]:
german_shepherd = Dog()
In [113]:
german_shepherd.age
Out[113]:
In [114]:
german_shepherd.age = 21
In [116]:
german_shepherd.age
Out[116]:
In [117]:
tibetan_husky = Dog()
In [118]:
tibetan_husky.age
Out[118]:
In [124]:
class Animal:
age = None
def is_alive(self):
return True
def sound(self):
return 'Cry'
In [125]:
class Cat(Animal):
def sound(self):
return 'Meow'
In [126]:
cat = Cat()
In [127]:
cat.is_alive()
Out[127]:
In [128]:
cat.age
In [129]:
cat.sound()
Out[129]:
In [164]:
class Patron:
firstname = None
lastname = None
address = None
books = []
def __repr__(self):
return '{} {}'.format(self.firstname, self.lastname)
def has_book(self, bookid):
return bookid in self.books
In [165]:
class Student(Patron):
faculty = None
year = None
def __init__(self, firstname, lastname, faculty, year):
self.firstname = firstname
self.lastname = lastname
self.faculty = faculty
self.year = year
In [166]:
class Staff(Patron):
department = None
In [167]:
class Book:
bookid = None
isbn = None
name = None
def __init__(self, isbn, name):
self.isbn = isbn
self.name = name
self.bookid = 'MYLIB' + ':' + self.isbn
def __repr__(self):
return self.bookid
In [168]:
class Teacher(Patron):
faculty = None
def __init__(self, firstname, lastname, faculty):
self.firstname = firstname
self.lastname = lastname
self.faculty = faculty
In [ ]:
In [169]:
english = Book(isbn='abc-8236482374', name='Learning Engligh in 21 days.')
In [170]:
math = Book(isbn='mth-283482347', name='Quick Calculus.')
In [171]:
nepali = Book(isbn='npl-293492834', name='बकमपबसिकमपवबसिकमप.')
In [172]:
class Shelve:
books = []
lended_books = []
def is_book_available(self, bookid):
return bookid in self.books
def is_book_lendable(self, bookid):
return bookid not in self.lended_books
In [173]:
my_library = Shelve()
In [152]:
# Shelve.is_book_available(my_library, 'abc-123')
Out[152]:
In [174]:
my_library.is_book_available('abc-123')
Out[174]:
In [175]:
my_library.books = [english, math, nepali]
In [176]:
my_library.books
Out[176]:
In [ ]:
ram
In [177]:
ram = Student('Ram', 'Thapa', 'Biology', '2nd')
In [178]:
ram
Out[178]:
In [179]:
ram.has_book('ajsdajsd')
Out[179]:
In [ ]:
In [ ]: