+ learn it 2014: python basic programming. + agenda python, pycharm ce basic programming concepts...

40
+ Learn IT 2014: Python Basic programming

Upload: kristopher-clarke

Post on 24-Dec-2015

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+

Learn IT 2014: Python

Basic programming

Page 2: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Agenda

Python, PyCharm CE

Basic programming concepts

Creating an executable file using Py2Exe

Where to go from here

Page 3: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Python

Python version 2.7.6 (an interpreter)

Py2Exe version 0.6.9 win32-py2.7.exe

PyCharm Community Edition (an IDE)

Page 4: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Python

Easy to learn (simple syntax)

High-level scripting language

Available on PC, Mac, Unix and Linux systems

Drawing shapes using Turtle Module

Create web apps, games, or search engine (such as Google) Lists of applications developed in Python

https://wiki.python.org/moin/Applications

Page 5: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Programming Basic Elements

Comments

Data Types, Variables and Variables Declaration

Functions

Turtle

Loops

Page 6: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Comments

Are used to describe the meaning and purpose of your source code or program

Can be thought of as notes for ourselves and others

Are ignored by compilers and interpreters

Should be included in all programs

Page 7: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Comments

Types Examples

Single # This is a single line comment.

Multiline # This is multiline comments# comments line 2…

Page 8: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Programming Basic Elements

Comments

Data Types, Variables and Variables Declaration

Functions

Turtle

Loops

Page 9: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Data Types

Common data types: String – letters, numbers, and symbols

name = “Ros” Numbers – integer and float

a = 1 # integer or int b = 3.5 # float

Boolean – True or False x = True y = False

List – array in other languages a_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

Page 10: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Variables & Variables Declaration

Used to store value or a piece of data

Declaring variables:Should be something meaningfulCan start with alphabet, and underscore but not

numberCase sensitive, i.e., name is not the same as Name

Page 11: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Examples

Data Types Variable Declaration Explanations

String my_str = “Ros” my_str = variable;“Ros” = string value (notes the use of quotations)

Integer my_int = 90Float my_float = 90.87

Boolean my_bool = True

my_bool2 = False

True is a value (note the use of an uppercase T and F)

List my_list = []

my_list2 = [1, 2, 3]

my_list = varialbe[] = an empty list, which you can use append to add to the list

[1, 2, 3] = a list with some values

Page 12: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Comments and Variables Example

Page 13: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Programming Basic Elements

Comments

Data Types, Variables and Variables Declaration

Functions

Turtle

Loops

Page 14: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Function

A building block of code or a chunk of code that can be reused

To define a function in Python, we used a keyword “def”

Example

Page 15: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Function in Python

Language Defining a function Explanations

Python def drawline(): # your code here # your code here

drawline()

colon (:) tells Python that a block of code iscoming next

block of code(recommended 4 spaces indentation)

calling drawline() function

Page 16: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Programming Basic Elements

Comments

Data Types, Variables and Variables Declaration

Functions

Turtle

Loops

Page 17: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Turtle

Turtle (cursor) graphics is a Python graphic package for creating vector graphics (scalable graphics)

Turtle graphics module is included in Python

Three things we need to know: Positions (distance) Directions (angle) States of the pens (line color, shape filled, line width,

etc.)

Page 18: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Directions in Turtle

Facing Left Facing Right

Page 19: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Degrees in Turtle

0

90

180

270

45

135225

315

Page 20: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Turtle Commands

Commands Descriptions

forward() Moves the turtle forward

left() Moves the turtle counter clockwise

right() Moves the turtle clockwise

penup() Picks up the turtles tail

pendown() Puts down the turtles tail

color() Changes the color of the turtle(http://www.tcl.tk/man/tcl8.4/TkCmd/colors.htm)

heading() Returns the current heading

shape() Shape of the turtle, can be arrow, classic, turtle, or circle

done() End turtle module

Visit https://docs.python.org/2/library/turtle.html for more information.

Page 21: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Drawing Square

Page 22: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Output

Page 23: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Activity

Draw other shapes like triangle, rectangle, etc.

Formula

Shape Sides Range() Angle

Triangle 3 3 360/3 = 120

Square/Rectangle 4 4 360/4 = 90

Octagon 8 8 360/8 = 45

Page 24: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Programming Basic Elements

Comments

Data Types, Variables and Variables Declaration

Functions

Turtle

Loops

Page 25: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Loops

Looping means repeating the same thing over and over again.

Two kinds of loops: For loop – repeat a certain number of times

While loop – repeat until a certain thing happens (or as long as some condition is met)

Page 26: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+For Loop

Examples For loop Outputs1 for i in [1,2,3,4]:

print i1234

2 for i in [0,1,2]: print “Hi”

HiHiHI

Explanations:• The variable i (loop index; loop counter)• The sequences in example started with the value 0 or 1 (example 1

and 2) and ended with any numbers (in example 1 and 2 the numbers were 4 and 2 respectively)

Page 27: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+For Loop

Range(start, stop, step) function is a short cut for entering starting and ending numbers

range() function returns the numbers between the start and stop

step in range() function is optional argument

An argument is a value that we put inside the parentheses and pass it to the function

Page 28: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+For Loop

Examples Outputs

1 for i in range (1, 4): print “Hello world”

Hello worldHello worldHello world

2 for i in range(2): print “Learn IT 2014!”

Learn IT 2014Learn IT 2014

Explanations:• range(1, 4) means our program will print out the words “Hello world” 3 times

starting from 1 and ending before 4, which is 3. Note, step is optional. In this case we didn’t specify step so it will count by 1

• If you want to print out 5 Hello world, how would you do that using range(n1, n2)?

• Example 2 showed another way of specifying a number of loops you want using just range(n)

• range(2) in example 2 is the same thing as range(0, 1)

Page 29: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+For Loop

Examples for i in range (start, stop, step): Outputs

1. Counting by 2 for i in range(1, 10, 2): print "i =", i

i = 1i = 3i = 5i = 7i = 9

2. Counting by 5 for i in range(1, 10, 5): print "i =", i

i = 1i = 6

3. Counting backward for i in range(5, 0, -1): print "i =", i

i = 5i = 4i = 3i = 2i = 1

Using “step” examples

Page 30: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+For Loop

We can also use a for loop to count something else other than numbers

Example for i in [item, item, item, so on]: Outputs

for i in [“coffee”, “hot chocolate”, “tea”]: print “I like to drink ” + i + “.”

I like to drink coffee.I like to drink hot chocolate.I like to drink tea.

Page 31: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+While Loop While loop is used to repeat a block of codes an unknown number

of times until a specific condition is met (conditional loop)

While loop is similar to if-else condition

(Code from http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/whilestatements.html)

Lines While loop Explanations

12345678

temperature = 115 # set initial conditionwhile temperature > 112: # first while loop print temperature temperature = temperature – 1

print “The tea is cool enough.”

115 > 112Print 115115 – 1 = 114, loop backuntil 112 > 112, which is false

Print “The tea is cool enough.”

What happen if you leave out line 4?

Page 32: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Activity

Draw a square using a for loop

Formula

Shape Sides Range() Angle

Triangle 3 3 360/3 = 120

Square/Rectangle 4 4 360/4 = 90

Octagon 8 8 360/8 = 45

Page 33: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Square Code

Page 35: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Py2Exe

Create setup.py in the same folder where you want your python script to be executable

Example:

#setup.py

from distutils.core import setup

import py2exe

setup(console=[‘yourpythonfile.py'])

Page 36: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Creating an EXE File

Go to Start and type “cmd” and press enter to open a command prompt

Note: Visit, http://www.pythoncentral.io/py2exe-python-to-exe-introduction/ for more information

Page 37: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Creating an EXE File

Navigate to the folder that we want to create a python file into an executable file

Example:

1. Create a directory called, “LearnIT2014” under C drive and copy the python file you

want to create an executable file to that directory

2. In the command prompt window, type the following command to change the directory

to LearnIT2014

c:\Users\n10rxa1> cd\

c:\>

c:\>cd LearnIT2014

3. This will take you to c:\LearnIT2014

4. Type the following command after c:\LearnIT2014>

c:\LearnIT2014>setup.py py2exe

5. When it is done creating, you will see two subfolders called “build” and “dist” created

under LearnIT2014

6. Double click “dist” folder to open, you should see an exe file

7. The “dist” folder is the folder that you can pack and send it your friends and family

Page 38: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Where to go from here

Python books: lists of free books about Pythonhttp://pythonbooks.revolunet.com/

Hello World Book http://www.manning.com/sande2/

How to think like a computer scientist http://www.openbookproject.net/thinkcs/python/english2e/

Python for Kids http://www.nostarch.com/pythonforkids

Page 40: + Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go

+Happy Coding