Python is Popular
Python has been growing in popularity over the last few years. The 2018 Stack Overflow Developer Survey ranked Python as the 7th most popular and the number one most wanted technology of the year. World-class software development countries around the globe use Python every single day.
According to research by Dice Python is also one of the hottest skills to have and the most popular programming language in the world based on the Popularity of Programming Language Index. Due to the popularity and widespread use of Python as a programming language, Python developers are sought after and paid well. If you’d like to dig deeper into Python salary statistics and job opportunities, you can do so here.
Python is Simple
Python code has a simple and clean structure that is easy to learn and easy to read. In fact, as you will see, the language definition enforces code structure that is easy to read. I want you have practice & understanding basic things from Python listed below before you go to the next section.
- Basic syntax
- Data types
- Classes, methods.
- Magic functions.
Looks very easy, but don't worry we will dive into the python during the development process.
History
Guido van Rossum is best known as the author of the Python programming language
"Benevolent Dictator For Life" for Python.
Helped develop the ABC programming language
In December 1989 had been looking for a "'hobby' programming project that would keep him occupied during the week around Christmas" as his office was closed.
He attributes choosing the name "Python" to "being in a slightly irreverent mood (big fan of "Monty Python's Flying Circus)
Source of inspiration
Maybe you don't heard about the ABC, Modula-3 programming languages, but python has borrowed some ideas from them and as we said before Van Rossum worked on developing ABC.
"ABC" programming language
HOW TO RETURN words document:
PUT {} IN collection
FOR line IN document:
FOR word IN split line:
IF word not.in collection:
INSERT word IN collection
RETURN collection
"MODULA-3" Programming langauge
TRY
DO.SOMETHING()
EXCEPT
IO.ERROR => IO.PUT("An I/O error occurred.")
END;
Wanted very simple, convenient, clear and useful language then we've got a Python. Look at the code below, even if you haven't worked with python you can easily understand what function does.
def magic(dir):
acc = []
for root, dirs, files in os.walk(dir):
acc.extend(os.path.join(root, file) for file in files)
return acc
Function magic get one argument which is a directory and get all root, directories, files on that directory. Build path absolute path for every file and add it to list. Easy! is not it?
Versions
In python world there are two main version of Python (Python 2 and Python 3). Start use version 3 and don't worry about it, cause in our days it's stable and widely used version.
Python - is a high level language
We don't care how does machine language code looks like also how does our program makes hardware work. We just write human readable, understandable code and run it. So that what is High Level Programming Language is. Python is a high level language that's why we can put it beside C++, Java, Pascal.
Main ideas, Data types, conditional operators, loops.
Module - is the same as a code library. A file containing a set of functions you want to include in your application with extension .py. _For example let's suppose that we have _hello.py file then we can use it as a module (we import functions, objects which were declared inside that file)
>>> import hello
>>> hello
<module 'hello' from '[...]/hello.py'>
>>> dir(hello)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__spec__', 'message']
>>> hello.__name, hello.__file__
('hello','[...]/hello.py')
>>> print(hello.message)
"Hello, world!"
indentation - Leading white space (spaces and tabs) at the beginning of a logical line is used to compute the indentation level of the line, which in turn is used to determine the grouping of statements. By conventions the indentation is equals to four white spaces.
Object - Everything is object.
>>> import hello
>>> type(hello)
<class 'module'>
>>> type(type(hello))
<class 'type'>
>>> type(type(type(hello)))
<class <'type'>
Data types
Strange
>>> None
>>> None == None
True
>>> None is None
True
Logical
>>> to_be = False
>>> to_be or not to_be
True
>>> True or print(None) # lazy operator
True
>>> 42 + True
43
There are exist Falsy & Truthy values in python. All values which looks like False is Falsy value. For example
>>> my_string = '' # empty string
>>> my_dict = {} # empty dictionary (key/value pairs)
>>> my_list = [] # empty list
>>> my_set = set() # empty set
>>> my_number = 0 # just zero
>>> my_tuple = () # empty tuple
>>> my_nothing = None # null in other languages
>>> '''All values above will return False if we will check them as Boolean'''
>>> if my_string:
>>> print("This will never prints because 'my_string' is Falsy value and return False")
>>>
Operators
>>> 0 or 10 or 20 or 30
10
# or - is lazy operator, which means that it will return the first Truthy value.
# For above example: It checks 0 and found that it's Falthy value, then it goes
# to the next one and checks it. 10 is Truthy value, so it will return it.
>>> if 0 or 10 or 20 or 30:
>>> print('This line will be printed, since the condition above is True')
# Question: What will be returned for expression below
>>> 0 or []
# Answer: it will return empty list, since it's the last checked element.
>>> 1 and 3 and [] and True
[]
# and - is also lazy operator, but it will return the first Falthy value.
# For above example: It checks 1, since it's Truthy value goes to the next one,
# checks 3, it's also Truthy value, so it goes to the [] and checks it.
# Found that empty list is Falthy value then return it
>>> if 1 and 3 and [] and True:
>>> print('This line will never printed, since the condition above is False')
# Question: What will be returned for expression below
>>> 1 and 3 and 8
# Answer: it will return eight, the last checked element.
Numbers
>>> 42 # integer
>>> 0.42 # float
>>> 42j # complex
>>> 16/3
5.3333333333
>>> 16//3
5
>>> 16 % 3
1
Strings & bytes
There is no difference with quotes to declaring string:
>>> single_quote_string = 'Hello world'
>>> double_quote_string = "Hello world 2"
>>> triple_quote_string = '''Hello world 3'''
>>> triple_doubled_quote_string = """Hello world 4"""
bytes - is a sequence consisted of 8-bit values
string - is a sequence consisted of __**unicode\_**_ symbols.
>>> b'foo' # bytes
b'foo'
>>> b'foo'.decode("utf-8")
'foo'
>>> bar = "bar"
>>> len(bar)
3
>>> s = '.' * 10
'.........'
>>> " foo bar ".strip()
'foo bar'
Lists
>>> [] # empty list
>>> my_list = list()
>>> [0] * 4
[0, 0, 0, 0]
>>> my_list = [1, 2, 3, 4]
>>> len(my_list)
4
>>> my_list[1]
2
>>> my_list[0] = 10
>>> my_list
[10, 2, 3, 4]
>>> my_list.append(42)
[10, 2, 3, 4, 42]
>>> del my_list[0] # or my_list.pop(0)
[2, 3, 4, 42]
Concatenation and slices
>>> xs = [1, 2, 3, 4]
>>> xs + [5, 6]
[1, 2, 3, 4, 5, 6]
>>> ", ".join(['foo', 'bar'])
'foo, bar'
>>> s ='foobar'
>>> xs[:2]
[1, 2]
>>> s[:2]
'fo'
>>> xs[1:3]
[2, 3]
>>> s[1:3]
'oo'
>>> xs[0:4:2]
[1, 3]
>>> s[0:4:2]
'fo'
>>> xs[:]
[1, 2, 3, 4]
>>> s[:]
'foobar'
Tuple, Set, Dictionary
Tuple is almost the same data type as a list, but it's immutable and for it's declaration we use '()'
>>> tuple()
()
>>> date = ('year', 2018)
>>> date[1] = 2019
Traceback (most recent call last):
File "stdin", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> del date[0]
Traceback (most recent call last):
File "stdin", line 1, in <module>
TypeError: 'tuple' object does not support item deletion
Set stores unique values
>>> set() # hash-set
set()
>>> xs = {1, 2, 3, 4}
>>> 42 in xs
False
>>> 42 not in xs
True
>>> xs.add(42) # {1, 2, 3, 4, 42}
>>> xs.discard(42) # {1, 2, 3, 4}
# Operators IN, NOT IN works for all containers
>>> 42 in [1, 2, 3, 4]
False
>>> 'month' not in ('year', 2018)
True
>>> xs = {1, 2, 3 ,4}
>>> ys = { 4 ,5}
>>> xs.intersection(ys) # or (xs & ys)
{4}
>>> xs.union(ys) # or (xs | ys)
{1, 2, 3, 4, 5}
>>> xs.difference(ys) # or (xs - ys)
{1, 2, 3}
Dictionary is a key/value table
>>> {} # or dict(), hash-table
>>> date = {'year': 2018, 'month': 'September'}
>>> date['year']
2018
>>> date.get('day', 7) # if there is no key 'day' return default value 7
7
>>> date.keys()
dict_keys(['month', 'year'])
>>> date.values()
dict_values(['September', 2018])
Conditional operators & loops
>>> x = 42
>>> if x % 5 == 0:
>>> print('fizz')
>>> elif x % 3 == 0:
>>> print('buzz')
>>> else:
>>> pass # do nothing
'buzz'
>>> "even" if x % 2 == 0 else 'odd' # ternary operator
'even'
>>> i = 0
>>> while i < 10:
>>> i += 1
>>> i
10
>>> sum = 0
>>> for i in range(1, 5):
>>> sum += i * i
>>> sum
30
Sprint 2
Learn python.
Assignment
- "Learn python" - practice course
- "solo learn python" - online course
- Try to read python in Habr
- Python and basic TDD task
- Choose python in Exercism and try to solve the problem as much as possible
Expectation
- You are comfortable to work with python.
- Data types, Classes, magic functions
- Inheritance,