1/55
Looks like no tags are added yet.
Tracing a program
Helps you to understand how programs work
If an item is a number,
The number is automatically converted to a string for displaying
Variable
Represents a value stored in the computer’s memory
Every variable has a
Name and value
Every value has a
Data type
Data type
Used to specify what type of the value are being used, such as integers or strings
Python provides
Basic (built-in) data types for integers, real numbers, string, and Boolean types
Input function
Used to ask the user to input a value, and then return the value entered as a string
Eval
Used to evaluate and convert the passed value (string) to a numeric value
If the user enters something other than a number,
The program will terminate with a runtime error
\
Used at the end of a line to tell the interpreter that the statement is continued on the next line
The three steps a program run on:
Input (input from the user),
Process (produce results using the input),
Output (displays the results)
Identifier
The names that identify the elements such as variables and functions in a program
Rule of an indentifier
Sequence of characters that consists of letters, digits, and underscores (_), and cannot be a keyword
Keyword
Reserved words, have special meanings in Python
Import
A keyword which tells the Python interpreter to import a module to the program
Variables
Used to reference (represent) values that may be changed in the program
Assignment statement
The statement for assigning a value to a variable
=
Used as the assignment operator
Expression
Represents a computation involving values, variables, and operators that, taken together, evaluate to a value
A variable can also be used
In both sides of the = operator.
Scope of a variable
The part of the program where the variable can be referenced (used)
A variable must be assigned a value before it can
Be used in an expression
Simultaneous assignment
It tells Python to evaluate all the expressions on the right and assign them to the corresponding variable on the left simultaneously
Swapping variable values
A common operation in programming and simultaneous assignment is very useful to perform this operation
A named constant is an identifier that represents
A permanent value
The value of a variable can change
During execution of a program
A named constant
Represents a permanent data that never changes
Data
What the information stored in a computer is generally referred to
Two types of numeric data
Integers and real numbers
Integer types (int for short)
Are for representing whole numbers
Real types
Representing numbers with a fractional part
Real numbers
Represented as floating-point (or float) values
Literal
A constant value that appears directly in a program (such as 1.0 or 1)
+
Addition
-
Subtraction
*
Multiplication
/
Float division
//
Integer division
**
Exponentiation
%
Remainder
The +, -, and * operators are straightforward, but note that the
+ and - operators can be both
Unary and binary
Unary operator
Only one operand
Binary operator
Has two operands
% operator (known as remainder)
Yields the remainder after division
Left-side operand
The dividend
Right-side operand
The divisor
Calculations involving floating-point numbers are approximated
Because these numbers are not stored with complete accuracy
Overflow
When a variable is assigned a value that is too large (in size) to
be stored
Underflow
When a floating-point number is too small (for example, too
close to zero) to be stored
There are no spaces in
The augmented assignment operators
The augmented assignment operator
Performed last after all the other operators in the expression are evaluated
Type conversion
The process of changing a value from one data type to another
If you would like to change the variable being converted by the
functions int and round,
Reset the variable after invoking the function
The int function can also be used to convert
An integer string into an integer
You can use the str(value) function to convert
The numeric value to a string