Expressions, Statements, and Programs CSC 101 Christopher Siu 1 / - - PowerPoint PPT Presentation

expressions statements and programs
SMART_READER_LITE
LIVE PREVIEW

Expressions, Statements, and Programs CSC 101 Christopher Siu 1 / - - PowerPoint PPT Presentation

Expressions, Statements, and Programs CSC 101 Christopher Siu 1 / 10 Expressions Defjnition An expression is a code fragment that evaluates to a value. The simplest expressions are literal values. Example 2 is an expression that evaluates to


slide-1
SLIDE 1

Expressions, Statements, and Programs

CSC 101 Christopher Siu

1 / 10

slide-2
SLIDE 2

Expressions

Defjnition

An expression is a code fragment that evaluates to a value. The simplest expressions are literal values.

Example

2 is an expression that evaluates to the value 2.

Example

'hello' is an expression that evaluates to the value 'hello'.

2 / 10

slide-3
SLIDE 3

Operators

More complex expressions can be created using operators.

Example

3 + 6 / 4 is an expression that evaluates to the value 4.5.

The Python interpreter can be used to evaluate expressions.

Example

>>> 2 2 >>> 'hello' 'hello' >>> 3 + 6 / 4 4.5

3 / 10

slide-4
SLIDE 4

Data Types

Defjnition

A data type determines how to interpret a value and what

  • perations may be performed on a value.

Every value in Python has a “type”. The types of values determine the behaviors of operators.

Example

1 + 2 evaluates to 3. 1.0 + 2.0 evaluates to 3.0. '1' + '2' evaluates to '12'.

4 / 10

slide-5
SLIDE 5

Type Conversion

Python provides a built-in way to convert among types.

Example

>>> '3.8' * 3 '3.83.83.8' >>> float('3.8') * 3 11.399999999999999 >>> int('3.8') * 3 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '3.8' >>> int(float('3.8')) * 3 9

5 / 10

slide-6
SLIDE 6

Variables

Defjnition

A variable has a name and contains a value. Variables are also expressions. Variables evaluate to their contained values.

Example

>>> x = 2 >>> x 2 >>> y Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'y' is not defined

6 / 10

slide-7
SLIDE 7

Statements

Defjnition

A statement is a complete command for the interpreter to execute. In Python, all expressions are statements. Not all statements are expressions.

Example

x = 2 is a statement but not an expression:

It is a command to assign the value 2 to the variable x. It does not evaluate to a value.

7 / 10

slide-8
SLIDE 8

Programs

Defjnition

A Python program is a sequence of statements. Python programs are also called scripts.

Example

Suppose the fjle file.py contains the statements:

1

x = 13

2

y = 5

3

print(x / y)

…then this program produces:

>$ python3 file.py 2.6

8 / 10

slide-9
SLIDE 9

Modules

Python fjles can be broken into multiple modules. Modules can be combined together using: import <file> from <file> import <defns, ...>

Example

Suppose the fjle file2.py contains the statements:

1

x = 4

…and the fjle file1.py contains:

1

from file2 import *

2

print(x)

…then, together, these produce:

>$ python3 file1.py 4

9 / 10

slide-10
SLIDE 10

Modules

Example

Suppose the fjle file4.py contains the statements:

1

x = 94

2

print(x)

…and the fjle file3.py contains:

1

x = 1

2

from file4 import *

3

print(x)

…then, together, these produce:

>$ python3 file3.py 94 94

users.csc.calpoly.edu/~cesiu/csc101/slides/expressions.pdf

10 / 10