string concatenation only works when both operands are strings
Let's repeat: string concatenation requires both operands to be strings
Let's Fix It!
You can change from one type to another using functions of the same name as the type you are trying to convert to. Let's look at what autocomplete says about the following functions and demo them. →
int(), str(), and float()
unlike print, these functions return a value to your program!
this is called type conversion or casting
And This Helps… How?
Let's fix our previous string concatenation →
(of course, this is contrived, since we know that 3 is an int, and we could have just wrapped it in quotes: '3'… but imagine if we didn't know the type beforehand!)
Review
what operators do we use for exponentiation and integer division?
what does -10//3 give back?
what's an easy way of creating a string that has 100 exclamation points in it?
what's string concatenation?
A Quick Summary of Operators for Numeric Types
Name as many operators that work on numeric types as you can: →
+ - addition
- - subtraction
* - multiplication
/ - division (always gives back float)
// - integer division
% - modulo (remainder)
** - exponentiation
A Quick Summary of String Operators
Name as many operators that work on strings as you can: →
+ - concatenation (addition), only works when both operands are strings
* - repetition (multiplication), only works when one operand is a str and the other is an int
Variables
What's a Variable?
variable - name that refers to a value
this terminology is important; very specific… name and value
we can now use that name instead of the explicit value
sometimes you'll hear me say the string "literal" - representation of a value within a program… i mean… the thing in quotes
So How Do Variables Actually Work?
this is an assignment statement - binds a value to a name
the equals sign assignment token - the "operator" that we use to bind a name to a value
name on left
value on right
eeezy…. just like maths
Another Aside - Interactive Shell
Whenever you type something in the interactive shell, it will always return a value. →
a value returns a value
a variable returns a value
a function can return a value
if a function doesn't actually return a value, like print, the resulting value will be None (NoneType)
Some More Miscellaneous Comments
how can I tell if something is printed vs a value is returned?
returning a string shows the string in quotes, while printing displays the string without quotes
confusing for other types, just know that each line gives back a value to your program, and consequently that value gets shown after each line is entered
another btw, metasyntactic variables:
typical to use nonsense names such as foo, bar, baz as variable names in examples
don't use these names, though; use more meaningful names when you write programs!
Variables That Aren't Defined Yet
what happens when you use a variable that doesn't exist? →
More About Reassignment
you can reassign or rebind
let's see that in action →
Naming Variables
you can make them as long as you want… though I suppose it could crash your computer
what's an easy way to create a long variable name? →
btw, you can autocomplete in the shell by using tab →
names can only consist of alphanumeric (numbers and letters) characters and the underscore
the first character has to be a letter or an underscore
case sensitive - case matters
the name can't be a keyword or reserved word
Am I a Valid Name?
Which of the following are valid variable names in Python?
_foo
1_foo
foo
1foo
$foo
1 and 3 are valid variable names.
Let's Actually Use Some Variables
Try the following on your own: →
create a variable called exclaim, set it eqaul to "!!!" and print out the variable
create 2 variables, length and width…
set them both equal 25 and 8 respectively
multiply both variables and and store the result in a variable called area