SL_W04IST_SI4011_lecture_01 (1).pdf

Full Transcript

Introduction into Python Script Langugages (W04IST-SI4011) Wojciech Thomas About the course Department of Applied Informatics Faculty of Inform...

Introduction into Python Script Langugages (W04IST-SI4011) Wojciech Thomas About the course Department of Applied Informatics Faculty of Information and Communication Technology Spring 2024 ©2023 by NCBiR – licensed for use in Politechnika Wrocławska 2 Course Courseware Teachers: ePortal – slides, assignments Wojciech Thomas, PhD. – lectures, labs Teams – chat [email protected] 3 4 Conditions of the Course Completion Lecture Completion Presence is not obligatory Lecture activities ad hoc, random Get passing grades from lab (lab exercises) Exam Pass the final exam (test) in the lab multiple or single answer test, no negative points you have to get over 50% of total points to pass cheating policy 5 6 Labs Completion Books Create a user account in Safari Books Online service to Presence is obligatory get access to many valuable books: Detailed conditions presented by lab teacher 1 Open https://www.safaribooksonline.com/library/vi Cheating and plagiarism policy ew/temporaryaccess/ Requirements: 2 From drop-down list Select your institution choose Not ability to explain code listed? Click here clean code 3 In login field type student’s email address follow Python coding rules 4 Finish registration using email You can access books offline from Android or iOS app on your mobile device 7 8 For a good start Python DOCS: https://www.python.org/doc/ Mark Lutz, Learning Python, 5th Edition, O’Reilly Media, Environment introduction 2013. Lee Vaughan, Impractical Python Projects, No Starch Press, 2018. Al Sweigart, Automate the Boring Stuff with Python, No Starch Press, 2nd ed, 2019. 9 10 Batteries included Python flavours - Python https://python.org/ version used in this lecture: 3.12 Python as a “batteries included” environment Distribution made by Python developers Dependency (libraries) management with pip: 1 pip install pylint 2 pip install --upgrade pylint 3 python -m pip install --upgrade pip 11 12 Python flavours - IPython Python flavours - Jupyter Notebook https://ipython.org I stands for Interactive https://jupyter.org adds extra functions to Python shell Part of Jupyter project syntax highlighting Interactive web based Notebook help data science variables introspection presentations magic commands % visualizations TAB completion and many, many more 13 14 Python flavours - Anaconda Integrated Development Environments https://www.anaconda.com/ VisualStudio Code, with extensions: (recommended) Python distribution for data science Python Pylint Includes data science libraries LiveShare Dependency management with Conda PyCharm Community or Enterprise Edition 1 conda install pylint IDLE Additional tools: R, VisualStudio Code 15 16 Language Introduction Python code file Python files use.py extension Simplest program On Windows.py extension suggests tool to run code 1 # File: hello.py 2 print("Hello world!") On Linux/iOS you have to add a “shebang” line: First run: 1 #!/usr/bin/python 1 $ py hello.py 2 Hello world! You can always run app using py : 3 $ 1 py app.py 17 18 Code Layout Code style Python uses indentation instead of brackets pycodestyle checks your code for compliance with It is advised to use spaces instead of Tabs PEP8 1 $ pycodestyle app.py I encourage you to use 4 spaces before use install pycodestyle with pip : 1 def foo(): 2 print("Line1") 3 print("Line2") 1 pip install pycodestyle 4 foo() pycodestyle was formerly known as pep8 19 20 What is PEP? The Way of Python (PEP20) Beautiful is better than ugly Explicit is better than implicit PEP ≡ Python Enhancement Proposal A set of documents describing: Simple is better than complex new functions proposals Complex is better than complicated guidelines or information process descriptions Readability counts https://www.python.org/dev/peps/ find more here 20 (but actually 19 written down) principles of Python design 21 22 Multi-paradigm language Dynamically typed variables Python is dynamically-typed language: Python allows you to use: 1 >>> id = 12 structured (procedural) programming 2 >>> type(id) object-based programming 3 >>> id = "ad3ez79" functional programming 4 >>> type(id) Compare Python to Java 23 24 Strongly typed variables Output print is a function used to output data Python is strongly-typed language: 1 >>> 1 + 1 # OK 1 >>> print("Hello!") 2 >>> "a" + "b" # OK 2 >>> print("Hello, {}!".format("World")) 3 >>> 2 + int("2") # OK 4 >>> 2 + "2" # Error! In Python2 there was a print statement 5 >>> "2" + 2 # Error! 1 >>> print "Hello!" Compare Python to JavaScript This syntax is forbidden in Python3 25 26 Basic types Input Integer int bool input is a function to read data from input Floating point float (double precision) 1 name = input("Please, type your name:") Complex numbers complex (eg. z : z.real and z.real ) string is a sequence type (see: next lecture) 27 28 Conditions Functions function definition starts with keyword def if keyword executes part of the code conditionally return keyword returns result of the function 1 if a > 5: 1 def sum(a, b): 2 print("a is bigger then 5") 2 return a+b 3 else: 3 result = sum(2, 2) 4 print("a is not bigger than 5") pass keyword is used when body is empty 1 def empty_function(): 2 pass 29 30 Loops Everything in Python is an Object Everything is object, e.g.: for is a keyword to loop through the values function value range is a function generating list of numbers type 1 for i in range(10): class 2 print(i) etc. Each object has: 3 for c in 'abcd': 4 print(c) identity (something like memory address) type value 31 32 Thank you for your attention See you next week 33

Use Quizgecko on...
Browser
Browser