expressions statements and programs
play

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


  1. Expressions, Statements, and Programs CSC 101 Christopher Siu 1 / 10

  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

  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

  4. Data Types Defjnition A data type determines how to interpret a value and what operations 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

  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

  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

  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

  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

  9. Modules 1 4 >$ python3 file1.py …then, together, these produce: print(x) 2 from file2 import * …and the fjle file1.py contains: Python fjles can be broken into multiple modules . x = 4 1 Suppose the fjle file2.py contains the statements: Example from <file> import <defns, ...> import <file> Modules can be combined together using: 9 / 10

  10. Modules from file4 import * users.csc.calpoly.edu/~cesiu/csc101/slides/expressions.pdf 94 94 >$ python3 file3.py …then, together, these produce: print(x) 3 2 Example x = 1 1 …and the fjle file3.py contains: print(x) 2 x = 94 1 Suppose the fjle file4.py contains the statements: 10 / 10

Recommend


More recommend