Introduction to Python
by Siddharth Yadav
About Me
- Python/C++/Go Programmer
- Working with python and c++ for about 6 years
- Started working at Synapse in April 2015
- Xk0nSid anywhere on the internet is me
What is python?
- A general purpose, interpreted programming language.
- High level Language
- Object Oriented
- Dynamic Typing
- Cross Platform
- Created by Guido Von Rossum, a German Computer Scientist in 1991. Currently an employee of Google Inc. working as lead of Google's Python Lab.
Setting up Python
How to get Python?
- Just go to http://python.org/downloads
What is Python 2 & Python 3?
- Two stable versions of Python
- Python 3 was released in 2008
- Python 3 is not backwards compatible
Which one should you use?
- Basically can choose anyone
- Python 3 is new and is the future
- Python 2 has most support and 3rd party packages
Installing Python
Windows
Just use the windows installer provided by python.org
Linux/Mac OSX
- It's there already
- Most linux distros use python for system automation and utilities
- Mostly Python 2.6
- It's old
- Install python using built in package manager
- Use Homebrew or Mac OSX installer from python.org for Macintosh
Check Python Installation
$ python --version
Python 2.7.8Just run "python --version" on terminal or command prompt
Python
version 2.7
Python IDLE or Interpreter
- Integrated DeveLopment Environment
- Just type "python" in terminal
- You should see something like this
➜ ~ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> Data in Python
- Numeric Types
- Sequences
- Sets
- Mappings
Numeric Types
- int: Integers; equivalent to C longs in Python 2.x, non-limited length in Python 3.x
- long: Long integers of non-limited length; exists only in Python 2.x
- float: Floating-Point numbers, equivalent to C doubles
- complex: Complex Numbers
Sequences
- str: String; sequence of 8-bit characters in Python 2.x, Unicode characters in Python 3.x
- byte: a sequence of integers in the range of 0-255; only available in Python 3.x
- byte array: like bytes, but mutable only available in Python 3.x
- list
- tuple
Sets
an unordered collection of unique objects
Mappings (Dictionary)
Python dictionaries, also called hashmaps or associative arrays, which means that an element of the list is associated with a definition
Booleans
- True
- False
Mutable Objects
- byte array
- list
- set
- dict
Immutable
- int, float, long, complex
- str
- bytes
- tuple
- frozen set
- Mutable Objects can be changed after creation.
- Immutable objects cannot be changed.
Objects
Dive into Code
Control Flow
If Statement
if (condition):
# block of code
elif (condition):
# block of code
else:
# block of codeIf statement in python
- brackets for "conditions" is optional
- elif is "else if" block
x = [100,200,300]
for i in x:
print iFor Loop
- A for loop in python
- Mostly used with lists or dictionaries
- Another example of for loop
l = [(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
for x, xsquared in l:
print x, ':', xsquaredWhile Loop
while (condition):
# block of code
x = 0
while True:
if x > 10:
break
print x
x++- While loop in python
- Don't forget to conditionally break loop
Functions
- A block of code that can be reused
- Can receive parameters both positional and keyword
- Can return something(s)
- A function definition in python
# A function in Python
def say(to, what="Hello"):
return what + " " + toClasses
- Defines a custom object
- Can receive arguments
- Can have static and instance method
- A simple class definition in python
class Hello:
def __init__(self, to, what="Hello"):
self.what = what
self.to = to
def say(self):
"""This is a public method"""
print self._build_message()
def _build_message(self):
"""This is a private method"""
return self.what + " " + self.toModules
- Bits of reusable code in Python
- Can be a python file or a folder
- Can also be DLL, SO, PYD
- Used with import and from statements
import math
import socket
import numpy
from hashlib import sha1, md5
from flask import FlaskDive into Code
Finding Help
- Documentation at Python.org
- Using in built Help function
➜ ~ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> help(help)
Help on _Helper in module site object:
class _Helper(__builtin__.object)
| Define the builtin 'help'.
| This is a wrapper around pydoc.help (with a twist).
|
| Methods defined here:
|
| __call__(self, *args, **kwds)
|
| __repr__(self) ...
The End
Introduction to Python
By Synapse Python
Introduction to Python
- 164