Professional Documents
Culture Documents
Python Data Types and Error Correction
Python Data Types and Error Correction
Integers
Whole numbers that do not contain a decimal point
Abbreviated as “ int” in Python
Example: 5, -5, 100, 10032
You can store numeric data inside variables that you create.
Example:
5.5
“Hello”
“5.5”
2.975
2.0
+ Numeric Data Types
PHP C ++
JavaScript Java
Perl ActionScript
+ Strictly Typed Languages -
Examples
ActionScript Java
var name:String = “Harry”; String name = “Harry”;
We can capture input from the user (via the input() function)
and use that input in our calculations
Example:
Syntax errors: The code does not follow the rules of the
language; for example, a single quote is used where a double
quote is needed; a colon is missing; a keyword is used as a
variable name.
Runtime errors: In this case, your code is fine but the program
does not run as expected (it “crashes”). For example, if your
program is meant to divide two numbers, but does not test for a
zero divisor, a run-time error would occur when the program
attempts to divide by zero.
Logic errors: These can be the hardest to find. In this case, the
program is correct from a syntax perspective; and it runs; but the
result is unanticipated or outright wrong. For example, if your
program prints “2+2 = 5” the answer is clearly wrong
+ Example Errors
Source Execution
num = input ('give me a give me a number: apple
number: ’)
Traceback (most recent
call last):
num_float = float(num)
File "/Users/
new_num = 10 + num_float HarryPotter/Documents/
madlibs01.py", line 6, in
print (new_num) <module>
new_num = 10 + num
TypeError:
unsupported
operand type(s)
for +:
'int' and 'str'
+
(that was a runtime error)
The program ran, but when given bad data it crashed
+ Example Errors
Source Execution
num_1 = float (input give me a num: 5
(‘give me a num: ’) )
give me another num: 2
num_2 = float (input
(‘give me another the sum is: 3.0
num: ’) )
Set small, incremental goals for your program. Don’t try and write large
programs all at once.
Stop and test your work often as you go. Celebrate small successes
Use comments to have Python ignore certain lines that are giving you
trouble
+
Advanced Math Operations
+ Division O perations
Most times you will use the floating point division operator
(“/”)
+ Division O perations
Ex:
((5+10+20)/60) *
100
+ Programming C hallenge
Example: 24
2 ** 4
+ ProgrammingC hallenge:
Calculating the area of a square
+ Remainder O perator (modulo)
Example:
5/2 # 2.5
5%2 # 1
+ ProgrammingC hallenge: Time
Calculations
Ask the user to input a number of
seconds as a whole number.
Then express the time value
inputted as a combination of
minutes and seconds.
(3)(12) 3 * 12
4xy 4*x*y
x
y=3*x/2
y3
2
+ ProgrammingC hallenge:
Investment Planning
In this exercise you will ask the user to input the following
values
How much money they want to generate
An interest rate value
How long they’d like to invest their money
% over years.
+ ProgrammingC hallenge:
Investment Planning
F P = Present Value
P F = Future Value
You can use the “\” symbol to indicate to Python that you’d
like to continue the expression onto another line
Example:
x = 5 + 2 / \
7
+ 8 – 12
Example:
Example:
# output: one*two
+ C ombing both lineendings
and separators
You can use both the ‘sep’ and the ‘end’ arguments at the
same time in the same print() function call.
Example:
Example:
# line 1
# line 2
# line 3
+ Escape C haracters
x = 5
y = 4
X Y X*Y
5 4 20
+ Programming C hallenge
Product Price
product1 price1
product2 price2
product3 price3
+ String C oncatenation
Example:
a = input(‘firstname’)
b = input(‘last
name’)
c = b + ‘,’ +
a print (c)
+ String Repetition
Example:
# Fa La La La La La La La
La
+
The format() function
+ Format ing Strings
Name Department
Harry Computer Science
In this case we need to ensure that the strings “Name” and
“Harry” are the same width so that the strings that come after
them (“Department” and “Computer Science”) line up
correctly.
+ String Format ing
You can use the format() function to “ pad” a string with extra
spaces at the beginning or the end of the string.
For example:
x = format(‘Harry’, ‘<20s’)
This will generate a new string (x) that contains the string
‘Harry’ plus 15 spaces at the end of the string. The total
length of the string in this case will be 20 characters.
The ‘<‘ character in the formatting pattern tells Python to left
justify the string and place the extra spaces at the end of the
new string
+ String Format ing
You can also have Python right justify the string and place the
spaces at the beginning of the new string by doing the
following:
b = format(‘Harry’, ‘>20s’)
+ String Format ing
x = “Hello, World!”
y = format(x, ‘>20s’)
print (x)
print (y)
a = 1/6
print (b)
+ Format ing pat erns
a = 10000/6
Number 1 Number 2 N1 * N2
2 1 2
2 2 4
2 3 6
2 4 8