Day 3 Variables
Day 3 Variables
• Even when a variable name is “legal” (i.e., follows the rules), it might not
be a “good” name.
• Here are some generally accepted conventions for designing good
names:
• A name should be meaningful: Name price is better than name p.
• For a multiple-word name, use either the underscore as the delimiter
(e.g., temp_var and interest_rate) or camelCase capitalization (e.g.,
tempVar, TempVar, interestRate or InterestRate); pick one style and use
it consistently throughout your program.
• Shorter meaningful names are better than longer ones.
Reserved keywords
• The names below are used as reserved keywords of the Python language.
You cannot use them other than as Python commands.
Types of Variables
• A string variable can hold text, such as letters of the alphabet, and
other special characters, such as punctuation marks. If a working
program contains the statement set lastName = "Lincoln", then
lastName is a string variable. A string variable can also hold digits
either with or without other characters. For example, “235 Main
Street” and “86” are both strings. A string like “86” is stored diff
erently than the numeric value 86, and you cannot perform arithmetic
with the string.
Types of Variables
• A string variable can hold text, such as letters of the alphabet, and
other special characters, such as punctuation marks. If a working
program contains the statement set lastName = "Lincoln", then
lastName is a string variable. A string variable can also hold digits
either with or without other characters. For example, “235 Main
Street” and “86” are both strings. A string like “86” is stored diff
erently than the numeric value 86, and you cannot perform arithmetic
with the string.
Assigning Values to Variable
• Type an algebraic expression, such as 3 + 7, and hit the Enter key on the
keyboard to view the result of evaluating the expression:
• >>> 3 + 7
10
Let’s try expressions that use different
algebraic operators:
>>> 2 * 3 + 1
• >>> 3 * 2 7
>>> (3 + 1) * 3
6
12
• >>> 5 / 2 >>> 4.321 / 3 + 10
2.5 11.440333333333333
• >>> 4 / 2
>>> 4.321 / (3 + 10)
>>> 14 // 3
2.0 4
>>> 14 % 3
2
Python functions
• Algebraic expressions evaluate to a number, whether of type int or float or one of the other
number types that Python supports.
• In an algebra class, expressions other than algebraic expressions are also common.
• For example, the expression 2 < 3 does not evaluate to a number; it evaluates to either True or
False (True in this case).
• Python can also evaluate such expressions, which are called Boolean expressions.
• Boolean expressions are expressions that evaluate to one of two Boolean values: True or False.
• These values are said to be of Boolean type, a type just like int and float and denoted bool in
Python.
Boolean Expression
>>> 2 < 3
True
>>> 3 < 2
False
>>> 5 - 1 > 2 + 1
True
Boolean Expression
Practice Problem 2
• Translate the following statements into Python Boolean expressions and evaluate
them:
• (a) The sum of 2 and 2 is less than 4.
• (b) The value of 7 // 3 is equal to 1 + 1.
• (c) The sum of 3 squared and 4 squared is equal to 25.
• (d) The sum of 2, 4, and 6 is greater than 12.
• (e) 1387 is divisible by 19.
• (f) 31 is even. (Hint: what does the remainder when you divide by 2 tell you?)
• (g) The lowest price among $34.99, $29.95, and $31.50 is less than $30.00.
Noted