0% found this document useful (0 votes)
10 views19 pages

Lecture 6 - Input, Processing and Output

Uploaded by

6c46mhz62j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views19 pages

Lecture 6 - Input, Processing and Output

Uploaded by

6c46mhz62j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Programming

Fundamentals
Lecture 6 - Input, Processing and Output
Outline
• Variables
• Creating Variables with Assignment Statements
• Examples
• Variable - WARNING!
• Variable Naming Rules
• Displaying Multiple Items with the print Function
• Example with explanation
• Variable Reassignment
• Example with explanation
Learning Outcomes
• You will be able to:
• Use variables appropriately to manipulate data
• Understand different data types available in python
• Learn how to display output in python
• Learn how to read data from user
Variables
• Programs usually store data in the computer’s memory and perform operations
on that data.
• A variable is a name that represents a value in the computer’s memory.
• For example, a program that calculates the sales tax on a purchase might use the
variable name tax to represent that value in memory. And a program that
calculates the distance between two cities might use the variable name distance
to represent that value in memory.
• When a variable represents a value in the computer’s memory, we say that the
variable references the value.
• Programs use variables to access and manipulate data that is stored in memory.
Creating Variables with Assignment
Statements
• You use an assignment statement to create a variable and make it reference a
piece of data. Here is an example of an assignment statement:
age = 25
• After this statement executes, a variable named age will be created and it will
reference the value 25.
• This concept is shown in Figure below. In the figure, think of the value 25 as being
stored somewhere in the computer’s memory. The arrow that points from age to
the value 25 indicates that the name age references the value.
Creating Variables with Assignment
Statements
• An assignment statement is written in the following general format:
variable = expression
• The equal sign (=) is known as the assignment operator. In the general format, variable is
the name of a variable and expression is a value, or any piece of code that results in a
value.
• After an assignment statement executes, the variable listed on the left side of the
operator will reference the value given on the right side of the operator.
• To experiment with variables, you can type assignment statements in interactive mode,
as shown here:
>>> width = 10
>>> length = 5
• The first statement creates a variable named width and assigns it the value 10. The
second statement creates a variable named length and assigns it the value 5.
Creating Variables with Assignment
Statements
• Next, you can use the print function to display the values referenced by these variables, as shown
here:
>>> print(width)
10
>>> print(length)
5
• When you pass a variable as an argument to the print function, you do not enclose the variable
name in quote marks. To demonstrate why, look at the following interactive session:
>>> print('width')
width
>>> print(width)
10
• In the first statement, you passed 'width' as an argument to the print function, and the function
printed the string width. In the second statement, you passed width (with no quote marks) as an
argument to the print function, and the function displayed the value referenced by the width
variable.
Creating Variables with Assignment
Statements
• In an assignment statement, the variable that is receiving the assignment must
appear on the left side of the = operator. As shown in the following interactive
session, an error occurs if the item on the left side of the = operator is not a
variable:
>>> 25 = age
SyntaxError: can't assign to literal
Creating Variables with Assignment
Statements – Example 1
• The code in Program below demonstrates a variable. Line 2 creates a variable
named room and assigns it the value 503. The statements in lines 3 and 4 display
a message. Notice that line 4 displays the value that is referenced by the room
variable.
Creating Variables with Assignment
Statements – Example 2
• Program below shows a sample program that uses two variables. Line 2 creates a
variable named top_speed, assigning it the value 160. Line 3 creates a variable
named distance, assigning it the value 300.
Variable - WARNING!
• You cannot use a variable until you have assigned a value to it. An error will occur if you try to
perform an operation on a variable, such as printing it, before it has been assigned a value.
• Sometimes a simple typing mistake will cause this error. One example is a misspelled variable
name, as shown here:
temperature = 74.5 # Create a variable
print(tempereture) # Error! Misspelled variable name
• In this code, the variable temperature is created by the assignment statement. The variable name
is spelled differently in the print statement, however, which will cause an error. Another example
is the inconsistent use of uppercase and lowercase letters in a variable name. Here is an example:
temperature = 74.5 # Create a variable
print(Temperature) # Error! Inconsistent use of case
• In this code the variable temperature (in all lowercase letters) is created by the assignment
statement. In the print statement, the name Temperature is spelled with an uppercase T. This will
cause an error because variable names are case sensitive in Python.
Variable Naming Rules
• Although you are allowed to make up your own names for variables,
you must follow these rules:
• You cannot use one of Python’s key words as a variable name. (See Table 1-2
for a list of the key words.)
• A variable name cannot contain spaces.
• The first character must be one of the letters a through z, A through Z, or an
underscore character (_).
• After the first character you may use the letters a through z or A through Z,
the digits 0 through 9, or underscores.
• Uppercase and lowercase characters are distinct. This means the variable
name ItemsOrdered is not the same as itemsordered.
Variable Naming Rules
• In addition to the mentioned rules, you should always choose names for your variables that give
an indication of what they are used for.
• For example, a variable that holds the temperature might be named temperature, and a variable
that holds a car’s speed might be named speed. You may be tempted to give variables names
like x and b2, but names like these give no clue as to what the variable’s purpose is.
• Because a variable’s name should reflect the variable’s purpose, programmers often find
themselves creating names that are made of multiple words. For example, consider the
following variable names:
grosspay
payrate
Hotdogssoldtoday
• Unfortunately, these names are not easily read by the human eye because the words aren’t
separated. Because we can’t have spaces in variable names, we need to find another way to
separate the words in a multiword variable name, and make it more readable to the human eye.
Variable Naming Rules
• One way to do this is to use the underscore character to represent a space. For example,
the following variable names are easier to read than those previously shown:
gross_pay
pay_rate
hot_dogs_sold_today
• This style of naming variables is popular among Python programmers. There are other
popular styles, however, such as the camelCase naming convention. camelCase names are
written in the following manner:
• The variable name begins with lowercase letters.
• The first character of the second and subsequent words is written in uppercase.
• For example, the following variable names are written in camelCase:
grossPay
payRate
hotDogsSoldToday
Displaying Multiple Items with the print
Function
• If you refer to Program above in slide 9, you will see that we used the following
two statements in lines 3 and 4:
print('I am staying in room number’)
print(room)
• We called the print function twice because we needed to display two pieces of
data. Line 3 displays the string literal 'I am staying in room number', and line 4
displays the value referenced by the room variable.
• This program can be simplified, however, because Python allows us to display
multiple items with one call to the print function.
Displaying Multiple Items with the print Function -
Example
• We simply have to separate the items with commas as shown in Program below.

• In line 3 we passed two arguments to the print function. The first argument is the
string literal 'I am staying in room number', and the second argument is the room
variable. When the print function executed, it displayed the values of the two
arguments in the order that we passed them to the function. Notice that the print
function automatically printed a space separating the two items. When multiple
arguments are passed to the print function, they are automatically separated by a
space when they are displayed on the screen.
Variable Reassignment
• Variables are called “variable” because they can reference different values while a
program is running.
• When you assign a value to a variable, the variable will reference that value until
you assign it a different value. For example, look at Program below:
Variable Reassignment – Example
Explanation
• The statement in line 3 in above slides creates a variable named dollars and assigns it the value
2.75. This is shown in the top part of Figure below. Then, the statement in line 8 assigns a
different value, 99.95, to the dollars variable. The bottom part of Figure below shows how this
changes the dollars variable. The old value, 2.75, is still in the computer’s memory, but it can no
longer be used because it isn’t referenced by a variable. When a value in memory is no longer
referenced by a variable, the Python interpreter automatically removes it from memory through a
process known as garbage collection.
References
• Starting Out with Python by Tony Gaddis, Pearson; 4 edition (March 16, 2017)
• https://www.tutorialspoint.com/python_data_structure/
python_algorithm_design.htm

You might also like