What is Python?

32
Welcome! Python SIG – Students with programming experience (PYA) – Class 1 – 12-9-15

Transcript of What is Python?

Page 1: What is Python?

Welcome!

Python SIG – Students with programming experience (PYA) –

Class 1 – 12-9-15

Page 2: What is Python?

Ask doubts and give feedback

Page 3: What is Python?

Let’s learn Python

Page 4: What is Python?

hello world!

• The traditional first program• In Python 2:–print ‘hello world!’

• In Python 3:–print(‘hello world!’)

Page 5: What is Python?

2 vs 3

• 2.7 last in 2 series• 3 under development• Ultimately, not much difference• “The most drastic improvement is

the better Unicode support (with all text strings being Unicode by default)” [1]

Page 6: What is Python?

2 vs 3

• Difference in print – statement, NOT function• Libraries, libraries, libraries

Page 7: What is Python?

Why 2 then?

• Tools & support – 2to3, future module• Libraries, libraries, libraries. Example:

Web development• Easy to learn the other

Page 8: What is Python?

What is code?

Page 9: What is Python?

What is code?

• Any fool can write code that a computer can understand. Good programmers write code that humans can understand.Martin Fowler, "Refactoring: Improving the Design of Existing Code" [2]

Page 10: What is Python?

Objects, objects everywhere!

• “Everything in Python is an object, and almost everything has attributes and methods. All functions have a built-in attribute __doc__, which returns the doc string defined in the function's source code.” [3]

From Dive Into Python by Mark Pilgrim• help(int)

Page 11: What is Python?

Python implementations

• Language and implementation – separate issues

• Language == syntax• “An \"implementation\" of Python should be

taken to mean a program or environment which provides support for the execution of programs written in the Python language” [4]

From wiki.python.org

Page 12: What is Python?

Python implementations

• CPython – reference implementation• Others:

PyPy – Python in Python - compiledIronPython - .NET comaptibleJython – JVM compatible

• Further reading:http://www.toptal.com/python/why-are-there-so-many-pythons

Page 13: What is Python?

CPython

• Interpreted; with “bytecode” in between (like Java)

• C heritage• Examples:– Can use numbers as boolean types; though a

separate boolean type exists– __name__ == __main__

Page 14: What is Python?

Obligatory xkcd reference [5]

Page 15: What is Python?

Explaining the comic - why Python is awesome

• Dynamic vs static typing• Whitespace as indentation• Strongly typed vs weakly typed languages• REPLs – rapid prototyping• Interpreted – line by line; easier to find errors

Page 16: What is Python?

import - ing

• Libraries and packages• Batteries included philosophy of Python• import antigravity• import this• import lotsofotherawesomestuff• import modulename (BTW, modules (== .py files))• from modulename import methodname (BTW, methods ((not exactly) == functions))

Page 17: What is Python?

Python is just words

• and del from not while as elif global or with assert else if pass yield break except import print class exec in raise continue finally is return def for lambda try

• + variables• + structure that you create

Page 18: What is Python?

User input

• Complex programs = input + efficient computation + output

• raw_input, NOT input• raw_input – reads as string, so convert when

required (strongly typed, remember?)• Example:– var = raw_input(prompt)

Page 19: What is Python?

Assignment statements

• Assignment is = • ( and equality is ==, but you know this right?)• Variable names should be sensible• varname = computation / input• Depends on frame

Page 20: What is Python?

(Important) Built-in types

• boolean• int • float• string• list• tuple• dictionary

Page 21: What is Python?

General idea

• Flexibility in Python• Lists >> Arrays• Methods – making things simpler

Page 22: What is Python?

Mutability

• What is it?• Why is it important?

Page 23: What is Python?

Immutable

• int • float• string• tuple

Page 24: What is Python?

Mutable

• list• dictionary

Page 25: What is Python?

Operations

• + - * / % and ** for exponentiation• Also +=, -=, *=, /=• Floating point arithmetic– [int].0– Or float()

Page 26: What is Python?

Boolean

• True / False• >• <• ==• !=• >=• <=• is , is not (same as id())

Page 27: What is Python?

Strings

• Immutable• Single or double, they don’t care• Raw strings• Escape sequences• String slicing – string[start:stop:step]• We count from 0 (why?)• String methods

Page 28: What is Python?

Read the documentation

• If you know programming, (which you say you do) reading the documentation is the best way to learn

• Don’t have to memorise, only know how to find what you need

Page 29: What is Python?

In-class assignment

• Start coding!• Use help() and dir()• Use the offline docs

Page 30: What is Python?

Thanks!

Pranav S Bijapur, ECE, BMSCE, Bangalore

[email protected] Telegram - @pranavsb

Page 31: What is Python?

References

1 - https://wiki.python.org/moin/Python2orPython3

2 -https://en.wikiquote.org/wiki/Martin_Fowler

3 -http://www.diveintopython.net/getting_to_know_python/everything_is_an_object.html

Page 32: What is Python?

References

4 - https://wiki.python.org/moin/PythonImplementations

5 -https://www.xkcd.com/353/