27
Introduction to Python Lecture 2 Kasyanov Anton IASA 2011

Anton Kasyanov, Introduction to Python, Lecture2

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Anton Kasyanov, Introduction to Python, Lecture2

Introduction to Python

Lecture 2Kasyanov Anton

IASA 2011

Page 2: Anton Kasyanov, Introduction to Python, Lecture2

Plan

Variables Types Functions If operator

Page 3: Anton Kasyanov, Introduction to Python, Lecture2

Variables

A variable is a name that refers to a value. Variables let us store and reuse values in

several places. Python is dynamically typed. That means that

we don't need to specify variable's type.

Page 4: Anton Kasyanov, Introduction to Python, Lecture2

Variables, assignment

Form: variable = expression An expression is a legal sentence in python that

can be evaluated. So we will put math expressions into the shell and

seen them be evaluated to single numbers.

What it does: 1. Evaluate the expression on the RHS.(This value

is a memory address) 2. Store the memory address in the variable.

Page 5: Anton Kasyanov, Introduction to Python, Lecture2

Variables, assignment

What this means is that a variable is a name and a memory address. The name points to a memory address where the value is stored.

Page 6: Anton Kasyanov, Introduction to Python, Lecture2

Practice

Let's play with python shell

Page 7: Anton Kasyanov, Introduction to Python, Lecture2

Functions

Useful but simple to create. First let's think about what it means to define a

function in math. Consider f(x)=x^2.

In python we can do the same with: def f(x):

return x**2

Page 8: Anton Kasyanov, Introduction to Python, Lecture2

Functions

A function definition has the form:

def function_name(parameters):block

def is a python keyword; it cannot be used for naming functions or variables.

A parameter of a function is a variable. A function can have any number of parameters, including 0.

A block is a sequence of legal python statements. A block must be indented.

If the block contains the keyword return, it returns a value; otherwise it returns the special value None.

Page 9: Anton Kasyanov, Introduction to Python, Lecture2

Functions documentation

We can use the built-in function help() to get information on functions or modules.

We can do this on functions that we've defined as well, but it doesn't give much information.

We can add useful documentation with docstrings. A docstring is surrounded by ''' and must be the first

line of a module or function.

Page 10: Anton Kasyanov, Introduction to Python, Lecture2
Page 11: Anton Kasyanov, Introduction to Python, Lecture2

Docstrings

If the first line of a function or module is a string, we call it a docstring. Short for documentation string.

Python saves the string to return if the help function is called.

Convention: Leave a blank line after but not before a docstring.

All functions should have docstrings.

Page 12: Anton Kasyanov, Introduction to Python, Lecture2
Page 13: Anton Kasyanov, Introduction to Python, Lecture2

Naming conventions

Naming rules and conventions apply to functions, variables and any other kind of name that you will see.

Must start with a letter or underscore. Can include letters, numbers, and underscores

and nothing else. Case matters, so age is not same name as

Age.

Page 14: Anton Kasyanov, Introduction to Python, Lecture2

Name conventions

For variables and functions pothole_case is used variable_name, useful_function

CamelCase is sometimes used, but not for variables and functions MyClass

Page 15: Anton Kasyanov, Introduction to Python, Lecture2

Types

Every variable has a type Use built-in function type() Type converting is available

Page 16: Anton Kasyanov, Introduction to Python, Lecture2

Booleans

Can have two values True, False. Have three operations: not, and, or. not changes a True to a False and vice versa. and returns False unless all the arguments are

True. or returns True unless all the arguments are

False.

Page 17: Anton Kasyanov, Introduction to Python, Lecture2

Booleans

We can use relational operators. <,>,<=,>=,!=, ==

These are all comparison operators that return True or False.

== is the equality operator. != is not equals.

Page 18: Anton Kasyanov, Introduction to Python, Lecture2

Practice

Let's use Python shell and play with types.

Page 19: Anton Kasyanov, Introduction to Python, Lecture2

If statement

The general form of an if statement is:

if condition:

block

Example:

if grade >=50:

print “pass”

Page 20: Anton Kasyanov, Introduction to Python, Lecture2

If statement

The general form of an if statement is:

if condition:

block

The condition is a boolean expression. Recall that a block is a series of python

statements. If the condition evaluates to true the block is

executed.

Page 21: Anton Kasyanov, Introduction to Python, Lecture2

If statement

If we want to execute different lines of code based on the outcome of the boolean expression we can use:

if condition:

block

else:

block

The block under the else is executed if the condition evaluates to false.

Page 22: Anton Kasyanov, Introduction to Python, Lecture2

Elif

if condition1:

block

elif condition2:

block

elif condition3:

block

else:

block

Page 23: Anton Kasyanov, Introduction to Python, Lecture2

Practice

Changing photo to make it look like it's sunset using Python.

Sunset is when red color is main on the photo. Let's decrease blue and green components of

each pixel. We will use Python Imaging Library (PIL).

Page 24: Anton Kasyanov, Introduction to Python, Lecture2

Workflow

Load an image Get it's size Step through all pixels

Get color of this pixel Change it Put it back

Save the image

Page 25: Anton Kasyanov, Introduction to Python, Lecture2

Home assignment

Create all following functions: xor(bool, bool) – returns result of xor operation distance(int, int, int, int) – returns (float)

distance between (x1,y1) and (x2, y2) percent(int, int) - if a<b then returns (int)

percent of a according to b, else returns -1

Page 26: Anton Kasyanov, Introduction to Python, Lecture2

Home assignment

Send to [email protected] due to 17.10 (Monday)

Just functions.py file First line is import math Math.sqrt() is useful

Page 27: Anton Kasyanov, Introduction to Python, Lecture2

Any questions?