1. Trang chủ
  2. » Tất cả

02-Expressions

32 197 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Expressions
Trường học Standard University
Chuyên ngành Computer Science
Thể loại Bài luận
Năm xuất bản 2023
Thành phố standard city
Định dạng
Số trang 32
Dung lượng 246,07 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Python Variable Name Rules• Good: spam eggs spam23 _speed • Different: spam Spam SPAM... Reserved Wordsand del for is raise assert elif from lambda return break else global not try class

Trang 1

Variables, Expressions, and

Statements

Chapter 2

Trang 2

• Fixed values such as numbers, letters, and strings are called

“ constants ” - because their value does not change

• String constants use single-quotes (')

Trang 3

• A variable is a named place in the memory where a programmer can

12.2 x

14 y

x = 12.2

x = 100

Trang 4

Python Variable Name Rules

• Good: spam eggs spam23 _speed

• Different: spam Spam SPAM

Trang 5

Reserved Words

and del for is raise assert elif from lambda return break else global not try class except if or while continue exec import pass yield

def finally in print

Trang 7

Assignment Statements

• An assignment statement consists of an expression on the right hand

x = 3.9 * x * ( 1 - x )

Trang 8

x = 3.9 * x * ( 1 - x )

0.6 x

Left side is an expression Once

expression is evaluated, the result

is placed in (assigned to) x

0.40.93

A variable is a memory location

used to store a value (0.6)

Trang 9

x = 3.9 * x * ( 1 - x )

0.6 0.93 x

Right side is an expression Once

expression is evaluated, the result

is placed in (assigned to) the

variable on the left side (i.e x)

0.93

A variable is a memory location

used to store a value The value

stored in a variable can be updated

by replacing the old value (0.6)

with a new value (0.93)

Trang 10

Numeric Expressions

symbols on computer keyboards - we

use “computer-speak” to express the

classic math operations

different from in math

Trang 11

3

Trang 12

Order of Evaluation

to do first

Trang 13

Operator Precedence Rules

Parenthesis Power

Multiplication

Addition

Left to Right

Trang 14

Parenthesis Power

Trang 15

Note 8/4 goes before 4*5 because of the left-right

rule

Trang 16

Operator Precedence

that they are easy to understand

clear

Parenthesis Power

Multiplication

Addition

Left to Right

Exam Question: x = 1 + 2 * 3 - 4 / 5

Trang 17

Python Integer Division is Weird!

floating point numbers

>>> print 10 / 25

>>> print 9 / 24

>>> print 99 / 1000

>>> print 10.0 / 2.05.0

>>> print 99.0 / 100.00.99

This changes in Python 3.0

Trang 18

Mixing Integer and Floating

operation where one

operand is an integer and the

other operand is a floating

point the result is a floating

point

floating point before the

operation

>>> print 99 / 1000

>>> print 99 / 100.00.99

>>> print 99.0 / 1000.99

>>> print 1 + 2 * 3 / 4.0 - 5-2.5

>>>

Trang 19

What does “ Type ” Mean?

between an integer number and a

string

if something is a number and

“concatenate” if something is a

string

>>> ddd = 1 + 4

>>> print ddd5

>>> eee = 'hello ' + 'there'

>>> print eeehello there

concatenate = put together

Trang 20

Type Matters

everything is

• You cannot “add 1” to a string

Trang 21

Several Types of Numbers

1, 100, 401233

parts: -2.5 , 0.0, 98.6, 14.0

variations on float and integer

Trang 22

Type Conversions

floating point in an expression

converted to a float

built in functions int() and float()

>>> print float(99) / 1000.99

>>> type(f)

<type 'float'>

>>> print 1 + 2 * float(3) / 4 - 5-2.5

>>>

Trang 23

String Conversions

float() to convert between

strings and integers

string does not contain

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

TypeError: cannot concatenate 'str' and 'int'

>>> ival = int ( sval )

>>> type ( ival )

<type 'int'>

>>> print ival + 1 124

>>> nsv = 'hello bob'

>>> niv = int ( nsv )

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

ValueError: invalid literal for int()

Trang 24

User Input

pause and read data from

the user using the

raw_input function

• The raw_input function

returns a string

nam = raw_input (‘Who are you?’)

print 'Welcome', nam

Welcome Chuck

Trang 25

Converting User Input

number from the user, we

must convert it from a

string to a number using a

type conversion function

bad input data

inp = raw_input (‘Europe floor?’)

usf = int ( inp ) + 1

print “US floor”, usf

Europe floor? 0

US floor 1

Trang 26

Comments in Python

Trang 27

# Get the name of the file and open it name = raw_input("Enter file:")

for word,count in counts.items():

if bigcount is None or count > bigcount: bigword = word

bigcount = count

# All done

print bigword, bigcount

Trang 28

String Operations

with a string or a number and

behaves appropriately

>>> print 'abc' + '123' abc123

>>> print 'Hi' * 5 HiHiHiHiHi

>>>

Trang 29

Mnemonic Variable Names

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

variables often “sound” so good that they must be keywords

http://en.wikipedia.org/wiki/Mnemonic

Trang 30

a = 35.0

b = 12.50

c = a * b print c

What is this

code doing?

Ngày đăng: 08/03/2013, 15:55

Xem thêm

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w