expressions, statements, variables, assignments, types cse 1310 – introduction to computers and...

55
Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington a significant part of this material has been created by Dr. Darin B and Dr. Gian Luca Mariottini 1

Upload: flora-chase

Post on 20-Jan-2016

284 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

1

Expressions, Statements, Variables, Assignments, Types

CSE 1310 – Introduction to Computers and ProgrammingVassilis Athitsos

University of Texas at Arlington

Credits: a significant part of this material has been created by Dr. Darin Brezealeand Dr. Gian Luca Mariottini

Page 2: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

2

Expression

• An expression is a piece of code that evaluates to a value.– That value is called a return value.

• The concept of an expression is key in this course, we will refer to it many times.

• It is VERY important to differentiate what is an expression and what is not an expression.

Page 3: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

3

Is it an Expression?

• 12• 1+(2**3)• a = 5+12• b• c = raw_input("enter a number: ")• raw_input("enter a number: ")

Page 4: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

4

Is it an Expression?

• 12 yes• 1+(2**3) yes• a = 5+12 no• b yes, if b is a defined variable• c = raw_input("enter a number: ") no• raw_input("enter a number: ") yes

Page 5: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

5

Is it an Expression?

• 12 yes, return value 12• 1+(2**3) yes, return value 9• a = 5+12 no• b yes, if b is defined, returns value of b• c = raw_input("enter a number: ") no• raw_input("enter a number: ") yes, return

value is a string of whatever we type.

Page 6: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

6

How Can We Tell If Something is an Expression?

• "Easy" way: type it in on the Python shell, and see if we get back a result.– Useful for experimentation, quick checks.

• An example: a = 5 in some languages (e.g., C++) a = 5 is an expression whose

result is 5.To figure out if it is an expression in Python, you can search on the documentation (potentially time-consuming) or type it in and see if you get a result.

(The result is that a = 5 is NOT an expression).

Page 7: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

7

How Can We Tell If Something is an Expression?

• "Thinking" way: understand what are expressions, learn some rules.– No need to memorize lots of rules, just identify some basic

broadly applicable rules that cover many cases.

• Example rule #1: Arithmetic calculations, involving numbers and variables, are expressions.

Page 8: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

8

How Can We Tell If Something is an Expression?

• "Thinking" way: understand what are expressions, learn some rules.– No need to memorize lots of rules, just identify some basic

broadly applicable rules that cover many cases.

• Example rule #2: a piece of code that is legal to appear at the right side of the assignment operator (i.e., the = sign) is an expression.

expression

c = raw_input("enter a number: ")

Page 9: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

9

How Can We Tell If Something is an Expression?

• NOTE: both the "easy" way and the "thinking" way lead to the same conclusions.

• The two rules shown before are worth memorizing (or keeping handy in your notes).

• This is a more general guideline. Oftentimes there are multiple ways to reason about an aspect of programming. However, all correct ways should lead to the same conclusion.

Page 10: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

10

Trick Question

• Is this an expression?12 minus 5

Page 11: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

11

Trick Question

• Is this an expression?12 minus

• Let's try answering this question the "easy" way, by typing it in:

>>> 12 minus 5

Page 12: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

12

Trick Question

• Is this an expression?12 minus

• Let's try answering this question the "easy" way, by typing it in:

>>> 12 minus 5

SyntaxError: invalid syntax

Page 13: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

13

Trick Question

• Is this an expression?12 minus

• Let's try answering this question the "easy" way, by typing it in:

>>> 12 minus 5

SyntaxError: invalid syntax

• 12 minus 5 is not valid code (gives an error, not a result value), so it is not an expression.

Page 14: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

14

Locating Expressions

• An expression can be an entire line of code.• An expression can appear as part of a line of

code.• Simple expressions can be combined into

complicated expressions.– Example: 12 + 5 + 12*9/3

• Three subexpressions: 12, 5, 12*9/3, connected by +.• 12*9/3 can be further decomposed…

Page 15: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

15

Locating Expressions• Can you find expressions that are entire lines?# get the radius from the user as a stringradius_string = raw_input("Enter the radius of your circle: ")

# convert the radius string to an integer.radius = int(radius_string)

# compute and print the circumferencepi = 3.14159circumference = radius * 2 * piprint "Circumference = ", circumference

# compute and print the areaarea = (radius ** 2) * piprint "area = ", area

Page 16: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

16

Locating Expressions• Can you find expressions that are entire lines? NO# get the radius from the user as a stringradius_string = raw_input("Enter the radius of your circle: ")

# convert the radius string to an integer.radius = int(radius_string)

# compute and print the circumferencepi = 3.14159circumference = radius * 2 * piprint "Circumference = ", circumference

# compute and print the areaarea = (radius ** 2) * piprint "area = ", area

Page 17: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

17

Locating Expressions• Can you find expressions that are parts of lines?# get the radius from the user as a stringradius_string = raw_input("Enter the radius of your circle: ")

# convert the radius string to an integer.radius = int(radius_string)

# compute and print the circumferencepi = 3.14159circumference = radius * 2 * piprint "Circumference = ", circumference

# compute and print the areaarea = (radius ** 2) * piprint "area = ", area

Page 18: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

18

Locating Expressions• Can you find expressions that are parts of lines? YES# get the radius from the user as a stringradius_string = raw_input("Enter the radius of your circle: ")

# convert the radius string to an integer.radius = int(radius_string)

# compute and print the circumferencepi = 3.14159circumference = radius * 2 * piprint "Circumference = ", circumference

# compute and print the areaarea = (radius ** 2) * piprint "area = ", area

Highlighted in red you see some examples of expressions, there are more examples than the ones shown.

Remember:

-Simple expressions can be parts of more complicated expressions.

- Expressions may consist of more simple expressions.

Page 19: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

19

What Does Python Do With Expressions?

• Every time Python sees an expression, it evaluates it.

• This is how Python computes.• When Python evaluates a longer piece of code

that includes an expression:– Python computes the return value of the

expression.– Python substitutes the return value for the

expression itself.

Page 20: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

20

An Example of Expression Evaluation

>>> print 12+3• Here we have a line of code that includes

expression 12+3.• When Python evaluates that line of code, it

computes the return value of 12+3, which is 15.• Then, Python simplifies the code that is

evaluated, by substituting the return value for the expression. Thus, "print 12+3" becomes "print 15".

Page 21: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

21

One Expression vs. Many Expressions

• Note: (kind of obvious, but I have seen many people make this mistake, which is serious):Two (or more) expressions are NOT an expression.

• print "some text here", some_variable– How many expressions is the word "print" followed by?

Page 22: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

22

One Expression vs. Many Expressions

• Note: (kind of obvious, but I have seen many people make this mistake, which is serious):Two (or more) expressions are NOT an expression.

• print "some text here", some_variable– print is followed by TWO expressions separated by a

comma. These two expressions do NOT make up a single expression.

– The comma is NOT part of either expression.– Yes, understanding code means understanding such details.

Page 23: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

23

One Expression vs. Many Expressions

• Note: (kind of obvious, but I have seen many people make this mistake, which is serious):Two (or more) expressions are NOT an expression.

• my_variable = 41 + 31– Here, 41 + 31 is a single expression, containing two more

simple expressions (41, and 31)– The + operator is a part of the single expression 41 + 31. It

is NOT a part of the two simple expressions 41, 31. The + operator is used to COMBINE those two simple expressions into a new, more complicated expression.

– There is no theoretical limit (just memory limits of the computer) on how large/complicated an expression can be.

Page 24: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

24

Checklist

• You should now be able to:– Define what an expression is.– Be able to determine if any piece of code is an

expression or not.– Be able to identify expressions in any line of code.– Be able to identify whether an expression contains

more simple expressions.– Be able to write some expressions by yourself (TRY

IT).

Page 25: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

25

Statements

• Consider this line of code:print "Circumference = ", circumference

• This line is NOT an expression (although it contains expressions).

• This line of code "does something", namely it prints out some information.

• Statements are pieces of code that are NOT expressions, but do something.

Page 26: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

26

Statements and Side Effects

• What a statement does is called a side effect.• Examples of side effects:

– Printing out a message– Assigning a value to a variable– Pausing for five seconds– Drawing a picture– Playing a song– Downloading and displaying a web page

Page 27: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

27

Statements and Expressions

• A typical program needs both expressions and statements.

• Expressions are used to compute new data.• Statements are used to do things that the user

can see.• A program without expressions does not

compute anything.• A program without statements does not

produce any output (hard to imagine a use).

Page 28: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

28

A Statement-Less Program

• Type this into a text file, and execute:

12+45*215 / 2 + 4**7

• Each of these two lines of code computes something (so Python will spend the time needed to compute those values).

• However, the program shows nothing to the user. Thus, the computation has been wasted.

Page 29: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

29

The print Statement• Syntax:

print exp_1, exp_2, …, exp_n– print is followed by 0 or more expressions,

SEPARATED BY A COMMA.– print prints the RETURN VALUE of each

expression.• What will this line print?print 12+3• It will print 15, it will NOT just print the text

"12+3"

Page 30: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

30

Expressions Can Also Have Side-Effects

• Consider this line:radius_string = raw_input("Enter the radius of your circle: ")

• In the above line, the right side of the assignment operator contains this expression: raw_input("Enter the radius of your circle: ")

• This expression also has a side effect: it prints out a message.

Page 31: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

31

Assignment Statements

• An assignment statement has this syntax:

my_variable = expression

• What an assignment statement does is:– compute the return value of expression– associate that return value with the variable called

my_variable.– from now on, my_variable is an expression whose

return value is the value stored in my_variable.

Page 32: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

32

The Assignment Operator

• In Python, we call the = sign the assignment operator.

• The assignment operator looks the same, but is not the same as the = sign in math.

• First difference:– in math, "a = 5" is the same as "5 = a".– In Python, "a = 5" is a valid piece of code assigning

value 5 to variable a. "5 = a" is not valid code.

Page 33: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

33

The Assignment Operator

• In Python, we call the = sign the assignment operator.

• The assignment operator looks the same, but is not the same as the = sign in math.

• Second difference:– in math, "a = a + 5" is nonsense (most of the time)– In Python, "a = a + 5" is a valid piece of code, and

increments the value of a by 5.

Page 34: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

34

The Assignment Operator

• In Python, we call the = sign the assignment operator.

• The assignment operator looks the same, but is not the same as the = sign in math.

• Third difference:– in math, "a + 5 = 7" is a well-defined equation.– In Python, "a + 5 = 7" is not a valid piece of code,

because the left side of the assignment operator is NOT a variable name.

Page 35: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

35

Variable Names

• Variable names must follow some simple rules:– must begin with a letter or an underscore.

• DO NOT start variable names with underscores for the time being.

– can include letters, numbers and underscores• but cannot start with a number.

– are case sensitive, name and Name are different variables.

– cannot be the same as a keyword• Try in Python: print = 15

Page 36: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

36

Choosing Variable Names

• Python does not care what names you use (as long as you follow the rules).

• However, descriptive variable names can make a huge difference in program readability, helping both others and yourself understand and debug your code.– You will probably be surprised by how hard it will

be for yourself to read your own code a few days or weeks after you wrote it.

Page 37: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

37

Examples of Assignments

>>> xInt = 5>>> yInt = 2 + 3>>> yInt = yInt + 7• NOTE: while evaluating the expression on the

right side of the assignment operator, the OLD value of the variable is used.

Page 38: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

38

Examples of Errors

• myInt + 5 = 7• 7 = myInt + 5• print myInt=5

Page 39: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

39

Examples of Errors

• myInt + 5 = 7– Left side of assignment operator is NOT a variable

name.• 7 = myInt + 5

– Left side of assignment operator is NOT a variable name.

• print myInt=5– Violates the print syntax: print must be followed

by expressions, myInt=5 is NOT an expression.

Page 40: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

40

Operators += -= *= /=

• a += 5 – is the same as: a = a + 5

• a -= 5– is the same as a = a – 5

• a *= 5– is the same as a = a * 5

• a /= 5– is the same as a = a / 5

Page 41: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

41

The Notion of Syntactic Sugar

• If Python did not have +=, -=, *=, /=, it would not prevent us from writing any code.– We would just need to write slightly longer (but

easier to read) lines.– The term "syntactic sugar" refers to elements of a

programming language that are not vital, but simply allow somewhat shorter/more convenient alternative ways to write something.

Page 42: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

42

Types

• In Python, every expression has a type.• You can find the type of any expression using

the type keyword

>>> type(4)<type 'int'>>>> type(10+12)<type 'int'>

• For now, we care about three types: – integers (int), real numbers (float), strings (str)

>>> a = 2.34>>> type(a)<type 'float'>>>> b = "hello">>> type(b)<type 'str'>

Page 43: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

43

Types

• The int type:– Used to store integers, like 4, 0, -10.

• The float type:– Used to store real numbers, like -13.34, 1.0, 10.5

• The str type:– Used to store strings, i.e., text, like:

• "hello"• "today is Monday"• the contents of an entire book

Page 44: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

44

Why Are Types Important?

• The type of an expression specifies two things:– the internal structure of the expression (what kind

of data it contains)– the kinds of operations that Python can perform

on that expression

Page 45: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

45

int vs. float

• A simple example where types make a difference:

>>> 5/2???>>> 5.0/2.0???

Page 46: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

46

int vs. float

• A simple example where types make a difference:

>>> 5/22>>> 5.0/2.02.5

• Why do these two lines produce different results?

Page 47: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

47

int vs. float

• A simple example where types make a difference:

>>> 5/22>>> 5.0/2.02.5

• Why do we get two different results?– The first line performs integer division: the result

is an integer and the remainder is discarded.

Page 48: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

48

Automatic Type Assignment

• Python does not require you to pre define the ‐type of a variable.– An important difference from Java, C++.

• What type a variable holds can change.– Nonetheless, knowing the type can be important

for using the correct operations on a variable.

Page 49: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

49

An Example of Type Conversion

• From the circles.py program:# get the radius from the user as a stringradius_string = raw_input("Enter the radius of your circle: ")

# convert the radius string to an integer.radius = int(radius_string)

• The int keyword is used to convert a string into an integer.

Page 50: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

50

Type Conversion

• int(var_name) converts to an integer• float(var_name) converts to a float• str(var_name) converts to a string• You should check these out:

– int(2.1) → 2, int(‘2’) → 2,– int(‘2.1’) will fail– float(2) → 2.0, float(‘2.0’) → 2.0, – float(‘2’) → 2.0, float(2.0) → 2.0– str(2) → ‘2’, str(2.0) → ‘2.0’, str(‘a’) → ‘a’

Page 51: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

51

Implicit Type Conversion

• What does 4/3.0 produce?• 4 is an integer, 3.0 is a float. Python cannot

perform arithmetic operations on mixed types.• Python will automatically convert to the most

informative type.– float is more informative than int, since converting

from int to float does not lose information, converting from float to int loses the decimal digits.

• Thus, the result is 1.33333333

Page 52: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

52

The + and += Operator on Strings

>>> "hello" + "world"'helloworld'>>> a = "good">>> a += " morning">>> a'good morning'

• The + operator concatenates strings together into a single string.

Page 53: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

53

Strings vs. Numerical Expressions

>>> a = 12+3>>> a15>>> a = "12+3">>> a>>> 12+3>>> print 12+315>>> print "12+3"12+3

do not confuse a string (which is text) looking like a numerical expression with the numerical expression itself.

Page 54: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

54

Assignments Using raw_input Expression

• Syntax: variable = raw_input(expression)

• Such a line does the following:– Prints the return value of the expression (this

means that the expression must be evaluated).– Waits for the user to write some text and press

<ENTER>– Stores the text typed by the user into variable.

Page 55: Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

55

Program Execution# get the radius from the user as a stringradius_string = raw_input("Enter the radius of your circle: ")

# convert the radius string to an integer.radius = int(radius_string)

# compute and print the circumferencepi = 3.14159circumference = radius * 2 * piprint "Circumference = ", circumference

# compute and print the areaarea = (radius ** 2) * piprint "area = ", area

Execute lines from top to bottom (exceptions apply, we will see soon).

For each line, first evaluate the expressions that are encountered on that line, and then execute the statements (if any) that use those expressions.