<jupyter_start><jupyter_text>Session 3---<jupyter_code>range(0, 9)
for i in range(0, 9):
    print(i)
list(range(0, 9))
int('1')
str(1)
int('a')
list('abcsd')
tuple('kathmandu')
list(('255', '255', '255', '0'))
tuple([255, 255, 254, 0])
tuple({'abc': 123})
dict(('name', 'hari'))
list({'abc': 123})
for item in {'a': 1}:
    print(item)
dict([('name', 'hari')])
lst = []
for i in range(0, 50):
    if i % 5 == 0:
        lst.append(i ** 2)
lst
# list comprehension
[i**2 for i in range(0, 50) if i % 5 == 0]
lst = [i**2 if i % 2 == 0 else i for i in range(0, 9)]
lst
i = 1
i**2 if i % 2 == 0 else i
i = 2
i**2 if i % 2 == 0 else i
# set comprehension
{i for i in range(0, 19) if i % 2 == 0 or i % 3 == 0}
# dictionary comprehension
{i:i**2 for i in range(0, 19) if i % 2 == 0 or i % 3 == 0}
fp = open('untitled.txt', 'r')
fp
open('notafile.txt', 'r')
dir(fp)
fp.read()
fp.read()
fp.seek(0)
fp.read()
fp.seek(0)
fp.readline()
line = fp.readline()
line
line = line.strip()
line
line = line.strip(',')
line
items = line.split(',')
items
import csv
fp.seek(0)
headers = 'euname,modified,linked_country,iso3,iso2,grc,isonum,country,imperitive\n'
headers = headers.strip().split(',')
headers
reader = csv.DictReader(fp)
reader
dir(reader)
reader.fieldnames
countries = list(reader)
countries
afg = None
for row in countries:
    if row['iso3'] == 'AFG':
        afg = row
afg<jupyter_output><empty_output><jupyter_text>final output should be    [('AFG', 'Afganistan'), ('NPL', 'Nepal') ...]<jupyter_code>csvfile = open('untitled.txt', 'r')
csvfile
csvreader = csv.DictReader(csvfile)
country_list = []
for country in csvreader:
    if country['iso3'] and country['country']:
        country_tup = (country['iso3'], country['country'])
        country_list.append(country_tup)
country_list
fp.close()
csvfile.close()
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}
    ])<jupyter_output><empty_output><jupyter_text>Classes and Objects---                                Dogs                                 |                        -------------------                        |                 |                      Domestic           Wild             ------------------             |                |           Semi Wild         Feral ...                     Transport                   |    -----------------------------------    |                |              |   Land             Water          Air    |--------------        ...........|        |Buses   Train ...<jupyter_code>class Animal:
    age = None
class Dog(Animal):
    pass
german_shepherd = Dog()
german_shepherd
german_shepherd = Dog()
german_shepherd
german_shepherd.age = 22
german_shepherd.age
class Dog(Animal):
    age = 18
german_shepherd = Dog()
german_shepherd.age
german_shepherd.age = 21
german_shepherd.age
tibetan_husky = Dog()
tibetan_husky.age
class Animal:
    age = None
    
    def is_alive(self):
        return True
    
    def sound(self):
        return 'Cry'
class Cat(Animal):
    
    def sound(self):
        return 'Meow'
cat = Cat()
cat.is_alive()
cat.age
cat.sound()<jupyter_output><empty_output><jupyter_text>CaseStudy: Library Management---<jupyter_code>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
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
class Staff(Patron):
    department = None
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
class Teacher(Patron):
    faculty = None
    
    def __init__(self, firstname, lastname, faculty):
        self.firstname = firstname
        self.lastname = lastname
        self.faculty = faculty
english = Book(isbn='abc-8236482374', name='Learning Engligh in 21 days.')
math = Book(isbn='mth-283482347', name='Quick Calculus.')
nepali = Book(isbn='npl-293492834', name='बकमपबसिकमपवबसिकमप.')
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
my_library = Shelve()
# Shelve.is_book_available(my_library, 'abc-123')
my_library.is_book_available('abc-123')
my_library.books = [english, math, nepali]
my_library.books
ram 
ram = Student('Ram', 'Thapa', 'Biology', '2nd')
ram
ram.has_book('ajsdajsd')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1652908067542214, 'fraction_numerical': 0.025703564727954972, 'mean_word_length': 2.537491705374917, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '125109', 'license': 'Unlicense', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1846, 'n_words': 454, 'path': 'session3.ipynb', 'repo_name': 'lfapython/git_python_session', 'size': '5330'}