ch03
ch03
Computer Programming 1
Program: Intermediate Diploma in Programming and Computer Science
Amira Alotaibi
x = 5
print(type(x)) # Output: <class 'int’>
x = "10"
y = int(x) # Converts string to integer
Assign value
The following table shows the value in memory for the variables area and
radius as the program is executed.
• In some cases, the Python interpreter cannot determine the end of the
statement written in multiple lines. You can place the line continuation
symbol (\) at the end of a line to tell the interpreter that the statement is
continued on the next line.
• For example, the following statement
sum = 1 + 2 + 3 + 4 + \
5 + 6
IPO
Most of the programs in early chapter soft his book perform three steps: Input,
Process, and Output, called IPO.
•Input is to receive input from the user.
•Process is to produce results using the input.
•Output is to display the results.
USING IDENTIFIERS
What Are Identifiers?
• Identifiers are names for variables, functions, and objects
• Rules for identifiers:
• Must start with a letter or _ (underscore)
• Can contain letters, numbers, and underscores
• Cannot be a Python keyword (ex: print, if, while … etc)
Example:
• Case sensitive:
Python is case sensitive, area, Area, and AREA are all different identifiers
Examples of Valid & Invalid Identifiers
Valid Invalid
student_age 2value (cannot start with a number)
_privateVar class (reserved keyword)
totalAmount total-amount (contains -)
ASSIGNING DATA TO VARIABLES
• Variables are used to reference ( values that may be changed in the
program
• In the previous programs, we used variables to store values: area,
radius, average
• They are called variables because their values can be changed
Variable Assignment
• The ( = ) operator assigns values
• Example:
Dynamic Typing in Python
• You can use a variable in an expression. A variable can also be used in both sides of
the = operator.
• For example, x = x + 1
• In this assignment statement, the result of x + 1 is assigned to x. If x is 1 before the
statement is executed, then it becomes 2 after the statement is executed.
Note
x = 1
y = 2
temp = x # Save x in a temp variable
x = y # Assign the value in y to x
y = temp # Assign the value in temp to y
• But you can simplify the task using the following statement to swap the values of x and y
x, y = y, x # Swap x with y
Obtaining Multiple Input In One Statement
Simultaneous assignment can also be used to obtain multiple input in one statement
For Example:
CONSTANTS
Example Code:
GETTING SYSTEM TIME
Using time.time()
SOFTWARE DEVELOPMENT PROCESS