[go: up one dir, main page]

0% found this document useful (0 votes)
10 views25 pages

Day 3 Variables

python programming tutorial series Day 3 variables

Uploaded by

Chit Su
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views25 pages

Day 3 Variables

python programming tutorial series Day 3 variables

Uploaded by

Chit Su
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Variables

Working with Variables


• Variables are named memory locations whose contents can vary or diff er
over time.
• For example, in the number-doubling program, myNumber and myAnswer
are variables.
• Th e ability of variables to change in value is what makes computers and
programming worthwhile.
• Because one memory location can be used repeatedly with different
values, you can write program instructions once and then use them for
thousands of separate calculations.
Two variables in this
program,
1. myNumber
2.myAnswer
Variable Names

• The characters making up a variable name can be lowercase and


uppercase letters from the alphabet (a through z and A through
Z), the underscore character (_), and, except for the first
character, digits 0 through 9:
• myList and _list are OK, but 5list is not.
• list6 and l_2 are OK, but list-3 is not.
• mylist and myList are different variable names.
Good names

• 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 numeric variable is one that can hold digits and have


mathematical operations performed on it. Also, you usually have
the option of having a numeric variable hold a decimal point and
a sign indicating positive or negative. In the statement set
myAnswer = myNumber * 2, both myAnswer and myNumber are
numeric variables; that is, their intended contents are numeric
values, such as 6 and 3, 14.8 and 7.4, or −18 and −9.
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

• set someNumber = someOtherNumber


• set someNumber = someOtherNumber * 5
• assignment statement (set x=5+2).
• assignment operator (=).
Expression
• Performing Arithmetic Operations
• Most programming languages use the following standard arithmetic
operators:
• + (plus sign)—addition
• − (minus sign)—subtraction
• * (asterisk)—multiplication
• / (slash)—division
• set x=5+2
• (5+2) is expression
Expressions, Variables, and Assignments
Python
Algebraic Expression

• 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

• >>> abs(-4) • >>> max(6, -2)


• 4 • 6
• >>> abs(4) • >>> min(2, -4, 6, -2)
• 4 • -4
• >>> abs(-3.2) • >>> max(12, 26.5, 3.5)
• 3.2 • 26.5
• >>> min(6, -2)
• -2
Practice Problem 1

• Write Python algebraic expressions corresponding to the following statements:


• (a) The sum of the first five positive integers
• (b) The average age of Sara (age 23), Mark (age 19), and Fatima (age 31)
• (c) The number of times 73 goes into 403
• (d) The remainder when 403 is divided by 73
• (e) 2 to the 10th power
• (f) The absolute value of the difference between Sara’s height (54 inches) and
Mark’s height (57 inches)
• (g) The lowest price among the following prices: $34.99, $29.95, and $31.50
Boolean Expressions and Operators
Boolean Expression

• 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

• Just as algebraic expression can be combined into larger algebraic


expression, Boolean expressions can be combined together using Boolean
operators and , or , and not to form larger Boolean expressions.
• The and operator applied to two Boolean expressions will evaluate to True if
both expressions evaluate to True; if either expression evaluates to False,
then it will evaluate to False:
Boolean Algebra
• Boolean algebra is the algebra of values true and false.
• Boolean algebra includes operators and, or, and not, which can be used to
create Boolean expressions, expressions that evaluate to true or false.
• The truth tables below define how these operators evaluate.
Variables and Assignments
Variables and Assignments
• >>> x = 4
• The statement x = 4 is called an assignment statement.
• The general format of an assignment statement is: =
• An expression we refer to as lies on the right-hand side of the = operator; it
can be an algebraic, Boolean, or other kind of expression.
• On the left-hand side is a variable . The assignment statement assigns to
the value.
• In the last example, x is assigned value 4.

You might also like