Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012...

22
Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

Transcript of Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012...

Page 1: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

Introduction to Python

Dr. Bernard Chen Ph.D.University of Central Arkansas

July 9th 2012 CS4HS@UCA

Page 2: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

Why do people use Python? Software Quality: Python is designed to be readable,

and hence maintainable. Developer productivity: Python code is typically 1/3 to 1/5

the size of equivalent C++ or JAVA code

Page 3: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

What can I do with Python?

System Programming GUIs Internet Scripting Database Programming Games, Images, AI, XML and more

Page 4: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

What are Python’s Technical Strength

It’s OO It’s free It’s Portable It’s Powerful It’s Easy to use It’s Easy to learn

Page 5: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

What is the Downside of Python?

Perhaps the only downside to Python is that the execution speed may not always as fast as compiled languages such as C and C++

Python is not compiled all the way down to binary machine code, it compiled to byte code instead.

Page 6: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

Who Uses Python Today?

Google and Yahoo currently use Python in Internet service

IBM use Python for hardware testing Industrial Light and Magic use Python

in the production of movie animation For more details, visit

www.python.org

Page 7: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

Install Python Go to

http://www.python.org/download/releases/3.2/ and download Python3.2, then select Windows x86 MSI Installer (3.2) or Windows X86-64 MSI Installer (3.2)

Page 8: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

Install Python Or you can search for Python, choose

“Download” and then select Python 3.2 Windows x86 MSI Installer or Python 3.2 Windows X86-64 MSI Installer

Page 9: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

Hello World Program

Implement by three different languages In C In JAVA In Python

Page 10: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

“Hello World” in C

main() { printf("hello, world\n"); }

Page 11: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

“Hello World” in JAVA

class myfirstjavaprog { public static void main(String args[]) { System.out.println("Hello World!"); }}

Page 12: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

“Hello World” in Python

print (“Hello World!!”)

Page 13: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

Be familiar with Python>>> print ("Hello, World! ")Hello, World!

>>> 10 + 2535

>>> 124 – 125-1

>>> 1/30.333333333333333

Page 14: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

Python command print – Displays data, values, and expressions

The single and double quotation mark string objects, which are collections of texts surrounded by quotes.

Some Python commands

Page 15: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

Be familiar with Python>>> print ("Hello, World! ")Hello, World!

>>> "hello"'hello'

>>> "world"'world'

>>> "hello"+"world"'helloworld'

Page 16: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

Be familiar with Python

>>> "hello" * 3'hellohellohello'

>>> "hello" * 300

Page 17: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

>>> width = 20

>>> height = 45

>>> area = width * height

>>> area900

Declare a variable and assign its value Example: Area of a rectangle

Page 18: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

>>> a=3>>> b=4>>> a+1>>> b*3>>> b/3>>> b/3.0>>> b**2>>> 2+4.0>>> 2.0**b

What are the outputs? Why?

Page 19: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

How do you run programs?

Three different methods:

1. Interactive Coding 2. Files (such as NotePad,

WordPad) 3. Integrated Development

Environment (IDE)

Page 20: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

Create and run a program in the Python IDLE

From File -> New Window

Type your program print ("Hello, World! ")

Save your program (Give a name you like, such as test.py)

Run the program (by selecting the Run Module or pressing the F5 key)

Page 21: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

Your first Python program- hello.py

# This program says hello and asks for my name.

hello = 'Hello world! 'print(hello)print('What is your name?')myName = input()print('It is good to meet you, ' + myName)

Page 22: Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA.

hello.py executed sequentially

# This program says hello and asks for my name.Any text following a # sign is a comment. Comments

are not for the computer, but for you, the programmer. 

1. hello = 'Hello world! ' # assign the string to a name

2. print(hello) # call the print( ) function3. print('What is your name?') # call the print( )

function4. myName = input() # call the input( ) function5. print('It is good to meet you, ' + myName)