Skip to content

Commit 4c04f9f

Browse files
author
lev epshtein
committed
adding more tut
1 parent 00d0f88 commit 4c04f9f

File tree

7 files changed

+158
-0
lines changed

7 files changed

+158
-0
lines changed

function/functions.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## 1 basic function definition, empty function
2+
3+
# def hello_func():
4+
# pass
5+
#
6+
# print(hello_func())
7+
8+
## 2 function defenition
9+
# def hello_func():
10+
# print('hello function!')
11+
#
12+
# hello_func()
13+
# hello_func()
14+
# hello_func()
15+
# hello_func()
16+
# hello_func()
17+
18+
## 3 return of function and method on return
19+
# def hello_func():
20+
# return 'hello function!'
21+
# print(hello_func())
22+
# print(hello_func().upper())
23+
24+
## function arguments, to show : error when without argument, default value argument, couple arguments
25+
# def hello_func(greeting):
26+
# return '{} function!'.format(greeting)
27+
#
28+
# print(hello_func())
29+
30+
#####
31+
# DRY principle keep your code clean without repeats. Don't repeat yourself !
32+
# Concentred on input and return value of functions
33+
#####
34+
35+
# Function with args and kwargs
36+
# def student_info(*args, **kwargs):
37+
# print(args)
38+
# print(kwargs)
39+
# #
40+
# student_info('Math', 'Art', name='Jhon', age=22)
41+
# print ("---")
42+
# courses = ['Math', 'Art']
43+
# info = {'name':'Jhon', 'age':22}
44+
45+
# student_info(courses, info)
46+
# print ("---")
47+
# student_info(*courses, **info)

function/lambda_fnc.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# lambda expretion or lambda function
2+
# anonymous funtion - function that don't have name bounce to it
3+
4+
# regular function
5+
# def p(num):
6+
# return num ** 2
7+
# print(p(4))
8+
# print(type(p))
9+
10+
# lambda function general syntax :
11+
# y = lambda x: x ** 2
12+
# print(y(4))
13+
# print(y)
14+
15+
## Why is usefull, for example :
16+
# def h(n):
17+
# return lambda x: (x + n) ** 2
18+
#
19+
# c = h(3)
20+
# b = h(0)
21+
# print(c)
22+
# print(b)
23+
#
24+
# print (b(2))
25+
# print(c(2))
26+
27+
##
28+
# Another example build quadratic functions
29+
# def build_quadratic_runction(a, b, c):
30+
# """Returns the function f(x)=ax^2 + bx + c"""
31+
# return lambda x: a*x**2 + b*x + c
32+
#
33+
# f = build_quadratic_runction(2, 3, -5)
34+
#
35+
# print(f(0))
36+
# print(f(1))
37+
# print(f(2))
38+
#
39+
# print(build_quadratic_runction(3, 0, 1)(2)) # 3x^2+1 evaluated for x=2

generators/generator.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# def square_numbers(nums):
2+
# for i in nums:
3+
# yield (i*i)
4+
5+
6+
# my_nums = square_numbers([1, 2, 3, 4, 5])
7+
8+
9+
# print(next(my_nums))
10+
# print(next(my_nums))
11+
# print(next(my_nums))
12+
# print(next(my_nums))
13+
# print(next(my_nums))
14+
15+
# for num in my_nums:
16+
# print(num)
17+
18+
# list comprehention advance
19+
20+
# my_nums = list(x*x for x in [1,2,3,4,5])
21+
22+
# print (my_nums)
394 Bytes
Binary file not shown.

modules/mymath.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""mymath - example math module"""
2+
pi = 3.14159
3+
def area(r):
4+
"""area(r): return the area of a circle with radius."""
5+
global pi
6+
return(pi * r * r)
7+
8+
# In REPL
9+
# >>> import mymath
10+
# >>> mymath.__doc__
11+
# >>> from mymath import pi
12+
# >>> pi

virtualenv/virtualenv-tutorial.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
virtualenv - you can separete differnt python environments
2+
3+
- pip install virtualenv
4+
- mkdir Environment
5+
- cd Environment
6+
- virtualenv project1_env/
7+
- cd project1_env/Scripts
8+
- activate.bat
9+
10+
Let's install some packages :
11+
- pip install numpy
12+
- pip install pytz
13+
- pip install psutil
14+
- pip freeze --local > ..\..\requirments.txt
15+
- rm -rf project1_env/
16+
17+
Let's create second project with different version of python and install modules
18+
- virtualenv project2_env/
19+
- virtualenv project2_env/
20+
- cd project2_env/Scripts
21+
- activate.bat
22+
- pip install -r ..\..\requirements.txt

web-scraping/scraping.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Basic scraping
2+
# from https://github.com/pricing/
3+
# use inspect in browser to find HTML path
4+
# Install lxml and request:
5+
# pip install lxml request
6+
7+
8+
from lxml import html
9+
import requests
10+
11+
page = requests.get('https://github.com/pricing/')
12+
tree = html.fromstring(page.content)
13+
print("Page Object:", tree)
14+
plans = tree.xpath('//h3[@class="h2-mktg text-bold mt-1"]/text()')
15+
pricing = tree.xpath('//h3[@class="h1-mktg lh-condensed-ultra my-3 py-1"]/text()')
16+
print("Plans:", plans, "\nPricing:", pricing)

0 commit comments

Comments
 (0)