You are on page 1of 3

The Shell

- This is a place for running small bits of Python code

- Constantly ask yourself "What would happen if I ran X?" and then immediately answer that question by
running it!

Navigating Shell History

- Up and Down Arrow Key

Strings

- are essentially snippets of text.

- A string is a sequence of characters. A character is a single symbol such as a letter, number,


punctuation, space, etc.

- Ex. 'hello'

- Quotes are not part of the string

- Strings are inside of the quotes

Adding Strings

- Strings can be added together using + (very different from adding numbers)

- Ex. 'hello' + 'world'

- + combines or joins two strings together end to end. This is called concatenation

Introducing Variables

- a way to refer to values that are unknown ahead of time and can change - values that can vary

- Ex. word = Hello; word    is a variable, 'word' is not. Quotes within a string makes the difference.

- 'word' is literally just 'word' because the string has no value. It is called string literal

- Always define a variable, a word is a variable if it has value in python.

Using Variables and print( )

- an underscore is used to separate words when you want a variable name containing multiple
words.
- You can use variables in calculations just like you would use literals. Example: ‘Hello’ +
your_name

- Variables can also change their values over time.

- PRINT = print(<something>) displays <something> in the shell. It displays the actual


content of strings that we usually care about, instead of a representation of strings that's
suitable for code which has things like quotes.

Writing Programs

- Editor is a place where you can writ and run longer programs.

- Editor is where real programs live.

- Python runs each line one at a time from top to bottom

- when print is used multiple times, each thing is printed on its own line.

- The program runs in the shell = meaning that the variables defined in the program
now exist in the shell with the last values they had in the program. This lets you explore
in the shell after the program completes

- Programs run in isolation - they don't depend on any previously defined variables. The
shell is reset and all previous variables are cleared.

- If you enter code in the shell and it has a value, that value will automatically be
displayed. That doesn't happen for programs in the editor - you have to print
values. If you remove print() from the program, changing the two lines to
just word + ' ' + name, nothing will be displayed.

Storing Calculations In Variables


- Often you will use variables to store the results of calculations. This will help to build
more complex programs.
- Ex.:
o word = 'Hello'
o name = 'World'
o sentence = word + ' ' + name
o print(sentence)
- Now sentence has the value 'Hello World' which can be used multiple times.
Note that it will continue to have this value until it is directly reassigned, e.g. with
another statement like sentence = <something>

- Unlike a spreadsheet where formulas update automatically, a variable


like sentence doesn't remember how it was calculated and won't change if the
underlying values word or name are changed.

Introducing For Loops

You might also like