Programming in Python Part I - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Programming in Python Part I

Description:

write a program in a file and use the interpreter to execute the contents of the file. ... python hello.py. First Program: 'Hello World' in the Python. print ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 25
Provided by: ef129204
Category:

less

Transcript and Presenter's Notes

Title: Programming in Python Part I


1
Programming in Python Part I
  • Dr. Fatma Cemile Serçe
  • Atilim University
  • 2009-2010

2
The Python Programming Language
  • High-level language (like C, C, Perl, Java,
    etc.)
  • Interpreted language
  • Python programs executed by an interpreter
  • Two ways to use the interpreter
  • command-line mode
  • script mode

3
The Python Programming Language
  • In Command-line Mode
  • type Python programs and the interpreter prints
    the result
  • The first line of this example is the command
    that starts the Python interpreter.
  • The next two lines are messages from the
    interpreter
  • The third line starts with gtgtgt, prompt the
    interpreter uses to indicate it is ready
  • Type print (11) program, and interpreter replied
    2

4
The Python Programming Language
  • In Script Mode
  • write a program in a file and use the interpreter
    to execute the contents of the file. The file is
    called a script
  • file name ends with .py
  • Ex use text editor to create a file
  • named hello.py.
  • Then tell interpreter the name of the script
  • python hello.py

5
First Program Hello World
  • in the Python
  • print ("Hello, World! ")
  • in the C
  • include ltstdio.hgt
  • int main(void)
  • printf("hello, world\n")
  • return 0
  • in the C
  • include ltiostream.hgt
  • void main()
  • cout ltlt "Hello, world." ltlt endl
  • in the Java
  • public class HelloWorld
  • public static void main(String args)
  • System.out.println(Hello, World!)

6
Values and Types
  • A value is one of the fundamental thingslike a
    letter or a numberthat a program manipulates
  • Ex
  • 2
  • Hello, World!
  • These values are belongs to different types
  • 2 integer
  • Hello, World! string

7
Values and Types
  • If you are not sure what type a value has, the
    interpreter can tell you
  • gtgtgt type(Hello, World!)
  • ltclass strgt
  • gtgtgt type(17)
  • ltclass intgt
  • gtgtgt type(3.2)
  • ltclass floatgt
  • str-gtString, int-gtInteger, float-gtfloating-point

8
Exercise 1
  • What about values like 17 and 3.2?
  • gtgtgt type(17)
  • lttype strgt
  • gtgtgt type(3.2)
  • lttype strgt
  • Theyre strings.

9
Exercise 2
  • What is the output?
  • gtgtgt print (1,000,000)
  • 1.000.000 ? NO
  • 1 0 0 ? YES
  • a semantic error the code runs without producing
    an error message, but it doesnt do the right
    thing.

10
Variables
  • A variable is a name that refers to a value
  • The assignment statement creates new variables
    and gives them values
  • gtgtgt message Hello, World!
  • gtgtgt n 17
  • gtgtgt pi 3.14159

11
Variables (cont.)
  • The print statement also works with variables
  • gtgtgt print (message)
  • Hello, World!
  • gtgtgt print (n)
  • 17
  • gtgtgt print (pi)
  • 3.14159

12
Variables (cont.)
  • Variables also have types again, we can ask the
    interpreter what they are
  • gtgtgt type(message)
  • lttype strgt
  • gtgtgt type(n)
  • lttype intgt
  • gtgtgt type(pi)
  • lttype floatgt
  • The type of a variable is the type of the value
    it refers to.

13
Variable names and keywords
  • choose meaningful names
  • both letters and numbers, but begin with a letter
  • Message and message are different (use lowercase
    by convention)
  • use underscore character (_) in names with
    multiple words
  • person_name

14
Variable names and keywords
  • If you give a variable an illegal name, you get a
    syntax error
  • gtgtgt 76tables seventy six tables
  • SyntaxError invalid syntax
  • gtgtgt more 1000000
  • SyntaxError invalid syntax
  • gtgtgt class COMPE 111
  • SyntaxError invalid syntax
  • 76trombones is illegal because it does not begin
    with a letter.
  • more is illegal because it contains an illegal
    character, the dollar sign
  • But whats wrong with class? It turns out that
    class is one of the Python keywords.

15
Variable names and keywords
  • Keywords define the languages rules and
    structure
  • Keywords cannot be used as variable names
  • Python has twenty-nine keywords

16
Statements
  • A statement is an instruction that the Python
    interpreter can execute
  • print and assignment
  • The result of a print statement is a value.
  • Assignment statements dont produce a result.
  • A script usually contains a sequence of
    statements.
  • Ex the script
  • print (1)
  • x 2
  • print (x)
  • produces the output
  • 1
  • 2

17
Operators and Operands
  • Operators are special symbols that represent
    computations like addition and multiplication
  • The values the operator uses are called operands
  • 2032
  • hour-1
  • hour60minute
  • minute/60
  • 52
  • (59)(15-7)

The symbols , -, and /, and the use of
parenthesis for grouping, mean in Python what
they mean in mathematics The asterisk () is the
symbol for multiplication is the symbol for
exponentiation modulo
18
Operators and Operands(cont.)
  • When a variable name appears in the place of an
    operand, it is replaced with its value before the
    operation is performed
  • gtgtgt minute 59
  • gtgtgt minute/60
  • 0.983888

19
Order of Operations
  • When more than one operator appears in an
    expression, the order of evaluation depends on
    the rules of precedence.
  • Python follows the same precedence rules for its
    mathematical operators that mathematics does.
  • The acronym PEMDAS is a useful way to remember
    the order of operations

20
Order of Operations
  • PEMDAS
  • Parentheses have the highest precedence
  • 2 (3-1) is 4, and (11)(5-2) is 8
  • Exponentiation has the next highest precedence,
  • 211 is 3 and not 4, and 313 is 3 and not 27
  • Multiplication and Division have the same
    precedence, which is higher than Addition and
    Subtraction
  • 23-1yields 5 rather than 4, and 2/3-1 is -1, not
    1
  • Operators with the same precedence are evaluated
    from left to right.
  • 6100/60 yields 10

21
Operations on Strings
  • In general, you cannot perform mathematical
    operations on strings, even if the strings look
    like numbers
  • The following are illegal
  • message-1
  • Hello/123
  • messageHello
  • 152
  • operator work with strings. It does
    concatenation, means joining the two operands by
    linking them end-to-end
  • fruit banana
  • bakedGood nut bread
  • print fruit bakedGood
  • Output banana nut bread
  • operator also works on strings it performs
    repetition.
  • Fun3 is FunFunFun

22
Warning!
  • There are limits on where you can use certain
    expressions.
  • For example, the left-hand side of an assignment
    statement has to be a variable name, not an
    expression.
  • The following is illegal
  • minute1 hour

23
Comments
  • Notes to your programs to explain in natural
    language what the program is doing, called
    comments, and they are marked with the symbol
  • Everything from the to the end of the line is
    ignoredit has no effect on the program
  • compute the percentage of the hour that has
    elapsed
  • percentage (minute100)/60 cautioninteger
    division

24
The Keyboard Input
  • Python provides built-in functions that get input
    from the keyboard.
  • The simplest is called input.
  • the program stops and waits for the user to type
    something
  • when the user presses Return or the Enter key,
    the program resumes and raw input returns what
    the user typed as a string
  • gtgtgt name input ("What...is your name? ")
  • What...is your name? F. Cemile Serce
  • gtgtgt print (name)
  • F. Cemile Serce
  • gtgtgt age input (how old are you?)
  • print (10 int(age))
  • gtgtgt how old are you?30
  • 40
Write a Comment
User Comments (0)
About PowerShow.com