[go: up one dir, main page]

0% found this document useful (0 votes)
7 views35 pages

CSE111 Lecture2 F

The document provides an introduction to Python scripting, contrasting interactive and script modes, and outlines the structure of Python programs including sequential, conditional, and repeated steps. It discusses the importance of variable naming conventions, assignment statements, and operator precedence in Python. Additionally, it covers basic data types and expressions, emphasizing best practices for writing clear and maintainable code.
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)
7 views35 pages

CSE111 Lecture2 F

The document provides an introduction to Python scripting, contrasting interactive and script modes, and outlines the structure of Python programs including sequential, conditional, and repeated steps. It discusses the importance of variable naming conventions, assignment statements, and operator precedence in Python. Additionally, it covers basic data types and expressions, emphasizing best practices for writing clear and maintainable code.
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/ 35

CSE-111 Introduction Computer Science

Lecture 2
Python Scripts

• Interactive Python is good for experiments and programs of 3-4 lines long

• But most programs are much longer so we type them into a file and tell
python to run the commands in the file.

• In a sense we are “giving Python a script”

• As convention, we add “.py” as the suffix on the end of these files to


indicate they contain Python
Interactive vs. Script
Interactive Mode
•You type Python commands one by one and see immediate results.

•It's typically done in the Python shell or an interactive environment like:

◦Python's default terminal (>>>)

◦IPython

◦Jupyter Notebooks

Good for: Quick testing, Learning and experimenting, Small code snippets
Script Mode
You write your Python code in a file (ending with .py) and run the whole file at once.

•Typically done in an editor (like VS Code, PyCharm) or even simple text editors.

•VS Code stands for Visual Studio Code — it’s a free, open-source code editor made by
Microsoft. (Cross-platform — works on Windows, macOS, and Linux)
•Built-in features: Syntax highlighting (colors for code), Autocompletion (suggestions while
you type), Error checking, Debugging (finding and fixing errors)

Script Mode is good for: Larger programs, Projects, Anything you want to save and reuse
Program Steps and Program Flow

• Like a recipe or installation instructions, a program is a sequence of


steps to be done in sequence

• Some steps are conditional

• Sometimes a step or group of steps are to be repeated

• Sometimes we store a set of steps to be used over and over as


needed several places throughout the program
Sequential Steps

x=2 Program:
Output:
print(x) x = 2
print(x) 2
x=x+2 x = x + 2 4
print(x)
print(x)

When a program is running, it flows from one step to the next. As


programmers, we set up “paths” for the program to follow.
x=5
Conditional Steps
Yes
x < 10 ?

print('Smaller') Program:
No Output:
x = 5
Yes if x < 10: Smaller
x > 20 ? print('Smaller') Finish
if x > 20:
print('Bigger') print('Bigger')
No
print('Finish')

print('Finish')
Repeated Steps
n=5

No Yes Output:
n>0? Program:
5
print(n) n = 5 4
while n > 0 :
print(n)
3
n = n -1 n = n – 1 2
print('Blastoff!') 1
Blastoff!
Loops (repeated steps) have iteration variables that
print('Blastoff')
change each time through a loop.
When you start Python, you will see something
like:
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>

9
Python Programming, 3/e
Elements of Python

• Vocabulary / Words - Variables and Reserved words (Chapter 2)

• Sentence structure - valid syntax patterns (Chapters 3-5)

• Story structure - constructing a program for a purpose


Python Variable Name Rules

• Must start with a letter or underscore _


• Must consist of letters, numbers, and underscores
• Case Sensitive

Good: spam eggs spam23 _speed


Bad: 23spam #sign var.12
Different: spam Spam SPAM
Reserved Words

• You can not use reserved words as variable names / identifiers

import keyword
print(keyword.kwlist)

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def',
'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda',
'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Statements

x=2 Assignment Statement


x=x+2 Assignment with expression
print x Print statement
Variables
• A variable is a named place in the memory where a programmer can store
data and later retrieve the data using the variable “name”

• Programmers get to choose the names of the variables

• You can change the contents of a variable in a later statement

x = 12.2 x 12.2
y = 14
y 14
Variables
• A variable is a named place in the memory where a programmer can store
data and later retrieve the data using the variable “name”

• Programmers get to choose the names of the variables

• You can change the contents of a variable in a later statement

x = 12.2 x 12.2 100


y = 14
x = 100 y 14
Mnemonic Variable Names

• Since we programmers are given a choice in how we choose


our variable names, there is a bit of “best practice”

• We name variables to help us remember what we intend to store


in them (“mnemonic” = “memory aid”)

• This can confuse beginning students because well-named


variables often “sound” so good that they must be keywords
x1q3z9ocd = 35.0
x1q3z9afd = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd
print(x1q3p9afd)

What is this bit of


code doing?
x1q3z9ocd = 35.0 a = 35.0
x1q3z9afd = 12.50 b = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd c = a * b
print(x1q3p9afd) print(c)

What are these bits


of code doing?
x1q3z9ocd = 35.0 a = 35.0
x1q3z9afd = 12.50 b = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd c = a * b
print(x1q3p9afd) print(c)

hours = 35.0
What are these bits rate = 12.50
of code doing? pay = hours * rate
print(pay)
Assignment Statements

• We assign a value to a variable using the assignment statement (=)

• An assignment statement consists of an expression on the


right-hand side and a variable to store the result

x = 3.9 * x * ( 1 - x )
x 0.6
A variable is a memory location
used to store a value (0.6)
0.6 0.6
x = 3.9 * x * ( 1 - x )

0.4

The right side is an expression.


0.936
Once the expression is evaluated, the
result is placed in (assigned to) x.
x 0.6 0.936

0.6 0.6
x = 3.9 * x * ( 1 - x )
A variable is a memory location used to
store a value. The value stored in a 0.4
variable can be updated by replacing the
old value (0.6) with a new value (0.936).
0.936
The right side is an expression. Once the
expression is evaluated, the result is
placed in (assigned to) the variable on the
left side (i.e., x).
Expressions…
Numeric Expressions

• Because of the lack of mathematical Operator Operation


symbols on computer keyboards - we + Addition
use “computer-speak” to express the
- Subtraction
classic math operations
* Multiplication
• Asterisk is multiplication / Division

• Exponentiation (raise to a power) looks ** Power


different than in math % Remainder
Numeric Expressions
>>> xx = 2 >>> jj = 23
>>> xx = xx + 2 >>> kk = jj % 5 Operator Operation
>>> print(xx) >>> print(kk)
+ Addition
4 3
>>> yy = 440 * 12 >>> print(4 ** 3) - Subtraction
>>> print(yy) 64 * Multiplication
5280
>>> zz = yy / 1000 4R3 / Division

>>> print(zz) 5 23 ** Power


5.28 20 % Remainder

3
Order of Evaluation
• When we string operators together - Python must know which one
to do first

• This is called “operator precedence”

• Which operator “takes precedence” over the others?

x = 1 + 2 * 3 - 4 / 5 ** 6
Operator Precedence Rules
Highest precedence rule to lowest precedence rule:

– Parentheses are always respected Parenthesis


Power
– Exponentiation (raise to a power) Multiplication
Addition
– Multiplication, Division, and Remainder
Left to Right
– Addition and Subtraction

– Left to right
1 + 2 ** 3 / 4 * 5
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print(x)
11.0 1 + 8 / 4 * 5
>>>
1 + 2 * 5
Parenthesis
Power
Multiplication 1 + 10
Addition
Left to Right 11
Operator Precedence
Parenthesis
• Remember the rules top to bottom Power
Multiplication
Addition
• When writing code - use parentheses Left to Right

• When writing code - keep mathematical expressions simple


enough that they are easy to understand

• Break long series of mathematical operations up to make them


more clear
Data Types
• The “>>>” is a Python prompt indicating that Python is ready for us to
give it a command. These commands are called statements.
• >>> print("Hello, world")
Hello, world
>>> print(2+3)
5
>>> print("2+3=", 2+3)
2+3= 5
>>>

35
Python Programming, 3/e

You might also like