An Introduction to Python - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

An Introduction to Python

Description:

Programming Workshop #1. 4/15/04. Introduction to Python. 3. Programming Language and Development Software. In this program, we'll use Python ... – PowerPoint PPT presentation

Number of Views:133
Avg rating:3.0/5.0
Slides: 28
Provided by: lanct
Category:

less

Transcript and Presenter's Notes

Title: An Introduction to Python


1
An Introduction to Python
  • Dr. Nancy Warter-Perez
  • April 15, 2004

2
Overview
  • Overview of program development
  • Python Basics
  • Python Types and Operators
  • Numbers and Arithmetic operators
  • Strings
  • Lists
  • Dictionaries
  • Input Output
  • Example amino acid search program
  • Programming Workshop 1

3
Programming Language and Development Software
  • In this program, well use Python
  • Interpretive Language
  • Development software
  • IDLE python gui
  • Pythonwin (recommended)
  • Do your work on either the hard disk or zip disk
    (not floppy disk, A drive too slow!)

4
Program Development
Problem solving
Problem specification
Algorithm design
Test by hand
Code in target language
Implementation
Test code / debug
Program
5
Python Basics - Comments
  • Python comments
  • line comment
  • Header comments
  • Description of program
  • Written by
  • Date created
  • Last Modified

6
Python Basics - Variables
  • Python variables are not declared.
  • To assign a variable, just type
    identifierliteral
  • Identifiers
  • Have the following restrictions
  • Must start with a letter or underscore (_)
  • Case sensitive
  • Must consist of only letters, numbers or
    underscore
  • Must not be a reserved word (LP pg 137)
  • Have the following conventions
  • All uppercase letters are used for constants
  • Variable names are meaningful thus, often
    multi-word
  • Convention 1 alignment_sequence
  • Convention 2 AlignmentSequence
  • Python specific conventions
  • Avoid _X, __X__, __X, _, (LP pg 138)

7
Numbers
  • Numbers
  • Normal Integers represent whole numbers
  • Ex 3, -7, 123, 76
  • Long Integers unlimited size
  • Ex 9999999999999999999999L
  • Floating-point represent numbers with decimal
    places
  • Ex 1.2, 3.14159,3.14e-10
  • Octal and hexadecimal numbers
  • Ex O177, 0x9ff, Oxff
  • Complex numbers
  • Ex 34j, 3.04.0j, 3J

8
Python Basics arithmetic operations
Operators Example
  • add
  • - subract
  • multiply
  • / divide
  • modulus/remainder

y5 z3 x y z x y z x y z x y
/ z x y z
x 8 x 2 x 15 x 1 x 2
9
Python Basics arithmetic operations
Operators Example
  • ltlt shift left
  • gtgt shift right
  • raise to power

y5 z3 x y ltlt 1 x y gtgt 2 x y z
x 10 x 1 x 125
10
Python Basics Relational and Logical Operators
  • Relational operators
  • equal
  • !, ltgt not equal
  • gt greater than
  • gt greater than or
  • equal
  • lt less than
  • lt less than or equal
  • Logical operators
  • and and
  • or or
  • not not

11
Python Basics Relational Operators
  • Assume x 1, y 4, z 14

12
Python Basics Logical Operators
  • Assume x 1, y 4, z 14

13
Strings
  • Enclosed in single or double quotes
  • Ex Hello! , Hello!, 3.5, a, a
  • Sequence of charactersmystringhello world!
  • mystring0 -gt h mystring1 -gt e
  • mystring2 -gt l mystring-1 -gt !

-1 is last, -2 next to last, etc
14
String operations
15
Strings (2)
  • substrings can be reassignedmystringspoonsmy
    string5!mystring -gt spoon!
  • slicingmystring2 -gt oon!mystring3 -gt
    spo note last element is never
    included!mystring13-gt po

16
Strings (3)
  • operatorsort of fill in the blanks
    operationmystrings has n marbles
    (John,35) mystring -gt John has 35 marbles
  • s replace with string
  • n,i replace with integer
  • f replace with float

17
Lists
18
Tuples
  • Tuples sequence of valueslike lists, but
    cannot be changed after it is createdmytuple(1,2
    ,3,4)mytuple(1,a,bc,3,87.2)mytuple13
  • Used when you want to pass several variables
    around at once

19
Dictionaries
  • Dictionaries map keys to values
  • like lists, but indices can be of any type
  • Also, keys are in no particular order
  • Egmydictb3, a4, 752.85mydictb -gt
    3mydict75 -gt 2.85mydicta -gt 4

20
Dictionaries
21
Dictionaries other considerations
  • Slicing not allowed
  • Referencing invalid key is an error
  • gtgtgt mydict8.5 8, 'a' 75, 'r' 1, 'g' 2, 'y'
    3.5, 9 'nine'
  • gtgtgt mydict"red"
  • Traceback (most recent call last) File
    "ltinteractive inputgt", line 1, in ?
  • KeyError 'red
  • Use mydict.get(red) instead, it returns None if
    key is not found

22
Input/Output
  • Function raw_input() designed to read a line of
    input from the user
  • 1 optional argument string to prompt user
  • If int or float desired, simply convert string
  • int(mystring)-gtconvert to int (if possible)
  • float(mystring)-gtconvert to float (if possible)

gtgtgt mystrraw_input("Enter a string") Enter a
stringHello World! gtgtgt mystr 'Hello World!'
23
Output
  • Function print
  • Prints each argument, followed by space
  • After all arguments, prints newline
  • Put comma after last arg to prevent newline
  • add strings to avoid spaces
  • print a,b,c
  • a b c
  • print a,b,c,
  • a b c
  • print abc
  • abc

24
Output Example
  • gtgtgt print "hello","world"print "hello","again"
  • hello world
  • hello again
  • gtgtgt print "hello","world",print "hello","again"
  • hello world hello again
  • gtgtgt print "hello s world" "cold and cruel"
  • hello cold and cruel world
  • gtgtgt print "hello","cold" " "
    "and","cruel","world"
  • hello cold and cruel world

25
Creating a Python Program
  • Enter your program in the editor
  • Notice that the editor has a color coding
  • Comments
  • Key words
  • Etc
  • Also notice that it automatically indents
  • Dont override!! this is how python tells when
    block statements end!
  • If doesnt indent to proper location indicates
    bug

26
Running your Program
  • To build your program
  • Under File-gtRun
  • Select No Debugging in the drop-down window
  • Fix any errors, then run again

27
Programming Workshop 1
  • Write a Python program to compute the
    hydrophobicity of an amino acid
  • Program will prompt the user for an amino acid
    and will display the hydrophobicity
Write a Comment
User Comments (0)
About PowerShow.com