2. Variables
• A variable is a name that represents a value stored in the
computer’s memory.
• So far, applications that you have created have stored data only
in component properties.
• Variables are objects that can save data
temporarily to reuse & simplify code.
• Variable names should be descriptive of what
they are storing
• Variables allow to update and modify code more
efficiently
3. Variable Types
• Programming languages have a plethora of variable types, however…
• Scripting languages like Python have tried to streamline
these types to the bare bones essential categories.
• There is a default variable type in scripting languages,
string.
• However Python is smart enough to know what
information is stored in a variable and knows behaviors
associated with it. That way if you store a number in a
variable it will automatically convert it for you to that type.
• Question: What are some other types of data that we could store?
4. Declaring, Initializing & Assigning Variables
• Declaring a Variable : N/A in python
• Initializing a Variable : Creating a variable and setting a
starting value to a variable.
• Assigning/Reassigning a Variable : When we set a value to a
variable
5. Creating a Programming Statement
• A programming statement is a complete action comprised of
expressions.
• A mathematical equation
• Initializing a variable
• Assigning a variable to an LED
• Ex. red = LED(17)
• name = “Jason”
• age = 34
• lie = True
• money = math.floor(1.92)
8. The Power of Remainders
• Question: What is the minimum number of coins to make $.97,
how did you come to this conclusion?
Saturday is the 6th
day in a week
A week has 7 days
After 10 days
The 2nd
day in a week is Tuesday
(6 + 10) % 7 is 2
124
3
12
0
73
2
6
1
268
3
24
2 Remainder
Quotient
2013
1
13
7
DividendDivisor
9. Arithmetic Expressions and Statements
is translated to
(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
)
94
(9
))(5(10
5
43
y
x
xx
cbayx
10. Built in Math Functions
>>> max(2, 3, 4) # Returns a maximum number
4
>>> min(2, 3, 4) # Returns a minimum number
2
>>> round(3.51) # Rounds to its nearest integer
4
>>> round(3.4) # Rounds to its nearest integer
3
>>> abs(-3) # Returns the absolute value
3
>>> pow(2, 3) # Same as 2 ** 3
8
11. The Math Module
Function Description Example
fabs(x) Returns the absolute value of the argument. fabs(-2) is 2
ceil(x) Rounds x up to its nearest integer and ceil(2.1) is 3
returns this integer. ceil(-2.1) is -2
floor(x) Rounds x down to its nearest integer and floor(2.1) is 2
returns this integer. floor(-2.1) is -3
exp(x) Returns the exponential function of x (e^x). exp(1) is 2.71828
log(x) Returns the natural logarithm of x. log(2.71828) is 1.0
log(x, base) Returns the logarithm of x for the specified log10(10, 10) is 1
base.
sqrt(x) Returns the square root of x. sqrt(4.0) is 2
sin(x) Returns the sine of x. x represents an angle sin(3.14159 / 2) is 1
in radians. sin(3.14159) is 0
asin(x) Returns the angle in radians for the inverse asin(1.0) is 1.57
of sine. asin(0.5) is 0.523599
cos(x) Returns the cosine of x. x represents an cos(3.14159 / 2) is 0
angle in radians. cos(3.14159) is -1
acos(x) Returns the angle in radians for the inverse acos(1.0) is 0
of cosine. acos(0.5) is 1.0472
tan(x) Returns the tangent of x. x represents an tan(3.14159 / 4) is 1
angle in radians. tan(0.0) is 0
fmod(x, y) Returns the remainder of x/y as double. fmod(2.4, 1.3) is 1.1
degrees(x) Converts angle x from radians to degrees degrees(1.57) is 90
radians(x) Converts angle x from degrees to radians radians(90) is 1.57
12. Checkpoint
• 1. What is a variable?
• 2. Why do we have naming conventions?
• 3. What is the difference between a programming
expression and a statement?
Notas do Editor
Introduce other types like float, int, long, string, char and Boolean but with discuss further conversions later