Python Programming: An Introduction to Computer Science Chapter 1 Computers and Programs
The Magic of Python When you start Python, you will see something like: Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>>
The Magic of Python  The “>>>” is a Python prompt indicating that Python is ready for us to give it a command. These commands are called statements.  >>> print("Hello, world“) Hello, world >>> print(2+3) 5 >>> print("2+3=", 2+3) 2+3= 5 >>>
The Magic of Python  Usually we want to execute several statements together that solve a common problem. One way to do this is to use a function.  >>> def hello(): print("Hello") print("Computers are Fun") >>>
The Magic of Python  >>> def hello(): print("Hello") print("Computers are Fun") >>>  The first line tells Python we are defining a new function called hello.  The following lines are indented to show that they are part of the hello function.  The blank line (hit enter twice) lets Python know the definition is finished.
The Magic of Python  >>> def hello(): print("Hello") print("Computers are Fun") >>>  Notice that nothing has happened yet! We’ve defined the function, but we haven’t told Python to perform the function!  A function is invoked by typing its name.  >>> hello() Hello Computers are Fun >>>
The Magic of Python  What’s the deal with the ()’s?  Commands can have changeable parts called parameters that are placed between the ()’s.  >>> def greet(person): print("Hello",person) print ("How are you?") >>>
The Magic of Python  >>> greet("Terry") Hello Terry How are you? >>> greet("Paula") Hello Paula How are you? >>>  When we use parameters, we can customize the output of our function.
The Magic of Python  When we exit the Python prompt, the functions we’ve defined cease to exist!  Programs are usually composed of functions, modules, or scripts that are saved on disk so that they can be used again and again.  A module file is a text file created in text editing software (saved as “plain text”) that contains function definitions.  A programming environment is designed to help programmers write programs and usually includes automatic indenting, highlighting, etc.
The Magic of Python # File: chaos.py # A simple program illustrating chaotic behavior def main(): print("This program illustrates a chaotic function") x = eval(input("Enter a number between 0 and 1: ")) for i in range(10): x = 3.9 * x * (1 - x) print(x) main()  We’ll use filename.py when we save our work to indicate it’s a Python program.  In this code we’re defining a new function called main.  The main() at the end tells Python to run the code.
Inside a Python Program # File: chaos.py # A simple program illustrating chaotic behavior  Lines that start with # are called comments  Intended for human readers and ignored by Python  Python skips text from # to end of line
Inside a Python Program def main():  Beginning of the definition of a function called main  Since our program has only this one module, it could have been written without the main function.  The use of main is customary, however.
Inside a Python Program print("This program illustrates a chaotic function")  This line causes Python to print a message introducing the program.
Inside a Python Program main()  This last line tells Python to execute the code in the function main
1. Write a program which prints the following information about at least 5 persons: `Full Name ‘ `Email-Address’ ` Telephone Number’ use print and println and see the difference. Some Assignments 
Program, Assignment2 ______ / / / ______/ / ______/ +--------+ ______ / / | STOP | / ______/ ______ / / +--------+ The structure of the output: initial "egg" figure second "teacup" figure third "stop sign" figure fourth "hat" figure This structure can be represented by methods: drawEgg drawTeaCup drawStopSign drawHat
Program, Assignment3

Chapter01_Python.ppt

  • 1.
    Python Programming: An Introductionto Computer Science Chapter 1 Computers and Programs
  • 2.
    The Magic ofPython When you start Python, you will see something like: Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>>
  • 3.
    The Magic ofPython  The “>>>” is a Python prompt indicating that Python is ready for us to give it a command. These commands are called statements.  >>> print("Hello, world“) Hello, world >>> print(2+3) 5 >>> print("2+3=", 2+3) 2+3= 5 >>>
  • 4.
    The Magic ofPython  Usually we want to execute several statements together that solve a common problem. One way to do this is to use a function.  >>> def hello(): print("Hello") print("Computers are Fun") >>>
  • 5.
    The Magic ofPython  >>> def hello(): print("Hello") print("Computers are Fun") >>>  The first line tells Python we are defining a new function called hello.  The following lines are indented to show that they are part of the hello function.  The blank line (hit enter twice) lets Python know the definition is finished.
  • 6.
    The Magic ofPython  >>> def hello(): print("Hello") print("Computers are Fun") >>>  Notice that nothing has happened yet! We’ve defined the function, but we haven’t told Python to perform the function!  A function is invoked by typing its name.  >>> hello() Hello Computers are Fun >>>
  • 7.
    The Magic ofPython  What’s the deal with the ()’s?  Commands can have changeable parts called parameters that are placed between the ()’s.  >>> def greet(person): print("Hello",person) print ("How are you?") >>>
  • 8.
    The Magic ofPython  >>> greet("Terry") Hello Terry How are you? >>> greet("Paula") Hello Paula How are you? >>>  When we use parameters, we can customize the output of our function.
  • 9.
    The Magic ofPython  When we exit the Python prompt, the functions we’ve defined cease to exist!  Programs are usually composed of functions, modules, or scripts that are saved on disk so that they can be used again and again.  A module file is a text file created in text editing software (saved as “plain text”) that contains function definitions.  A programming environment is designed to help programmers write programs and usually includes automatic indenting, highlighting, etc.
  • 10.
    The Magic ofPython # File: chaos.py # A simple program illustrating chaotic behavior def main(): print("This program illustrates a chaotic function") x = eval(input("Enter a number between 0 and 1: ")) for i in range(10): x = 3.9 * x * (1 - x) print(x) main()  We’ll use filename.py when we save our work to indicate it’s a Python program.  In this code we’re defining a new function called main.  The main() at the end tells Python to run the code.
  • 11.
    Inside a PythonProgram # File: chaos.py # A simple program illustrating chaotic behavior  Lines that start with # are called comments  Intended for human readers and ignored by Python  Python skips text from # to end of line
  • 12.
    Inside a PythonProgram def main():  Beginning of the definition of a function called main  Since our program has only this one module, it could have been written without the main function.  The use of main is customary, however.
  • 13.
    Inside a PythonProgram print("This program illustrates a chaotic function")  This line causes Python to print a message introducing the program.
  • 14.
    Inside a PythonProgram main()  This last line tells Python to execute the code in the function main
  • 15.
    1. Write aprogram which prints the following information about at least 5 persons: `Full Name ‘ `Email-Address’ ` Telephone Number’ use print and println and see the difference. Some Assignments 
  • 16.
    Program, Assignment2 ______ / / / ______/ / ______/ +--------+ ______ / / | STOP | / ______/ ______ / / +--------+ The structure of the output: initial "egg" figure second "teacup" figure third "stop sign" figure fourth "hat" figure This structure can be represented by methods: drawEgg drawTeaCup drawStopSign drawHat
  • 17.