Skip to content

Commit 2d202af

Browse files
committed
update
Signed-off-by: Harmouch101 <mahmoudddharmouchhh@gmail.com>
1 parent a7b5fac commit 2d202af

File tree

4 files changed

+106
-7
lines changed

4 files changed

+106
-7
lines changed

.travis.yml

Lines changed: 0 additions & 6 deletions
This file was deleted.

Chapter_01: The Language Basics.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,76 @@ StopIteration
20502050
128
20512051
```
20522052

2053+
### 3.11.3 Generator Exercices. <a name="3.11.3"></a><h5>[Go To TOC](#TOC).</h5>
2054+
2055+
```python
2056+
>>> def factorial():
2057+
... prod = n = 1
2058+
... while True:
2059+
... yield prod
2060+
... prod *= n
2061+
... n += 1
2062+
...
2063+
>>> fact_gen = factorial()
2064+
>>> fact_gen
2065+
<generator object factorial at 0x7fc7eac497d0>
2066+
>>> hasattr(fact_gen, "__next__")
2067+
True
2068+
>>> next(fact_gen)
2069+
1
2070+
>>> next(fact_gen)
2071+
1
2072+
>>> next(fact_gen)
2073+
2
2074+
>>> next(fact_gen)
2075+
6
2076+
>>> next(fact_gen)
2077+
24
2078+
>>> next(fact_gen)
2079+
120
2080+
>>> next(fact_gen)
2081+
720
2082+
>>> next(fact_gen)
2083+
5040
2084+
>>> def odds_v0():
2085+
... result = []
2086+
... for n in range(1,10,2):
2087+
... result.append(n)
2088+
... return result
2089+
...
2090+
>>> def odds_v1():
2091+
... result = []
2092+
... for n in range(1,10,2):
2093+
... yield n
2094+
...
2095+
>>> odds_v0()
2096+
[1, 3, 5, 7, 9]
2097+
>>> odds_v1()
2098+
<generator object odds_v1 at 0x7fc7eab74cd0>
2099+
>>> list(odds_v1())
2100+
[1, 3, 5, 7, 9]
2101+
>>> odds = odds_v1()
2102+
>>> next(odds)
2103+
1
2104+
>>> next(odds)
2105+
3
2106+
>>> next(odds)
2107+
5
2108+
>>> next(odds)
2109+
7
2110+
>>> next(odds)
2111+
9
2112+
>>> next(odds)
2113+
Traceback (most recent call last):
2114+
File "<stdin>", line 1, in <module>
2115+
StopIteration
2116+
>>> timeit.timeit('for n in odds_v0(): pass', 'from __main__ import odds_v0', number=1000000)
2117+
1.2579899600004865
2118+
>>> timeit.timeit('for n in odds_v1(): pass', 'from __main__ import odds_v1', number=1000000) # iterating over a generator is slightly faster
2119+
1.1573705800001335
2120+
```
2121+
2122+
20532123
### 3.12 Input. <a name="3.12"></a><h5>[Go To TOC](#TOC).</h5>
20542124

20552125
Using the built-in `input()` method, you can get information from the user. It has an optional parameter `prompt` which prints a string on the screen.

Chapter_02: Functions, File IO, Generators.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,3 +1855,38 @@ To move the pointer to a given position, use the seek() function.
18551855

18561856
### 2.3 Writing to a file. <a name="2.3"></a>
18571857

1858+
The `write()` function is used to write to a file opened in write mode. If the file does not exist, then a new one will be created.
1859+
1860+
```python
1861+
>>> f = open('file1.txt', 'w')
1862+
>>> help(f.write)
1863+
1864+
write(text, /) method of _io.TextIOWrapper instance
1865+
Write string to stream.
1866+
Returns the number of characters written (which is always equal to
1867+
the length of the string).
1868+
1869+
>>> f.write('Hi \n There!')
1870+
11
1871+
>>> len('Hi \n There!')
1872+
11
1873+
```
1874+
1875+
### 2.4 Closing a file. <a name="2.4"></a>
1876+
1877+
The `close()` method automatically closes the file, and any unsaved information is lost.
1878+
1879+
```python
1880+
>>> f = open('file1.txt', 'r')
1881+
>>> f.readline()
1882+
'Hi \n'
1883+
>>> f.readline()
1884+
' There!'
1885+
>>> f.closed
1886+
False
1887+
>>> f.close()
1888+
>>> f.closed
1889+
True
1890+
```
1891+
1892+

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Python-3-Notes
1+
# Python-3-Resources
22

33
This repository is used to be documentation during my journey in mastering the python programming language while reading plenty of books and tons of online resources.
44

0 commit comments

Comments
 (0)