Python Basics

20 Pages
173 Views

Department
Computer Science
Course Code
CSCA08H3
Professor
Kathy Fellowes

This preview shows pages 1,2,3,4. Sign up to view the full 20 pages of the document.
Description
Python Basics Assignment Statement Form: variable = expression How it’s executed: 1.Evaluate the expression on the RHS. (The value of the expression is a memory address.) 2.Store that memory address in the variable on the LHS. Assignment Statement Form: Rememberriable = expression this! How it’s executed: 1.Evaluate the expression on the RHS. (The value of the expression is a memory address.) 2.Store that memory address in the variable on the LHS. Python T ypes English Python Every Python value • has a type that integer int describes what sortal” number float of value it is picture Picture • Built-in function pixel Pixel type will tell you the Color expression colour string of letters str Assignment vs Equality • Python variables look like math variables. • This could be Python or math: p = 5 q = p ∗ 7 • But “=” in math means equality (stating a fact) whereas “=” in Python means assignment (asking Python to do something) • This makes a big difference! 1. Changeability In math, this is inconsistent: • p = 5 q = p ∗ 7 p = q + 10 • p can’t be both 5 and 45! But in Python, it makes perfect sense. p • starts out referring to 5, but then changes to refer to 45. • You can change a variable’s value as many times as you wantou can even change its type. In math, this makes no sense either: • x = x + 1 It can’t be true! • But in Python, it makes perfect sense. It is asking to make x refer to a something that is one bigger. • We say “x is assigned x + 1” or “x gets x + 1” • Programming languages usually have different symbols for assignment and equalit. Python uses “==” for equal.ty 2. Can’t tie two variables • What does this do? x = 37 y = x + 2 # y is now 39. x = 20 # Is y now 22? • You can’t use assignment to tie the values of two variables together permanently. 3. Assignment is not symmetric In math In Python sum = a + b they mean fine the same thing a + b = sum illegal Naming Rules for the format of names • There are a few rules about names of variables (and other things we’ll see later): Must start with a letter (or underscore). • • but nothing else.rs, digits, and underscores, • And case matters, by t.e way age = 11 aGe # Error! This is not defined. Valid: _moo_cow, cep3, I_LIKE_TRASH • • Invalid: 49ers, @home Conventions for the format of names • thEre’S a Go
More Less
Unlock Document

Only pages 1,2,3,4 are available for preview. Some parts have been intentionally blurred.

Unlock Document
You're Reading a Preview

Unlock to view full version

Unlock Document

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit