Slide 1

Slide 1 text

Learning Python @kabirbaidhya

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Introduc on

Slide 4

Slide 4 text

What is Python?

Slide 5

Slide 5 text

Python is a programming language that lets you work quickly and integrate systems more effectively. www.python.org “ “

Slide 6

Slide 6 text

Why Python? 1. Easy to learn yet very powerful 2. General purpose language that could be used almost everywhere 3. Flexibility and scalability 4. Multi-paradigm: object oriented, functional, procedural etc. 5. Popular language, big community 6. Fun to work with 7. Career Opportunities

Slide 7

Slide 7 text

Python is everywhere CLI applications Web Apps backened / APIs GUI applications Network applications System Administration / DevOps Scienti c Applications Data Science Projects Machine Learning / Deep learning and much more

Slide 8

Slide 8 text

Ge ng Started

Slide 9

Slide 9 text

Python 2 or 3? There are two versions of python currently in use python 2.7 & python 3. So, begineers often come up with question “Should I choose Python 2 or Python 3”?

Slide 10

Slide 10 text

Current State 1. Most production applications today use Python 2.7. 2. Python 3 is ready for the production deployment of applications today. 3. Python 2.7 will only receive necessary security updates until 2020. 4. The brand name “Python” encapsulates both Python 3 and Python 2.

Slide 11

Slide 11 text

So... 3? Yes, start with python 3.x but familiarizing yourself with Python 2.7 will be very useful. In case if you have to work with legacy projects in the future you might need to know 2.x too.

Slide 12

Slide 12 text

Installa on Linux Python comes out of the box in most modern linux distros like Debian, Fedora, Centos etc. In case it's not preinstalled you can always download it from the of cial website. Windows You can download the installer from the of cial website. https://www.python.org/downloads/

Slide 13

Slide 13 text

Code Editors You could use any code editors out there to write code in python as long as it supports syntax highlighting. Here are some recommended editors/IDEs: Microsoft Vscode Atom Sublime Vim Jetbrains PyCharm IDE

Slide 14

Slide 14 text

Python Basics

Slide 15

Slide 15 text

Hello World Hello World in python is just a one liner. print "Hello, World!" Pretty neat. Isn't it?

Slide 16

Slide 16 text

Hello World: Python 3 In python3 it's print("Hello, World!") Mind the parenthesis. In python3 print is a function.

Slide 17

Slide 17 text

How'd you run it?

Slide 18

Slide 18 text

1. Directly in the Python shell. Run python shell rst. ➜ python Python 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more informa >>> Then >>> print "Hello World!" Hello World!

Slide 19

Slide 19 text

2. Standard approach Create a le hello_world.py with the code. print "Hello World!" Now run it with python. ➜ python hello_world.py Hello World!

Slide 20

Slide 20 text

How it works?

Slide 21

Slide 21 text

Compila on & Interpreta on Python is a dynamic language, and running it from the command line essentially triggers the following steps: 1. The source is compiled the rst time it is encountered (e.g., imported as a module or directly executed). This step generates the binary le, with a pyc or pyo extension depending on your system. 2. The interpreter reads the binary le and executes the instructions (opcodes) one at a time. From http://security.coverity.com/blog/2014/Nov/understanding-python-bytecode.html

Slide 22

Slide 22 text

Syntax and seman cs

Slide 23

Slide 23 text

Incase you're from a C‐style language You need to be aware of few things in python. 1. No semicolon 2. No curly braces 3. Indentation is a part of syntax 4. Dynamically-typed language

Slide 24

Slide 24 text

Pythonic code Looks something like this """ Generates Fibonacci series upto n """ def fib(n): a, b = 0, 1 while a < n: print a a, b = b, a+b fib(1000)

Slide 25

Slide 25 text

Let's dive into the code

Slide 26

Slide 26 text

Example 1: Prin ng and Comments # This prints Hello World! print "Hello World!" # This is a comment; single line comment """ And this is a multiline comment. As this could take more than one line of comments. Like this. """ print "Hello Again." Output: Hello World! Hello Again.

Slide 27

Slide 27 text

Example 2: Prin ng More # Printing more texts print "Hello World!" print "Hello Again." print "We enjoy typing." print "Learning python is easy." print "And fun." print "Printing text is way easier." Output: Hello World! Hello Again. We enjoy typing. Learning python is easy. And fun. Printing text is way easier.

Slide 28

Slide 28 text

Example 3: Prin ng more than just texts print "One + One =", 1 + 1 print "One - Two =", 1 - 2 print "Two * Five =", 1 * 5 print "Four / Two =", 4 / 2 print "Expression (12 * 5) - (2 ^ 3) + (1 / 2) =", ((12 * 5) - ( print "Seven is less than or equal to Six is", 7 <= 6 Output: One + One = 2 One - Two = -1 Two * Five = 5 Four / Two = 2 Expression (12 * 5) - (2 ^ 3) + (1 / 2) = 52 Seven is less than or equal to Six is False

Slide 29

Slide 29 text

Example 4: Variables and Prin ng first_name = "Kabir" last_name = "Baidhya" dob = "July 30, 1992" home_town = "Kathmandu, Nepal" print "Hi! I am", first_name, last_name, "." print "I was born on", dob, "." print "I'm from", home_town, "." Output: Hi! I am Kabir Baidhya . I was born on July 30, 1992 . I'm from Kathmandu, Nepal .

Slide 30

Slide 30 text

Example 5: Proper Forma ng first_name = "Kabir" last_name = "Baidhya" dob_month = "July" dob_day = 30 dob_year = 1992 difficulty = "easy" print "Hi! I am %s %s." % (first_name, last_name) print "I was born on %s %d, %d." % (dob_month, dob_day, dob_year) print "Python is %s to learn." % difficulty Output: Hi! I am Kabir Baidhya. I was born on July 30, 1992. Python is easy to learn.

Slide 31

Slide 31 text

Example 6: User Input print "What is your name?", # This would take input from the user # and store it in a variable. name = raw_input() print "Hi! %s. \nIt's nice to meet you." % (name) print "Hope you're doing good." Output: What is your name? Kabir Hi! Kabir. It's nice to meet you. Hope you're doing good.

Slide 32

Slide 32 text

Data Types

Slide 33

Slide 33 text

Buit‐in Data types Numeric: int, oat, long Boolean: bool Sequences: str, list, tuple, bytes Mappings: dict Sets: set, frozen set Read more https://en.wikibooks.org/wiki/Python_Programming/Data_Typ es

Slide 34

Slide 34 text

Immutable & Mutable types 1. Immutable types int, oat, long, tuple, byetes, frozen set, etc. 2. Mutable types list, dict, set, etc.

Slide 35

Slide 35 text

Example 7: Basic data types an_integer = 6 a_floating_point = 17.60 a_boolean = True a_string = "Foo" print "Integer value = %d" % an_integer print "Float value = %f" % a_floating_point print "Boolean value = %r" % a_boolean print "String value = %s" % a_string Output: Integer value = 6 Float value = 17.600000 Boolean value = True String value = Foo

Slide 36

Slide 36 text

Example 8: Lists fruits = ['Banana', 'Apple', 'Lime'] numbers = [1, 2, 3, 4, 5, 6, 7, 8] # This is called list comprehension even_numbers = [x for x in numbers if x % 2 == 0] print "Numbers: %s" % numbers print "Even Numbers: %s" % even_numbers print "Fruits: %s, %s and %s" % (fruits[0], fruits[1], fruits[ Output: Numbers: [1, 2, 3, 4, 5, 6, 7, 8] Even Numbers: [2, 4, 6, 8] Fruits: Banana, Apple and Lime

Slide 37

Slide 37 text

Example 9: Dic onaries data = { "name": "Kabir Baidhya", "dob": "July 30, 1992", "home_town": "Kathmandu, Nepal" } print "Hi! I am %s." % data["name"] print "I was born on %s." % data["dob"] print "I'm from %s." % data["home_town"] Output: Hi! I am Kabir Baidhya. I was born on July 30, 1992. I'm from Kathmandu, Nepal.

Slide 38

Slide 38 text

Read More? 1. http://docs.python-guide.org/en/latest 2. https://www.python.org 3. https://iluxonchik.github.io/why-you-should- learn-python/ 4. https://learnpythonthehardway.org/book/

Slide 39

Slide 39 text

Any Ques ons?

Slide 40

Slide 40 text

Thank You @kabirbaidhya kabirbaidhya@gmail.com The slides were created using Marp. https://yhatt.github.io/marp/