Skip to content

Commit afe4305

Browse files
committed
Merge remote-tracking branch 'original repo/master'
2 parents 6e1dd5b + f52d53e commit afe4305

34 files changed

+378
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding//examples_2/05_string.py=utf-8

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
python-basics
2+
=============
3+
4+
Training stuff.

examples_2/01_name_not_defined.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
print(not_defined_variable)
2+
# Execution won't get here
3+
not_defined_varable + 2

examples_2/02_name_defined.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
a = 1
2+
print(a)
3+
a = "string"
4+
print(a)
5+
a = 0.2
6+
print(a)
7+
a = object()
8+
# BTW, in python 2 print is also a statement
9+
print a

examples_2/03_boolean.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from examples_2.utils import print_stars
2+
3+
boolean = True
4+
# Expressive python
5+
print("boolean is True or False: {}".format(boolean is True or False))
6+
7+
print_stars()
8+
9+
if boolean:
10+
print("boolean is True")
11+
else:
12+
# will not get here
13+
print("boolean is False")
14+
15+
print_stars()
16+
17+
# `and` is same as `&&` in Java, C
18+
if not boolean and boolean:
19+
# will not get here
20+
print("False and True is False")
21+
22+
print_stars()
23+
24+
# `or` is same as `||` in Java, C
25+
if not boolean or boolean:
26+
print("False or True is True")
27+
28+
print_stars()
29+
30+
list1 = []
31+
list2 = []
32+
33+
print("Lists are same: {}".format(list1 is list2)) # `is` is same as `==` in Java
34+
print("Lists are equal: {}".format(list1 == list2)) # `==` is same as `.equals()` in Java
35+
print(id(list1), id(list2))
36+
print(id(True), id(True))
37+
38+
print_stars()
39+
40+
# In python everything can be cast to True or False
41+
print("Empty String: {}".format(bool("")))
42+
print("Non-Empty String: {}".format(bool("not empty")))
43+
print("Zero: {}".format(bool(0)))
44+
print("One: {}".format(bool(1)))

examples_2/04_none.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
print("None to bool is: {}".format(bool(None)))
2+
3+
# None is default return value of function
4+
def f():
5+
pass
6+
7+
result = f()
8+
9+
print("Return value of f(): {}".format(result))

examples_2/05_string.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# -*- coding: utf-8 -*-
2+
from examples_2.utils import print_stars
3+
4+
single_line_byte_string = "single line byte string"
5+
print(single_line_byte_string)
6+
print_stars()
7+
8+
multi_line_byte_string = """Multi
9+
Line
10+
Byte
11+
String"""
12+
print(multi_line_byte_string)
13+
print_stars()
14+
15+
single_line_unicode_string = u"single line unicode string \u263a"
16+
print(single_line_unicode_string)
17+
print_stars()
18+
19+
# search for occurences
20+
print("x in abc? {}".format("x" in "abc"))
21+
print("x in xyz? {}".format("x" in "xyz"))
22+
print_stars()
23+
24+
# string length
25+
print('len("12345"): {}'.format(len("12345")))
26+
print_stars()
27+
28+
# c-style format: http://docs.python.org/2/library/stdtypes.html#string-formatting
29+
print('c-style format: %d' % 1.0)
30+
print_stars()
31+
32+
# python-style format: http://docs.python.org/2/library/string.html#format-string-syntax
33+
34+
# indexes and slices
35+
my_string = "My String"
36+
print("my_string[0]: {!r}".format(my_string[0]))
37+
print("my_string[1:3]: {!r}".format(my_string[1:3]))
38+
print("my_string[:-1]: {!r}".format(my_string[:-1]))
39+
print("my_string[::2]: {!r}".format(my_string[::2]))
40+
print("my_string[1:-1:3]: {!r}".format(my_string[1:-1:3]))
41+
print_stars()
42+
43+
# joins
44+
print('", ".join(["a", "b", "c"]): {!r}'.format(", ".join([ "a", "b", "c"])))
45+
print_stars()
46+
47+
# more info with help: help(str)

examples_2/06_list.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from examples_2.utils import print_stars
2+
3+
empty_list = []
4+
non_empty_list = [1, 'string', 3.0]
5+
6+
# Lists are mutable
7+
print("Before append(4): %r" % non_empty_list)
8+
non_empty_list.append(4)
9+
print("After append(4): %r" % non_empty_list)
10+
non_empty_list.insert(0, 0)
11+
print("After insert(0, 0): %r" % non_empty_list)
12+
non_empty_list.pop(1)
13+
print("After pop(1): %r" % non_empty_list)
14+
non_empty_list[0] = "new 0"
15+
print("After set element: %r" % non_empty_list)
16+
print_stars()
17+
18+
# Lists are indexed (starting from 0)
19+
print("non_empty_list[1]: %r" % non_empty_list[1])
20+
print("non_empty_list[1:-1]: %r" % non_empty_list[1:-1])
21+
print("Slice and List are not same objects: %r" % (
22+
non_empty_list is not non_empty_list[:]))
23+
print_stars()
24+
25+
# Lists algebra
26+
print("[1, 2] + [3, 4] = %r" % ([1, 2] + [3, 4]))
27+
print("[1] * 4 = %r" % ([1] * 4))
28+
print_stars()
29+
30+
# List comprehensions
31+
list_0 = [el for el in [1, 2, 3, 4]]
32+
list_map = [2 ** el for el in [1, 2, 3, 4]]
33+
list_filter = [el for el in [1, 2, 3, 4] if el % 2 == 0]
34+
print("[el for el in [1, 2, 3, 4]] = %r" % list_0)
35+
print("[2 ** el for el in [1, 2, 3, 4]] = %r" % list_map)
36+
print("[el for el in [1, 2, 3, 4] if el %% 2 == 0] = %r" % list_filter)
37+
print_stars()
38+
39+
# for loop
40+
for el in [1, "string", 3.0, object()]:
41+
print("In for loop, element is %r" % el)
42+
print_stars()
43+
44+
# classical for loop
45+
for i in xrange(11):
46+
print("i = %r" % i)
47+
print_stars()
48+
49+
# more info on lists: help(list)

examples_2/07_tuple.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
empty_tuple = ()
2+
non_empty_tuple = (1, 'string', 3)
3+
4+
# Tuples are immutable
5+
# non_empty_tuple[0] = 'new 1'
6+
7+
# Tuple with single element
8+
i = (1)
9+
t = (1,)
10+
print("type(i) = %r; type(t) = %r" % (type(i), type(t)))

examples_2/08_dict.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from examples_2.utils import print_stars
2+
3+
emtpy_dict = {}
4+
non_empty_dict = {'key1': 'value1', 'key2': 'value2'}
5+
6+
# Dict element access
7+
print("non_empty_dict['key1'] = %r" % non_empty_dict['key1'])
8+
print("non_empty_dict.keys() = %r" % non_empty_dict.keys())
9+
print("non_empty_dict.values() = %r" % non_empty_dict.values())
10+
print("non_emtpy_dict.items() = %r" % non_empty_dict.items())
11+
print_stars()
12+
13+
# Dicts are mutable
14+
d = non_empty_dict.copy()
15+
print("Before: %r" % d)
16+
d['key1'] = 'new value1'
17+
print("After d['key1'] = 'new value1': %r" % d)
18+
del d['key1']
19+
print("After del d['key1']: %r" % d)
20+
print_stars()
21+
22+
# Dicts are iterable (by key)
23+
d = non_empty_dict.copy()
24+
for key in d:
25+
print("In Loop. Key: %r, Value: %r" % (key, d[key]))
26+
print_stars()
27+
28+
# Test if key is in dict
29+
del d['key1']
30+
print("'key1' in d: %r" % ('key1' in d))
31+
print("'key2' in d: %r" % ('key2' in d))

examples_2/09_iterables.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from examples_2.utils import print_stars
2+
3+
# Strings
4+
print("String:")
5+
for letter in 'word':
6+
print(" %r" % letter)
7+
print_stars()
8+
9+
# Lists
10+
print("List:")
11+
for el in [1, 2, ['sub-list'], '3']:
12+
print(" %r" % el)
13+
print_stars()
14+
15+
# Tuples
16+
print("Tuple:")
17+
for el in (1, 'tuple', 2):
18+
print(" %r" % el)
19+
print_stars()
20+
21+
# Dicts
22+
print("Dict:")
23+
for key in {"key1": 1, 2: 2}:
24+
print(" %r" % key)
25+
print_stars()
26+
27+
# Iterables unpacking
28+
print("Unpack")
29+
x, y = [1, 2]
30+
print("x, y = [1, 2] -> x = %r\ty = %r" % (x, y))
31+
x, y = 'xy'
32+
print("x,y = 'xy' -> x = %r\ty = %r" % (x, y))
33+
x, y = ('v1', 'v2')
34+
print("x, y = ('v1', 'v2') -> x = %r\ty = %r" % (x, y))
35+
print_stars()

examples_2/10_function_basics.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from examples_2.utils import print_stars
2+
3+
4+
def f():
5+
pass
6+
7+
print("f() = %r" % f())
8+
print_stars()
9+
10+
11+
def f_docstring():
12+
"""Empty function. Does nothing, returns None."""
13+
pass
14+
15+
help(f_docstring)
16+
print_stars()
17+
18+
19+
def mean(v1, v2):
20+
"""Calculates mean value.
21+
>>> mean(1, 1)
22+
1.0
23+
>>> mean(2,4)
24+
3.0
25+
"""
26+
return (v1 + v2) / 2.0
27+
28+
print("mean(1, 1) = %r" % mean(1, 1))
29+
print("mean(2, 4) = %r" % mean(2, 4))
30+
help(mean)
31+
print_stars()
32+
33+
34+
# Blocks have scopes
35+
if True:
36+
def f_true():
37+
pass
38+
else:
39+
def f_false():
40+
pass
41+
42+
print('f_true: %r' % f_true)
43+
print('f_false: %r' % f_false)

examples_2/11_exception.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from utils import print_stars
2+
3+
4+
# Basic try - except - [else] - finally
5+
# `else` - optional
6+
# should have `except` or `finally` or both
7+
print("assert False")
8+
try:
9+
assert False, "Error message here"
10+
print("Execution continued")
11+
except AssertionError as e:
12+
print("Exception caught %r" % e.message)
13+
else:
14+
print("No exception thrown")
15+
finally:
16+
print("Finally block")
17+
print_stars()
18+
19+
20+
print("assert True")
21+
try:
22+
assert True
23+
print("Execution continued")
24+
except AssertionError as e:
25+
print("Exception caught")
26+
else:
27+
print("No exception thrown")
28+
finally:
29+
print("Finally block")
30+
print_stars()
31+
32+
33+
def raise_assertion_error(msg):
34+
raise AssertionError(msg)
35+
36+
raise_assertion_error("Custom Message")
37+
38+
# ValueError ~ IllegalStateException, IllegalArgumentException in Java
39+
# TypeError - object of incorrect type was used
40+
# AssertionError ~ AssertionError in Java

examples_2/__init__.py

Whitespace-only changes.

examples_2/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def print_stars():
2+
print("******")

hw_1/ayrat.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8f61cbee7a4bde849a0099da310c3252ffe84167

hw_1/dmitry yanter.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
36bad473a408ebdfc60b036905dedaa4175baae6

hw_1/ilya.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
482e68dbeed71dc76f78b5d1f0b9d00205bff675

hw_1/ivan.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a15f8b81a160b4eebe5c84e9e3b65c87b9b2f18e

hw_1/maria.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
e21fc56c1a272b630e0d1439079d0598cf8b8329

hw_1/simeon.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ac385182eeebdd2afabb8ba31c560221d91384de

hw_1/yakov.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0ff84d97fea9820f7fba81c418db8e8418a30c3e

practice_1/ayrat.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("My name is Ayrat")

practice_1/dyanter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print "Dmitry Yanter"

practice_1/ilya.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def name(a, b):
2+
return a + b
3+
print name('il', 'ya')

practice_1/irina.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print('Irina')

practice_1/ivan.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'''
2+
Created on Mar 3, 2014
3+
4+
@author: Java Student
5+
'''
6+
7+
8+
print("Ivan")

0 commit comments

Comments
 (0)