851.33K
Категория: ПрограммированиеПрограммирование

Advanced Programming I

1.

Advanced Programming I
Lecture 1 – Introduction to Python
Shynggys Kairatuly Alshynov
MSc in IT
[email protected]

2.

Academic Fairness
• Cheating is not acceptable!
• Anyone who cheats or anyone who gives his/her work will receive a
mark of 0.
• Please, be honest and do not hesitate to contact me or talk to me if
you have problems or questions.
• Marking system:
• 80% - for the completion of a lab
• 20% - additional self-study for a lab

3.

Objectives
• What is Python?
• Why use Python?
• Python versions
• Python installation
• Environment for development
• Built-in instruments and pip
• Useful links and official documentation
• Your first primitive program in Python
• Python print() function
• Short description of python programming components
• Topics for the next lectures

4.

Python Book and Resources
• Basic literature:
• www.python.org – Python official web site with fully provided
documentation. [Online]
• Kuhlman, D. (2009). A Python Book: Beginning Python, Advanced Python,
and Python Exercises. 1st ed. http://www.opensource.org/licenses/mitlicense.php.
• Mueller, J.P. (2018). Beginning Programming with Python for dummies. 2nd
editions. https://www.pdfdrive.com/beginning-programming-with-pythonfor-dummies-e176211134.html
• Supplementary literature:
• Cisco Corp., Programming Essentials in Python, 2018, www.netacad.com
Edube[Online]

5.

Learning outcomes
• At the end of this lecture you will be able to:
• Explain what Python is;
• Tell purposes why you use Python;
• Understand differences between Python versions;
• Create very simple programs in Python.

6.

What is Python?
• Python is an interpreted, object-oriented, high-level programming
language with dynamic semantics. Its high-level built in data
structures, combined with dynamic typing and dynamic binding,
make it very attractive for Rapid Application Development[2], as well
as for use as a scripting or glue language to connect existing
components together.[1]

7.

Some useless history of Python
• Python is an interpreted high-level programming languagefor generalpurpose programming. Created by Guido van Rossum and first
released in 1991, Python has a design philosophy that
emphasizes code readability, notably using significant whitespace. It
provides constructs that enable clear programming on both small and
large scales.[25] In July 2018, Van Rossum stepped down as the leader
in the language community after 30 years.
• (википедия, короч)

8.

Why use Python?
• Absolutely free and available online:
• Wide range of libraries.
• Python is everywhere:
• Web apps
• DB apps
• Desktop apps
• Data analysis
• Image and Video pre/processing
• OOP
• Interactive visualization

9.

Python versions
• Widely used versions of Python are 2.7.* and 3.*
• Please install python version of 3.6.* and newer (but not beta)
• What are differences of these versions?
• SYNTAX and Built-in functions
Python 2.7.*
Python 3.6.*

10.

Python versions
• Python 2.x is legacy and 3.x is the present and future of the
language.
• Python 3.x has downsides as slightly worse library support and some current
Linux distributions and Macs still use 2.x.
• Memory efficiency
• Home Task: Read this piece of reading
https://wiki.python.org/moin/Python2orPython3

11.

Python Installation
• https://www.python.org/downloads/ - please go here after the
lecture and install your python. Be cautious as you need to add
python interpreter to your path variables (переменные среды).

12.

Environment for development
• Common Python - download
• Official releases of Python, IDLE and Python shell
• Recommended versions 2.7.x and 3.6.x
• CMD
• Run cmd and type “python”
• Side development kits:
• Web resource – www.pythonanywhere.com
• Free server with some limited space
• PyCharm - https://www.jetbrains.com/pycharm/
• For the professional purposes, “kind of”
• Jupyter Notebook - http://jupyter.org/
• Useful notebook to start a local server and keep your scripts

13.

Examples of Environments
• IDLE
• CMD

14.

Examples of Environments
• PyCharm
Jupyter Notebook

15.

Built-in instruments and pip
• List of built-in instruments (https://docs.python.org/3.6/library/functions.html)
• pip is the tool to install
new libraries to your
existing python
environment

16.

Useful links
• Check these web resources if you are interested (I hope you are):
• www.google.kz
• www.python.org
• https://www.practicepython.org/resources-for-learners/
• https://codingbat.com/python

17.

First primitive program in Python
• lec1.py - program output

18.

First primitive program in Python
• lec1.py - program code
a = ""
def lec1(s):
global a
a = '"Fly fools" - , said ' + s
return a

19.

First half Summary (break)
• Python is not a snake
• Python is relatively better than whatever programming language
you can think about
• Generally 2 common versions
• IDLE, Shell, CMD, Jupyter Notebook, www.pythonanywhere.com,
PyCharm, etc.
• We can print out something

20.

It is good isn’t it?
If you do not have any pet raccoons.

21.

Second Half
• print() function
• Output formatting
• Data Input
• Commenting of code
• Code examples

22.

print ()
• Probably, the most common function that you (will) use in Python.
• Built-In function in Python.
• print “Some Text” – python 2.7.*
• print(“Another Text) – python 3.6.*
• Arguments:
• print(*object, sep=“”, end='\n', file=sys.stdout, flush=False)
• Example:
• print(“Can someone lend me”, “2 million dollars”, sep=“PLEASE”, end=“?!”)

23.

Output formatting
• You can customize your output.
• So how can you do it?
• Several ways to do it:
• Example 1:
• name = “Gintoki”
• print(“My name is %s” % name)

24.

Output formatting
• Example 2:
• name = “Gintoki”
• surname = “Sakata”
• print(“My full name is %s %s” % (surname, name))
• Example 3:
• a = 10
• b = 10
• print(“Sum of %i and %i is equal to “ % (a,b), a+b)

25.

Output formatting
• Example 4:
• .format(...) method
• print(“You will {1} it useful, {0} you?”.format(“won't”, “find”))
• HT: Read about .format() in documentation, you will find something special.
• Some keys in formatting for the %
i – int
d – signed int decimal
c – character
f – float
• Full table is given in the next slide

26.

Table of conversion using %
Conversion
Meaning
d
Signed integer decimal.
i
Signed integer decimal.
o
Unsigned octal.
u
Obsolete and equivalent to 'd', i.e. signed
integer decimal.
x
Unsigned hexadecimal (lowercase).
X
Unsigned hexadecimal (uppercase).
e
Floating point exponential format (lowercase).
E
Floating point exponential format
(uppercase).
f
Floating point decimal format.
F
Floating point decimal format.
g
Same as "e" if exponent is greater than -4 or
less than precision, "f" otherwise.
G
Same as "E" if exponent is greater than -4 or
less than precision, "F" otherwise.
c
Single character (accepts integer or single
character string).
r
String (converts any python object
using repr()).
s
String (converts any python object using str()).
%
No argument is converted, results in a "%"
character in the result.

27.

Math operators
Operation
Syntax
Function
Addition
Concatenation
a+b
seq1 + seq2
add(a, b)
concat(seq1, seq2)
Containment Test
obj in seq
contains(seq, obj)
Division
a/b
truediv(a, b)
Division
a // b
floordiv(a, b)
Bitwise And
a&b
and_(a, b)
Bitwise Exclusive Or
a^b
xor(a, b)
Bitwise Inversion
~a
invert(a)
Bitwise Or
a|b
or_(a, b)
Exponentiation
Identity
Identity
a ** b
a is b
a is not b
pow(a, b)
is_(a, b)
is_not(a, b)
Indexed Assignment
obj[k] = v
setitem(obj, k, v)
Indexed Deletion
del obj[k]
delitem(obj, k)
Indexing
Left Shift
Modulo
obj[k]
a << b
a%b
getitem(obj, k)
lshift(a, b)
mod(a, b)

28.

Math operators (cont.)
Operation
Syntax
Function
Multiplication
Matrix Multiplication
a*b
a@b
mul(a, b)
matmul(a, b)
Negation (Arithmetic)
-a
neg(a)
Negation (Logical)
not a
not_(a)
Positive
+a
pos(a)
Right Shift
a >> b
rshift(a, b)
Slice Assignment
seq[i:j] = values
setitem(seq, slice(i, j), values)
Slice Deletion
del seq[i:j]
delitem(seq, slice(i, j))
Slicing
seq[i:j]
getitem(seq, slice(i, j))
String Formatting
Subtraction
Truth Test
s % obj
a-b
obj
mod(s, obj)
sub(a, b)
truth(obj)
Ordering
a<b
lt(a, b)
Ordering
a <= b
le(a, b)
Equality
Difference
Ordering
a == b
a != b
a >= b
eq(a, b)
ne(a, b)
ge(a, b)

29.

Data Input
• To input data you can use a ready function called “input”:
• Example:
• a = input(“Ask a user to input something”) (for python 3.*)
• Returns only string
• b = raw_input(“Ask a user to input something”) (for python 2.*)
• Return only strings too
• b = input(“Ask a user to input something”) (for python 2.*)
• Returns a data type that was entered

30.

Adding comments to your code
• Commenting code is a good way of programming
• Commenting helps to understand your code for a person who will
work with/after you
• To comment your code you can use:
• # - sharp (octothorpe), comments one line of code
• '''' Something '''' – triple quotes to open and another three to close the
commenting; comments several lines

31.

Summary
• Python is still not a snake
• Now you can use print() function properly
• Can use conditionals in Python
• Got familiar with math operators in Python
• You can make input() from a keyboard

32.

That is all for today. Thank you for your
attention!
If you do not have any pet raccoons.
English     Русский Правила