You are on page 1of 5

Consolidated Notes (Lesson_1)

Part 1: Python Expressions and Variables

1. Basic Python Operations


Arithmetic Operations:

Operators Example Functions


* 8*2 = 16 Multiply
** 8**2=64 Power
/ 8/2=4 Divide
// 8//2=4 Root
% 8%2=0 Modular  Remainder after a by b
8%3=2
print () Why do you have to print it out?
Makes python explicitly print out the line you are interested in.
X Container used to define a specific variable
# Makes python ignore the code, so that you can still keep it for future
reference
_ Used as word connectors when typing in code
+= Add itself

Other Useful Hotkey(s):

Operators Example Functions


Shift + Enter Runs Code
NA.
Ctrl shift p Shows all the hotkeys

2. Variables
a. Defining Variables
Named place for programmer to store data and later retrieve the data using the variable name

 Start with letter or underscore:


e.g. _underscore / underscore

 Remainder of variable name only can consist of letters, numbers and underscores:

 Variable Names are Case sensitive:


e.g. Note: Total is not equal to total

b. Rules for Variables


 Reserved Keywords cannot be used as variable names:

 Assessing Content  variables are symbolic names for your container  when you call a container,
you assess its contents

 Reassignment
You can change the content of a variable in a later statement by simply reassigning a new value to it

c. Data Types

Type Meaning/Form Eg
Integer Number 40
Float Real numbers with decimal point dividing 40.0
the integer and fractional parts
String Sequence of Characters “40”

d. Conversion Function

Function Meaning
Int(x) Convert x into an integer
float(x) Convert x into a float
Str(x) Convert x into a string
Note: A variable of one type can be converted into another type, provided if data can be converted 
results in type error

3. String Special Properties


a. String Slicing
Allows you to take out a set of values from a string

Example
fav_fruit[a:b] a= I start reading from here
b= I stop here (excluding this point)
Fav_fruit[a:b:c] c= steps
r.g. if you ask it to read
Note: Pythons starts with 0 + The conversion happens only if the data can be converted

b. String Concatenation
When you are adding certain things, they must be of the same type (e.g. string with string; integer with
integer)

Note: “ “ counts as a space

4. The Art of Debugging


a. Types of Error
a. Syntax: code does not follow rules of language
e.g. single quote is used when double quote is needed

b. Exceptions: program does not run as expected


e.g. ZeroDivisionError; NameError; TypeError

c. Logic Errors: program is correct from syntax perspective, but output is obviously wrong
e.g.

b. Basic Debugging Techniques

5. Print & Format Function


a. Line Endings
You can make python end with a character of your choice when using the print function by using the
optional ‘end’ argument in conjunct with the print function

b. Combining both line endings and separations


Beyond combining line endings, you can also choose the separators between the line endings
c. Escape Character
Allows python to treat the next character after the “\”
 Line Break (“\n”)

 Tab Break (“\t”)

d. Format Function
 Limiting Decimal Places

 Formatting Patterns

 Formatting Percentages

You might also like