Skip to content

Commit 7d15e76

Browse files
committed
update
Signed-off-by: Harmouch101 <mahmoudddharmouchhh@gmail.com>
1 parent a3be6f1 commit 7d15e76

File tree

4 files changed

+277
-23
lines changed

4 files changed

+277
-23
lines changed

Chapter_02: Built-In func & Std Modules.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,4 +2117,187 @@ line_07 <------
21172117

21182118
### [binascii](https://docs.python.org/3.9/library/binascii.html?highlight=binascii)
21192119

2120+
Convert between binary and various ASCII-encoded binary representations.
2121+
2122+
**binascii.a2b_hex, binascii.unhexlify**
2123+
2124+
Coverts binary data to hexadecimal and returns a bytes object.
2125+
2126+
```python
2127+
>>> binascii.b2a_hex('Hi there!'.encode('utf-8')) # input should be a byte object.
2128+
b'486920746865726521' # length = 2 * len(input) = 2 * 9 = 18
2129+
>>> binascii.hexlify('Hi there!'.encode('utf-8'))
2130+
b'486920746865726521'
2131+
```
2132+
2133+
**binascii.a2b_hex, binascii.unhexlify**
2134+
2135+
Coverts hexadecimal to binary data and returns a bytes object.
2136+
The input should have an even number of hex digits.
2137+
2138+
```python
2139+
>>> binascii.unhexlify(b'486920746865726521')
2140+
b'Hi there!'
2141+
>>> binascii.unhexlify(b'486920746865726521').decode('utf-8')
2142+
'Hi there!'
2143+
>>> binascii.a2b_hex(b'486920746865726521').decode('utf-8')
2144+
'Hi there!'
2145+
```
2146+
2147+
### [sys](https://docs.python.org/3.9/library/sys.html?highlight=sys#module-sys)
2148+
2149+
The sys module contains most of the information relating to the current execution, as well as a series of basic functions and objects of the low level.
2150+
2151+
**argv**
2152+
2153+
Contains the list of parameters of a given script. The first element of the list is the name of the script(.py) followed by the list of parameters.
2154+
2155+
**executable**
2156+
2157+
Returns the path of the Python interpreter.
2158+
2159+
```python
2160+
>>> sys.executable
2161+
'/home/.../bin/python3'
2162+
```
2163+
2164+
**exc_info**
2165+
2166+
Returns a tuple contains the type of exception, the instance of the exception, and the traceback object(sys.last_type, sys.last_value, sys.last_traceback).
2167+
2168+
```python
2169+
>>> import sys
2170+
>>> sys.exc_info()
2171+
(None, None, None)
2172+
>>> try:
2173+
... a = input()
2174+
... a = a/3
2175+
... except:
2176+
... print(sys.exc_info())
2177+
...
2178+
qwe
2179+
(<class 'TypeError'>, TypeError("unsupported operand type(s) for /: 'str' and 'int'"), <traceback object at 0x7f13b8ccb690>)
2180+
2181+
>>> sys.last_type
2182+
<class 'TypeError'>
2183+
>>> sys.last_value
2184+
TypeError("unsupported operand type(s) for /: 'str' and 'int'")
2185+
>>> sys.last_traceback
2186+
<traceback object at 0x7f13b8ccb690>
2187+
```
2188+
2189+
**platform**
2190+
2191+
Returns the type of the current running platform.
2192+
2193+
```python
2194+
>>> sys.platform
2195+
'linux'
2196+
```
2197+
2198+
**builtin_module_names**
2199+
2200+
Returns a tuple of strings containing the names of all available modules.
2201+
2202+
```python
2203+
>>> sys.builtin_module_names
2204+
('_abc', '_ast', '_codecs', '_collections', '_functools', '_imp', '_io', '_locale', '_operator', '_signal', '_sre',
2205+
'_stat', '_string', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', 'atexit', 'builtins',
2206+
'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'posix', 'pwd', 'sys', 'time', 'xxsubtype', 'zipimport')
2207+
```
2208+
2209+
**byteorder**
2210+
2211+
Returns 'big' if the bits are in most significant order(big-Endian), and 'little' if it is in least significant order(little-Endian).
2212+
2213+
```python
2214+
>>> sys.byteorder
2215+
'little'
2216+
```
2217+
2218+
**maxsize**
2219+
2220+
Returns an integer that represents the maximum value a variable can have. In a 32-bit platform, the value is usually 2 ** 33 - 1 (2147483647), and in a 64-bit platform it is 2 ** 63 - 1 (9223372036854775807).
2221+
2222+
```python
2223+
>>> sys.maxsize
2224+
9223372036854775807
2225+
```
2226+
2227+
**version**
2228+
2229+
Returns the version of the Python interpreter.
2230+
2231+
```python
2232+
>>> sys.version
2233+
'3.7.6 (default, Jan 8 2020, 19:59:22) \n[GCC 7.3.0]'
2234+
```
2235+
2236+
**version_info**
2237+
2238+
Returns a tuple containing five version number components.
2239+
2240+
```python
2241+
>>> sys.version_info
2242+
sys.version_info(major=3, minor=7, micro=6, releaselevel='final', serial=0)
2243+
```
2244+
2245+
**api_version**
2246+
2247+
Returns the C API version.
2248+
2249+
```python
2250+
>>> sys.api_version
2251+
1013
2252+
```
2253+
2254+
2255+
**stdin, stdout, stderr**
2256+
2257+
standard input, standard output and standard error stream.
2258+
2259+
```python
2260+
>>> sys.stdin
2261+
<_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>
2262+
>>> sys.stdout
2263+
<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
2264+
>>> sys.stdout.write("Hi there!\n") # print out the message and returns the len of the string.
2265+
Hi there!
2266+
10
2267+
>>> a = sys.stderr.write("Error\n")
2268+
Error
2269+
```
2270+
2271+
**ps1, ps2**
2272+
2273+
Returns the primary and the secondary prompt for the interpreter.
2274+
2275+
```python
2276+
>>> sys.ps1
2277+
'>>> '
2278+
>>> sys.ps2
2279+
'... '
2280+
2281+
```
2282+
2283+
**implementation**
2284+
2285+
Returns an object containing information about the running python interpreter.
2286+
2287+
```python
2288+
>>> sys.implementation
2289+
namespace(_multiarch='x86_64-linux-gnu', cache_tag='cpython-37', hexversion=50792176,
2290+
name='cpython', version=sys.version_info(major=3, minor=7, micro=6, releaselevel='final', serial=0))
2291+
```
2292+
2293+
### [sys](https://docs.python.org/3/library/os.html)
2294+
2295+
The os module groups together 333 functions or objects.
2296+
2297+
```python
2298+
>>> len(dir(os))
2299+
333
2300+
>>> len([n for n in dir(os) if not n.startswith("_")])
2301+
313
2302+
```
21202303

Exercises/ex18_MonteCarlo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def pi_montecarlo(a, b, n):
77
return 4 * (b -a ) * sum(math.sqrt(1 - random.uniform(a, b)**2)
88
for i in range(n)) / n
99
if __name__ == "__main__":
10-
n = int(input("Enter the number of iteration: "))
10+
n = int(input("Enter the number of iterations: "))
1111
print('|' + '-'*98 + '|')
1212
print(f"|{'n':<24}|{'approximation':^24}|{'absolute error':^24}|{'relative error':^23}|")
1313
print('|' + '-'*98+ '|')

Exercises/ex19_timeModule.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import time
2+
3+
def func1(n):
4+
list_ = []
5+
for i in range(n):
6+
list_ += [i**2]
7+
return list_
8+
def func2(n):
9+
list_ = []
10+
for i in range(n):
11+
list_.append(i**2)
12+
return list_
13+
def func3(n):
14+
return [i**2 for i in range(n)]
15+
def func4(n):
16+
return (i**2 for i in range(n))
17+
def time_(func, n = 1000000):
18+
start = time.clock()
19+
func(n)
20+
stop =time.clock()
21+
return stop - start
22+
23+
if __name__ == "__main__":
24+
print(f"{'+ operator ':-<30}> {time_(func1) :.6f} sec")
25+
print(f"{' append ':-<30}> {time_(func2) :.6f} sec")
26+
print(f"{'list comp ':-<30}> {time_(func3) :.6f} sec")
27+
print(f"{'gen exp ':-<30}> {time_(func4) :.6f} sec")

0 commit comments

Comments
 (0)