Introduction to Python

advertisement
Introduction to Python
Dr. Bernard Chen Ph.D.
University of Central Arkansas
July 9th 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
What can I do with Python?





System Programming
GUIs
Internet Scripting
Database Programming
Games, Images, AI, XML and more
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
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.
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
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)
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
Hello World Program

Implement by three different languages



In C
In JAVA
In Python
“Hello World” in C
main()
{
printf("hello, world\n");
}
“Hello World” in JAVA
class myfirstjavaprog
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
“Hello World” in Python
print (“Hello World!!”)
Be familiar with Python
>>> print ("Hello, World! ")
Hello, World!
>>> 10 + 25
35
>>> 124 – 125
-1
>>> 1/3
0.333333333333333
Some Python commands


Python command print – Displays
data, values, and expressions
The single and double quotation mark
string objects, which are collections of
texts surrounded by quotes.
Be familiar with Python
>>> print ("Hello, World! ")
Hello, World!
>>> "hello"
'hello'
>>> "world"
'world'
>>> "hello"+"world"
'helloworld'
Be familiar with Python
>>> "hello" * 3
'hellohellohello'
>>> "hello" * 300
Declare a variable and assign its
value Example: Area of a
rectangle
>>> width = 20
>>> height = 45
>>> area = width * height
>>> area
900
What are the outputs? Why?
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
a=3
b=4
a+1
b*3
b/3
b/3.0
b**2
2+4.0
2.0**b
How do you run programs?
Three different methods:



1. Interactive Coding
2. Files (such as NotePad, WordPad)
3. Integrated Development Environment
(IDE)
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)
Your first Python programhello.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)
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( ) function
3. print('What is your name?') # call the print( )
function
4. myName = input() # call the input( ) function
5. print('It is good to meet you, ' + myName)
Download