3. Python Essentials#
3.1. Overview#
നമ്മൾ examples-ൽ focus ചെയ്തുകൊണ്ട് ഒരുപാട് material വളരെ വേഗത്തിൽ cover ചെയ്തു കഴിഞ്ഞു.
ഇനി, Python-ന്റെ ചില core features കുറച്ചുകൂടി systematic ആയ രീതിയിൽ നമുക്ക് cover ചെയ്യാം.
ഈ approach അത്ര exciting അല്ലെങ്കിലും ചില details വ്യക്തമാക്കാൻ ഇത് സഹായിക്കുന്നു.
3.2. Data Types#
Computer programs സാധാരണയായി പല data types-നെ track ചെയ്യാറുണ്ട്.
For example, 1.5 എന്നത് ഒരു floating point number ആണ്, 1 എന്നത് ഒരു integer ആണ്.
പല കാരണങ്ങൾ കൊണ്ട് programs-ന് ഈ രണ്ട് types-നെ distinguish ചെയ്യേണ്ടി വരുന്നു.
ഒന്ന്, ഇവ memory-യിൽ different ആയ രീതിയിലാണ് store ചെയ്യപ്പെടുന്നത്.
മറ്റൊന്ന്, arithmetic operations വ്യത്യസ്തമാണ് എന്നതാണ്:
For example, മിക്ക machines-ലും floating point arithmetic ഒരു specialized Floating Point Unit (FPU) ഉപയോഗിച്ചാണ് implement ചെയ്തിരിക്കുന്നത്.
പൊതുവേ, floats കൂടുതൽ informative ആണ്, പക്ഷേ integers-ലെ arithmetic operations വേഗത കൂടിയതും accurate ആയതും ആണ്.
നമ്മൾ already കണ്ടുകഴിഞ്ഞ strings, lists തുടങ്ങിയവ ഉൾപ്പെടെ, Python വേറെയും അനേകം built-in data types നൽകുന്നു.
strings, lists, etc.
ഇവയെക്കുറിച്ച് കുറച്ചുകൂടി നമുക്ക് നോക്കാം.
3.2.1. Primitive Data Types#
3.2.1.1. Boolean Values#
ഒരു simple data type ആണ് Boolean values, ഇവ True അല്ലെങ്കിൽ False ആകാം:
x = True
x
True
type() എന്ന function ഉപയോഗിച്ച് memory-യിലെ ഏത് object-ന്റെയും type നമുക്ക് check ചെയ്യാം.
type(x)
bool
താഴെയുള്ള line of code-ൽ, interpreter = ചിഹ്നത്തിന്റെ വലതുവശത്തുള്ള expression evaluate ചെയ്ത്, ആ value-യിലേക്ക് y-യെ bind ചെയ്യുന്നു:
y = 100 < 10
y
False
type(y)
bool
Arithmetic expressions-ൽ, True എന്നത് 1 ആയും False എന്നത് 0 ആയും convert ചെയ്യപ്പെടുന്നു.
ഇതിനെ Boolean arithmetic എന്ന് വിളിക്കുന്നു. Programming-ൽ ഇത് പലപ്പോഴും useful ആണ്.
ചില examples താഴെ കാണാം:
x + y
1
x * y
0
True + True
2
bools = [True, True, False, True] # List of Boolean values
sum(bools)
3
3.2.1.2. Numeric Types#
Numeric types-ഉം important ആയ primitive data types ആണ്.
integer, float എന്നീ types നമ്മൾ മുമ്പ് കണ്ടിട്ടുണ്ട്.
Complex numbers എന്നത് Python-ലെ മറ്റൊരു primitive data type ആണ്:
x = complex(1, 2)
y = complex(2, 1)
print(x * y)
type(x)
5j
complex
3.2.2. Containers#
(ഒരുപക്ഷേ heterogeneous ആയ) data-യുടെ collections store ചെയ്യാൻ Python-ൽ പല basic types ഉണ്ട്.
നമ്മൾ lists-നെക്കുറിച്ച് already discuss ചെയ്തു കഴിഞ്ഞു.
ഇതുമായി ബന്ധപ്പെട്ട ഒരു data type ആണ് tuples, ഇവ "immutable" ആയ lists ആണ്:
x = ('a', 'b') # Parentheses instead of the square brackets
x = 'a', 'b' # Or no brackets --- the meaning is identical
x
('a', 'b')
type(x)
tuple
Python-ൽ, ഒരു object ഒരിക്കൽ create ചെയ്ത ശേഷം മാറ്റാൻ പറ്റാത്തതാണെങ്കിൽ, അതിനെ immutable എന്ന് വിളിക്കുന്നു.
നേരെ മറിച്ച്, create ചെയ്ത ശേഷവും മാറ്റാൻ പറ്റുന്ന object-നെ mutable എന്ന് വിളിക്കുന്നു.
Python lists mutable ആണ്:
x = [1, 2]
x[0] = 10
x
[10, 2]
പക്ഷേ tuples അങ്ങനെയല്ല:
x = (1, 2)
x[0] = 10
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[13], line 2
1 x = (1, 2)
----> 2 x[0] = 10
TypeError: 'tuple' object does not support item assignment
Mutable, immutable data-യുടെ role-നെക്കുറിച്ച് കൂടുതൽ കാര്യങ്ങൾ നമുക്ക് പിന്നീട് കാണാം.
Tuples-നെയും, (lists-നെയും) താഴെ കാണുന്ന രീതിയിൽ "unpack" ചെയ്യാം:
integers = (10, 20, 30)
x, y, z = integers
x
10
y
20
ഇതിന്റെ ഒരു example നിങ്ങൾ already കണ്ടുകഴിഞ്ഞു.
Tuple unpacking convenient ആണ്. നമുക്ക് ഇത് പലപ്പോഴും ഉപയോഗിക്കേണ്ടി വരും.
3.2.2.1. Slice Notation#
ഒരു sequence-ന്റെ (list, tuple അല്ലെങ്കിൽ string) ഒന്നിലധികം elements access ചെയ്യാൻ, Python-ന്റെ slice notation ഉപയോഗിക്കാം.
For example,
a = ["a", "b", "c", "d", "e"]
a[1:]
['b', 'c', 'd', 'e']
a[1:3]
['b', 'c']
a[m:n] എന്നത് a[m]-ൽ തുടങ്ങി n - m elements return ചെയ്യുന്നു എന്നതാണ് പൊതുവായ rule.
Negative numbers-ഉം ഉപയോഗിക്കാം:
a[-2:] # Last two elements of the list
['d', 'e']
Step specify ചെയ്യാൻ [start:end:step] എന്ന format-ഉം ഉപയോഗിക്കാം:
a[::2]
['a', 'c', 'e']
ഒരു negative step ഉപയോഗിച്ചാൽ, sequence-നെ reverse ആയ order-ൽ return ചെയ്യാം:
a[-2::-1] # Walk backwards from the second last element to the first element
['d', 'c', 'b', 'a']
ഇതേ slice notation tuples-ലും, strings-ലും work ചെയ്യുന്നു:
s = 'foobar'
s[-3:] # Select the last three elements
'bar'
3.2.2.2. Sets and Dictionaries#
മുന്നോട്ട് പോകുന്നതിന് മുമ്പ് പറയേണ്ട മറ്റ് രണ്ട് container types ആണ് sets, dictionaries എന്നിവ.
Dictionaries, lists-നെ പോലെ തന്നെയാണ്, പക്ഷേ items-ന് number-ന് പകരം name ഉണ്ടാകും:
d = {'name': 'Frodo', 'age': 33}
type(d)
dict
d['age']
33
'name', 'age' എന്നീ names-നെ keys എന്ന് വിളിക്കുന്നു.
Keys ഏതൊക്കെ objects-ലേക്കാണോ map ചെയ്യപ്പെടുന്നത് ('Frodo', 33) അവയെ values എന്ന് വിളിക്കുന്നു.
Sets എന്നത് duplicates ഇല്ലാത്ത unordered collections ആണ്. സാധാരണ set-theoretic operations set methods നൽകുന്നു:
s1 = {'a', 'b'}
type(s1)
set
s2 = {'b', 'c'}
s1.issubset(s2)
False
s1.intersection(s2)
{'b'}
set() function sequences-ൽ നിന്നും sets create ചെയ്യുന്നു:
s3 = set(('foo', 'bar', 'foo'))
s3
{'bar', 'foo'}
3.3. Input and Output#
Text files-ലേക്ക് read, write ചെയ്യുന്നതിനെക്കുറിച്ച് ചുരുക്കമായി നമുക്ക് നോക്കാം. ആദ്യം writing-ൽ നിന്നും തുടങ്ങാം:
f = open('newfile.txt', 'w') # Open 'newfile.txt' for writing
f.write('Testing\n') # Here '\n' means new line
f.write('Testing again')
f.close()
ഇവിടെ:
Built-in function ആയ
open(), write ചെയ്യാനുള്ള ഒരു file object create ചെയ്യുന്നു.write(),close()എന്നിവ രണ്ടും file objects-ന്റെ methods ആണ്.
നമ്മൾ create ചെയ്ത ഈ file എവിടെയാണ്?
Python-ന് present working directory (pwd) എന്ന ഒരു concept ഉണ്ടെന്ന് ഓർക്കുക, ഇത് Jupyter-ൽ നിന്നോ IPython-ൽ നിന്നോ താഴെ കൊടുത്തിരിക്കുന്ന രീതിയിൽ കണ്ടുപിടിക്കാം:
%pwd
'/home/runner/work/lecture-python-programming.ml/lecture-python-programming.ml/lectures'
ഒരു path specify ചെയ്തിട്ടില്ലെങ്കിൽ, ഇവിടേക്കാണ് Python write ചെയ്യുന്നത്.
newline.txt-ന്റെ content read ചെയ്യാനും നമുക്ക് Python താഴെ കൊടുത്തിരിക്കുന്ന രീതിയിൽ ഉപയോഗിക്കാം:
f = open('newfile.txt', 'r')
out = f.read()
out
'Testing\nTesting again'
print(out)
Testing
Testing again
In fact, files properly acquire ചെയ്യപ്പെടുകയും release ചെയ്യപ്പെടുകയും ചെയ്യുന്നുവെന്ന് ഉറപ്പാക്കാൻ modern Python-ൽ recommend ചെയ്യുന്ന approach ഒരു with statement ഉപയോഗിക്കുക എന്നതാണ്.
Operations-നെ ഒരേ block-ൽ ഉൾപ്പെടുത്തുന്നത് നിങ്ങളുടെ code-ന്റെ clarity-യും improve ചെയ്യുന്നു.
Note
ഇത്തരത്തിലുള്ള block-നെ formal ആയി context എന്ന് വിളിക്കുന്നു.
മുകളിൽ കൊടുത്ത രണ്ട് examples-നെയും ഒരു with statement ആയി convert ചെയ്യാൻ നമുക്ക് നോക്കാം.
ആദ്യം writing example മാറ്റാം:
with open('newfile.txt', 'w') as f:
f.write('Testing\n')
f.write('Testing again')
ശ്രദ്ധിക്കുക, close() method call ചെയ്യേണ്ട ആവശ്യമില്ല. കാരണം block-ന്റെ അവസാനം stream close ആകുന്നുവെന്ന് with block ഉറപ്പാക്കും.
ചെറിയ modifications-ഓടെ, with ഉപയോഗിച്ച് files-നെ read ചെയ്യാനും കഴിയും:
with open('newfile.txt', 'r') as fo:
out = fo.read()
print(out)
Testing
Testing again
ഇനി, ഒരു file-ൽ നിന്നും input read ചെയ്ത് മറ്റൊരു file-ലേക്ക് output write ചെയ്യണം എന്ന് കരുതുക.
with statements ഉപയോഗിച്ച്, operating system-ലേക്ക് resources ശരിയായി acquire ചെയ്തും return ചെയ്തും ഈ task എങ്ങനെ ചെയ്യാം എന്ന് താഴെ കാണാം:
with open("newfile.txt", "r") as f:
file = f.readlines()
with open("output.txt", "w") as fo:
for i, line in enumerate(file):
fo.write(f'Line {i}: {line} \n')
Output file ഇതായിരിക്കും:
with open('output.txt', 'r') as fo:
print(fo.read())
Line 0: Testing
Line 1: Testing again
രണ്ട് with statements-നെയും ഒറ്റ line-ൽ group ചെയ്ത് മുകളിലെ example-നെ നമുക്ക് simplify ചെയ്യാം:
with open("newfile.txt", "r") as f, open("output2.txt", "w") as fo:
for i, line in enumerate(f):
fo.write(f'Line {i}: {line} \n')
Output file ഇതേപോലെ തന്നെ ആയിരിക്കും:
with open('output2.txt', 'r') as fo:
print(fo.read())
Line 0: Testing
Line 1: Testing again
Existing file overwrite ചെയ്യാതെ അതിലേക്ക് write ചെയ്യുന്നത് തുടരണം എന്ന് കരുതുക.
Append mode-നെ സൂചിപ്പിക്കുന്ന a എന്ന mode-ലേക്ക് നമുക്ക് മാറാം:
with open('output2.txt', 'a') as fo:
fo.write('\nThis is the end of the file')
with open('output2.txt', 'r') as fo:
print(fo.read())
Line 0: Testing
Line 1: Testing again
This is the end of the file
Note
ശ്രദ്ധിക്കുക, ഏറ്റവും സാധാരണയായി ഉപയോഗിക്കുന്ന r, w, a എന്നീ modes മാത്രമാണ് ഇവിടെ നമ്മൾ cover ചെയ്തത്.
നിങ്ങൾക്ക് experiment ചെയ്യാനായി Python പല modes നൽകുന്നു.
3.3.1. Paths#
ശ്രദ്ധിക്കുക, newfile.txt present working directory-യിൽ ഇല്ലെങ്കിൽ open()-ലേക്കുള്ള ഈ call fail ആകും.
ഈ അവസ്ഥയിൽ, file-നെ pwd-യിലേക്ക് മാറ്റുകയോ ആ file-ലേക്കുള്ള full path specify ചെയ്യുകയോ ചെയ്യാം
f = open('insert_full_path_to_file/newfile.txt', 'r')
3.4. Iterating#
Computing-ലെ ഏറ്റവും important ആയ tasks-ൽ ഒന്നാണ് data-യുടെ ഒരു sequence-ലൂടെ step ചെയ്തു പോയി, തന്നിരിക്കുന്ന ഒരു action perform ചെയ്യുക എന്നത്.
for loop വഴി ഇത്തരം iteration-ന് simple-ഉം, flexible-ഉം ആയ ഒരു interface നൽകുന്നു എന്നതാണ് Python-ന്റെ ഒരു strength.
3.4.1. Looping over Different Objects#
പല Python objects-ഉം "iterable" ആണ്, അതായത് അവയ്ക്ക് മീതെ loop ചെയ്യാൻ കഴിയും.
ഒരു example നൽകാൻ, US cities-ഉം അവയുടെ population-ഉം list ചെയ്യുന്ന us_cities.txt എന്ന file present working directory-യിലേക്ക് നമുക്ക് write ചെയ്യാം.
%%writefile us_cities.txt
new york: 8244910
los angeles: 3819702
chicago: 2707120
houston: 2145146
philadelphia: 1536471
phoenix: 1469471
san antonio: 1359758
san diego: 1326179
dallas: 1223229
Writing us_cities.txt
ഇവിടെ %%writefile എന്നത് ഒരു IPython cell magic ആണ്.
Names-നെ capitalize ചെയ്തും thousands-നെ സൂചിപ്പിക്കാൻ commas ചേർത്തും ഈ information-നെ കൂടുതൽ readable ആക്കണം എന്ന് കരുതുക.
താഴെയുള്ള program data read ചെയ്ത്, ആ conversion ചെയ്യുന്നു:
data_file = open('us_cities.txt', 'r')
for line in data_file:
city, population = line.split(':') # Tuple unpacking
city = city.title() # Capitalize city names
population = f'{int(population):,}' # Add commas to numbers
print(city.ljust(15) + population)
data_file.close()
New York 8,244,910
Los Angeles 3,819,702
Chicago 2,707,120
Houston 2,145,146
Philadelphia 1,536,471
Phoenix 1,469,471
San Antonio 1,359,758
San Diego 1,326,179
Dallas 1,223,229
ഇവിടെ f' എന്നത് strings-ലേക്ക് variables insert ചെയ്യാൻ ഉപയോഗിക്കുന്ന ഒരു f-string ആണ്.
ഓരോ line-ന്റെയും reformatting മൂന്ന് different string methods-ന്റെ result ആണ്. അവയുടെ details പിന്നീട് നോക്കാം.
ഈ program-ലെ നമുക്ക് interesting ആയ ഭാഗം line 2 ആണ്. അത് കാണിക്കുന്നത്:
File object ആയ
data_fileiterable ആണ്, അതായത് ഇതിനെ ഒരുforloop-ലെin-ന്റെ വലതുവശത്ത് വെക്കാം.Iteration file-ലെ ഓരോ line-ഇലൂടെയും step ചെയ്യുന്നു.
ഇത് നമ്മുടെ program-ൽ കാണുന്ന clean-ഉം, convenient-ഉം ആയ syntax-ലേക്ക് നയിക്കുന്നു.
വേറെയും പല തരം objects iterable ആണ്. അവയിൽ ചിലതിനെക്കുറിച്ച് നമുക്ക് പിന്നീട് നോക്കാം.
3.4.2. Looping without Indices#
Explicit ആയ indexing ഇല്ലാതെ loop ചെയ്യുന്നതിനെ Python prefer ചെയ്യുന്നു എന്നതാണ് നിങ്ങൾ ശ്രദ്ധിച്ചിട്ടുണ്ടാകാവുന്ന ഒരു കാര്യം.
For example,
x_values = [1, 2, 3] # Some iterable x
for x in x_values:
print(x * x)
1
4
9
ഇതിനെയാണ്:
for i in range(len(x_values)):
print(x_values[i] * x_values[i])
1
4
9
ഇതിനേക്കാൾ prefer ചെയ്യുന്നത്.
ഈ രണ്ട് alternatives-നെ compare ചെയ്യുമ്പോൾ, ആദ്യത്തേത് എന്തുകൊണ്ട് prefer ചെയ്യുന്നു എന്ന് നിങ്ങൾക്ക് മനസ്സിലാക്കാം.
Indices ഇല്ലാതെ loop ചെയ്യുന്നത് simplify ചെയ്യാൻ Python ചില facilities നൽകുന്നു.
അതിലൊന്നാണ് zip(), രണ്ട് sequences-ൽ നിന്നുള്ള pairs-ഇലൂടെ step ചെയ്യാൻ ഇത് ഉപയോഗിക്കുന്നു.
For example, താഴെയുള്ള code run ചെയ്ത് നോക്കുക:
countries = ('Japan', 'Korea', 'China')
cities = ('Tokyo', 'Seoul', 'Beijing')
for country, city in zip(countries, cities):
print(f'The capital of {country} is {city}')
The capital of Japan is Tokyo
The capital of Korea is Seoul
The capital of China is Beijing
Dictionaries create ചെയ്യാനും zip() function useful ആണ് --- for
example
names = ['Tom', 'John']
marks = ['E', 'F']
dict(zip(names, marks))
{'Tom': 'E', 'John': 'F'}
ഒരു list-ൽ നിന്നും index വേണമെങ്കിൽ, ഒരു option enumerate() ഉപയോഗിക്കുക എന്നതാണ്.
enumerate() എന്താണ് ചെയ്യുന്നതെന്ന് മനസ്സിലാക്കാൻ, താഴെയുള്ള example നോക്കാം:
letter_list = ['a', 'b', 'c']
for index, letter in enumerate(letter_list):
print(f"letter_list[{index}] = '{letter}'")
letter_list[0] = 'a'
letter_list[1] = 'b'
letter_list[2] = 'c'
3.4.3. List Comprehensions#
List comprehension എന്ന ഒന്ന് ഉപയോഗിച്ച്, random draws-ന്റെ list generate ചെയ്യാനുള്ള code-നെ നമുക്ക് ഗണ്യമായി simplify ചെയ്യാം.
Lists create ചെയ്യാനുള്ള ഒരു elegant Python tool ആണ് List comprehensions.
താഴെയുള്ള example നോക്കാം. രണ്ടാമത്തെ line-ന്റെ വലതുവശത്താണ് list comprehension ഉള്ളത്:
animals = ['dog', 'cat', 'bird']
plurals = [animal + 's' for animal in animals]
plurals
['dogs', 'cats', 'birds']
മറ്റൊരു example താഴെ കാണാം:
range(8)
range(0, 8)
doubles = [2 * x for x in range(8)]
doubles
[0, 2, 4, 6, 8, 10, 12, 14]
3.5. Comparisons and Logical Operators#
3.5.1. Comparisons#
പല expressions-ഉം Boolean values-ൽ ഒന്നായി (അതായത് True അല്ലെങ്കിൽ False) evaluate ആകുന്നു.
ഒരു common type ആണ് comparisons, ഇതുപോലെ:
x, y = 1, 2
x < y
True
x > y
False
Python-ന്റെ ഒരു നല്ല feature, നമുക്ക് inequalities-നെ chain ചെയ്യാം എന്നതാണ്:
1 < 2 < 3
True
1 <= 2 <= 3
True
നമ്മൾ മുമ്പ് കണ്ടതുപോലെ, equality test ചെയ്യുമ്പോൾ == ഉപയോഗിക്കുന്നു:
x = 1 # Assignment
x == 2 # Comparison
False
"Not equal"-ന് != ഉപയോഗിക്കുക:
1 != 2
True
ശ്രദ്ധിക്കുക, conditions test ചെയ്യുമ്പോൾ, valid ആയ ഏത് Python expression-ഉം നമുക്ക് ഉപയോഗിക്കാം:
x = 'yes' if 42 else 'no'
x
'yes'
x = 'yes' if [] else 'no'
x
'no'
ഇവിടെ എന്താണ് നടക്കുന്നത്?
Rule ഇതാണ്:
Zero-യിലേക്കോ empty sequences അല്ലെങ്കിൽ containers-ലേക്കോ (strings, lists, etc.) evaluate ആകുന്ന expressions-ഉം,
None-ഉം എല്ലാംFalse-ന് equivalent ആണ്.for example, ഒരു
ifclause-ൽ[]-ഉം,()-ഉംFalse-ന് equivalent ആണ്
മറ്റ് എല്ലാ values-ഉം
True-ന് equivalent ആണ്.for example, ഒരു
ifclause-ൽ42എന്നത്True-ന് equivalent ആണ്
3.5.2. Combining Expressions#
and, or, not എന്നിവ ഉപയോഗിച്ച് expressions-നെ നമുക്ക് combine ചെയ്യാം.
ഇവ standard logical connectives ആണ് (conjunction, disjunction, denial):
1 < 2 and 'f' in 'foo'
True
1 < 2 and 'g' in 'foo'
False
1 < 2 or 'g' in 'foo'
True
not True
False
not not True
True
ഓർക്കുക:
രണ്ടും
Trueആണെങ്കിൽP and Q,Trueആണ്, അല്ലെങ്കിൽFalseരണ്ടും
Falseആണെങ്കിൽP or Q,Falseആണ്, അല്ലെങ്കിൽTrue
Expressions-ന്റെ ഒരു sequence test ചെയ്യാൻ all(), any() എന്നിവയും നമുക്ക് ഉപയോഗിക്കാം:
all([1 <= 2 <= 3, 5 <= 6 <= 7])
True
all([1 <= 2 <= 3, "a" in "letter"])
False
any([1 <= 2 <= 3, "a" in "letter"])
True
Note
Sequence-ലെ എല്ലാ boolean values/expressions-ഉം
Trueആകുമ്പോൾall()Truereturn ചെയ്യുന്നുSequence-ലെ ഏതെങ്കിലും boolean values/expressions
Trueആകുമ്പോൾany()Truereturn ചെയ്യുന്നു
3.6. Coding Style and Documentation#
ഒരു consistent ആയ coding style-ഉം documentation-ന്റെ ഉപയോഗവും, code-നെ മനസ്സിലാക്കാനും maintain ചെയ്യാനും എളുപ്പമാക്കുന്നു.
3.6.1. Python Style Guidelines: PEP8#
Prompt-ൽ import this എന്ന് type ചെയ്താൽ Python programming philosophy നിങ്ങൾക്ക് കണ്ടുപിടിക്കാം.
മറ്റ് പല കാര്യങ്ങളുടെയും കൂട്ടത്തിൽ, Python, programming style-ൽ consistency-യെ ശക്തമായി prefer ചെയ്യുന്നു.
Consistency-യെയും ചെറിയ minds-നെയും കുറിച്ചുള്ള ആ പഴഞ്ചൊല്ല് നമ്മൾ എല്ലാവരും കേട്ടിട്ടുണ്ട്.
Programming-ൽ, mathematics-ലെ പോലെ തന്നെ, നേരെ മറിച്ചാണ് ശരി:
\(\cup\), \(\cap\) എന്നീ symbols മാറ്റിവെച്ചിരിക്കുന്ന ഒരു mathematical paper, author ആദ്യത്തെ page-ൽ തന്നെ അത് പറഞ്ഞിരുന്നാലും read ചെയ്യാൻ വളരെ ബുദ്ധിമുട്ടായിരിക്കും.
Python-ൽ, standard style PEP8-ൽ set ചെയ്തിരിക്കുന്നു.
(Mathematical notation-മായി കൂടുതൽ match ചെയ്യാൻ, ഈ lectures-ൽ ചിലപ്പോൾ നമ്മൾ PEP8-ൽ നിന്നും deviate ചെയ്യും)
3.6.2. Docstrings#
Modules, classes, functions, etc.-ലേക്ക് comments ചേർക്കാൻ docstrings എന്ന ഒരു system Python-ന് ഉണ്ട്.
Docstrings-ന്റെ നല്ല കാര്യം, അവ run-time-ൽ available ആണ് എന്നതാണ്.
ഇത് run ചെയ്ത് നോക്കുക:
def f(x):
"""
This function squares its argument
"""
return x**2
ഈ code run ചെയ്ത ശേഷം, docstring available ആണ്:
f?
Type: function
String Form:<function f at 0x2223320>
File: /home/john/temp/temp.py
Definition: f(x)
Docstring: This function squares its argument
f??
Type: function
String Form:<function f at 0x2223320>
File: /home/john/temp/temp.py
Definition: f(x)
Source:
def f(x):
"""
This function squares its argument
"""
return x**2
ഒരു question mark ഉപയോഗിച്ചാൽ docstring കിട്ടും, രണ്ടെണ്ണം ഉപയോഗിച്ചാൽ source code-യും കൂടി കിട്ടും.
Docstrings-ന്റെ conventions PEP257-ൽ നിങ്ങൾക്ക് കാണാം.
3.7. Exercises#
Solve the following exercises.
(For some, the built-in function sum() comes in handy).
Exercise 3.1
Part 1: Given two numeric lists or tuples x_vals and y_vals of equal length, compute
their inner product using zip().
Part 2: In one line, count the number of even numbers in 0,...,99.
Part 3: Given pairs = ((2, 5), (4, 2), (9, 8), (12, 10)), count the number of pairs (a, b)
such that both a and b are even.
Hint
x % 2 returns 0 if x is even, 1 otherwise.
Solution
Part 1 Solution:
Here's one possible solution
x_vals = [1, 2, 3]
y_vals = [1, 1, 1]
sum([x * y for x, y in zip(x_vals, y_vals)])
6
This also works
sum(x * y for x, y in zip(x_vals, y_vals))
6
Part 2 Solution:
One solution is
sum([x % 2 == 0 for x in range(100)])
50
This also works:
sum(x % 2 == 0 for x in range(100))
50
Some less natural alternatives that nonetheless help to illustrate the flexibility of list comprehensions are
len([x for x in range(100) if x % 2 == 0])
50
and
sum([1 for x in range(100) if x % 2 == 0])
50
Part 3 Solution:
Here's one possibility
pairs = ((2, 5), (4, 2), (9, 8), (12, 10))
sum([x % 2 == 0 and y % 2 == 0 for x, y in pairs])
2
Exercise 3.2
Consider the polynomial
Write a function p such that p(x, coeff) that computes the value in (3.1) given a point x and a list of coefficients coeff (\(a_1, a_2, \cdots a_n\)).
Try to use enumerate() in your loop.
Solution
Here’s a solution:
def p(x, coeff):
return sum(a * x**i for i, a in enumerate(coeff))
p(1, (2, 4))
6
Exercise 3.3
Write a function that takes a string as an argument and returns the number of capital letters in the string.
Hint
'foo'.upper() returns 'FOO'.
Solution
Here's one solution:
def f(string):
count = 0
for letter in string:
if letter == letter.upper() and letter.isalpha():
count += 1
return count
f('The Rain in Spain')
3
An alternative, more pythonic solution:
def count_uppercase_chars(s):
return sum([c.isupper() for c in s])
count_uppercase_chars('The Rain in Spain')
3
Exercise 3.4
Write a function that takes two sequences seq_a and seq_b as arguments and
returns True if every element in seq_a is also an element of seq_b, else
False.
By "sequence" we mean a list, a tuple or a string.
Do the exercise without using sets and set methods.
Solution
Here's a solution:
def f(seq_a, seq_b):
for a in seq_a:
if a not in seq_b:
return False
return True
# == test == #
print(f("ab", "cadb"))
print(f("ab", "cjdb"))
print(f([1, 2], [1, 2, 3]))
print(f([1, 2, 3], [1, 2]))
True
False
True
False
An alternative, more pythonic solution using all():
def f(seq_a, seq_b):
return all([i in seq_b for i in seq_a])
# == test == #
print(f("ab", "cadb"))
print(f("ab", "cjdb"))
print(f([1, 2], [1, 2, 3]))
print(f([1, 2, 3], [1, 2]))
True
False
True
False
Of course, if we use the sets data type then the solution is easier
def f(seq_a, seq_b):
return set(seq_a).issubset(set(seq_b))
Exercise 3.5
When we cover the numerical libraries, we will see they include many alternatives for interpolation and function approximation.
Nevertheless, let's write our own function approximation routine as an exercise.
In particular, without using any imports, write a function linapprox that takes as arguments
A function
fmapping some interval \([a, b]\) into \(\mathbb R\).Two scalars
aandbproviding the limits of this interval.An integer
ndetermining the number of grid points.A number
xsatisfyinga <= x <= b.
and returns the piecewise linear interpolation of f at x, based on n evenly spaced grid points a = point[0] < point[1] < ... < point[n-1] = b.
Aim for clarity, not efficiency.
Solution
Here’s a solution:
def linapprox(f, a, b, n, x):
"""
Evaluates the piecewise linear interpolant of f at x on the interval
[a, b], with n evenly spaced grid points.
Parameters
==========
f : function
The function to approximate
x, a, b : scalars (floats or integers)
Evaluation point and endpoints, with a <= x <= b
n : integer
Number of grid points
Returns
=======
A float. The interpolant evaluated at x
"""
length_of_interval = b - a
num_subintervals = n - 1
step = length_of_interval / num_subintervals
# === find first grid point larger than x === #
point = a
while point <= x:
point += step
# === x must lie between the gridpoints (point - step) and point === #
u, v = point - step, point
return f(u) + (x - u) * (f(v) - f(u)) / (v - u)
Exercise 3.6
Using list comprehension syntax, we can simplify the loop in the following code.
import numpy as np
rng = np.random.default_rng()
n = 100
ϵ_values = []
for i in range(n):
e = rng.standard_normal()
ϵ_values.append(e)
Solution
Here's one solution.
rng = np.random.default_rng()
n = 100
ϵ_values = [rng.standard_normal() for i in range(n)]