0% found this document useful (0 votes)
9 views

Programs (Or Scripts) : - Print

Programs allow users to write Python code in files called scripts. Scripts allow users to include multiple statements and take user input using functions like raw_input. Simple scripts can declare variables, perform math operations, and print output to demonstrate basic programming concepts. Comments in scripts help explain the programmer's decisions to users.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Programs (Or Scripts) : - Print

Programs allow users to write Python code in files called scripts. Scripts allow users to include multiple statements and take user input using functions like raw_input. Simple scripts can declare variables, perform math operations, and print output to demonstrate basic programming concepts. Comments in scripts help explain the programmer's decisions to users.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Programs

(or scripts)
While we can type expressions directly to a Python interpreter (for example using an interface such as an IDLE shell), in general we will want to include statements in a program le Execu@ng an expression from a script will not produce any output; for that we need statements (not expressions), such as
print(ab)! print(3*3)!

Providing input
If we are going to write programs or scripts, we will need a way to incorporate input from a user. We use the Python func@on raw_input, as in:
>>> name = raw_input(Enter your name: )! Enter your name: Eric Grimson! >>> print(Are you + name + ?)! Are you Eric Grimson?!

Some simple code


One can use variable names anywhere you might use the expression whose value it holds >>> myString = Too much! >>> weather = snow! >>> print(myString + + weather)! Too much snow!

A straight line program


Suppose we type the following into a le, and load it into a Python IDLE window
x = 3! x = x*x # square value of x! print(x)! y = float(raw_input('Enter a number: '))! print(y*y)! !

Then we observe the following behavior (where I type a 4 below)


9! Enter a number: 4! 16.0!

Some observa@ons
Comments appear aLer a #
These are very valuable, as they help a user understand decisions the programmer has made in crea@ng the program Well commented code should be very readable by a user

A straight line program simply executes each statement in order, with no varia@on in order Most programs require more sophis@cated ow control

You might also like