Basic coding in Python: part 2.
[email protected]
Python
Compiled languages Interpreted languages
1. Uses compilers (translators) to input your code into machine code 1. Directly writes source code
2. Are faster 2. Are slower (each command is executed sequentially)
3. C, C++, Pascal 3. Python, MATLAB
Each command (instruction) contains the programmed data + information on task
Eg. Print(Welcome)
Python REPL
Read
Evaluate
Print
Loop: get ready for the next input
Python in Replit
1. Hello World
2. Strings & Variables
3.Booleans
4. Data Collections & Functions
5. Loops
Variables
A variable is a location in memory where a program can store a value.
Rule Examples
Must begin with a letter or underscore. x, y, course, exams etc
Other characters can be letters, numbers or underscore. X2, x_2 etc
Variables are case sensitive student is different than Student etc
Python has keyword/reserved words that you cannot use as False, class, finally, is, return, None, continue for, try, True, def,
variable names: from, while, and, not, with, as, elif, if, or, yield, assert, else,
import, except etc
Remember that strings were enclosed by single/double quotation marks: ”class” or “exam” etc
Variables
To assign a value to a variable, use the = sign
x=3 • Do not use spaces
name = “XYZ" • Do not start with a number
price = 100.50 • Do not start with a capital letter
Remember that strings were enclosed by single/double quotation marks: ”class” or “exam” etc
Resources scarce: delay in Console when hitting Run
Variables: overwriting other variables
Variables can be overwritten with other values
Hit
RUN
Input own number
String Concatenation
In Python, there are several ways to concatenate strings
1. Plus (+) sign: print("hello" + " " + "world")
2. Comma: print("hello","world")
3. Interpolation: print("{0} {1}".format("hello","world"))
4. By using variables: greeting = "hello“
who = “world“
print(f"{greeting} {who}")
Hit Enter to go to
line code below
String Concatenation
In Python, there are several ways to concatenate strings
By using variables: greeting = "hello“
who = “world“
print(f"{greeting} {who}")
The curly braces are used to initialize dictionaries in Python
String Concatenation
{} Curly braces are used to create a dictionary and sets.
() Brackets are used for order of operations
Assign String to a Variable
Use an = sign and the string
Assign Multiline Strings to a Variable
Assign a multiline string to a variable by using three double quotes OR three single quotes
Finding string length with len() function
You can Copy-paste the Latin text from the above command line
Slicing (cutting) strings
1. Slice in between
1. Specify the start index and the end index, separated by a colon (:), to return a part of the string
2. Slice From the Start
2. Leave out the start index, so that the range will start at the first character:
3. Slice To the End
3. Leave out the end index, the range will go to the end:
Modify Strings
Boolean (true/false) values + Conditional statements (if/else)
Boolean (true/false) values + Conditional statements (if/else)
Choose a number for a and b
Conditional Statements (if)
Always with indentation (obtained if you hit TAB key)
Hit twice
Note the indentation
ENTER
Next week we meet here