SUMMARY
DAY 1
String Manipulation:
Double & Single Quotes:
print('String concatenation is done with the "+" sign')
---> single then double then single
String concatenation is done with the "+" sign
print("String concatenation is done with the \"+\" sign")
---> double then ignore the double quotes as code, but keep it, add the +, ignore double quotes as code and keep the double quotes and end the double quotes.
String concatenation is done with the "+" sign
New Lines and Multi-lines
print("Hello World\nHello World\nHello World")
---> \n places at the end of the word
Hello Wallis
Hello Wallis
Hello Wallis
message = """This is a
multi-line string in Python"""
print(message)
---> Triple double quotes OR triple single quotes to use multi-lines
This is a
multi-line string in Python
text = '''She said, "It's a great day!"'''
---> Triple single quotes to avoid escape characters
She said, "It's a great day!"
Concatenated
print("Hello " + "Wallis, " + "How are you?")
---> add space to get normal text
Hello Wallis, How are you?
print("Hello" + " " + "Wallis," + " " + "How are you?")
add a '+' sign between and double quotes
Hello Wallis, How are you?
Input Functions
print("Hello " + input("What is your name?"))
---to enter data into Python
print(len(input("What is your name?\n ")))
Wallis Short
12
Actually the name has 11 characters. To remove the white space between
name = input("What is your name?\n")
print(len(name.replace(" ", "")))
Wallis Short
11
Variables
name = input("What is your name?\n")
length = len(name.replace(" ", ""))
print(f"Hello {name}\nYour name has {length} characters in it")
What is your name?
Leonardo da Vinci
Hello Leonardo da Vinci
Your name has 15 characters in it
Variable Naming - PEP8
OIL - nevewr use any of these as single character variable names (Uppercase O, Uppercase I or lowercase el )
A-Z, a-z, 0-9 and undersocre _ , all these can all be used in varaibles however a digit cannot be at the beginning
Most popular used are:
Camel Case - camelCaseUsage - First word is lowercase, and each following word starts with a capital letter.
Pascal Case - PascalCaseUsage - Every word starts with a capital letter (including the first one).
Snake Case - snake_case_usage - All letters are lowercase, words are separated by underscores.
To check for reserved words, you can use:
help("keywords")
Which One Should You Use?
🔹 Python: Use snake_case
for variables/functions, PascalCase
for classes.
=========================================================================
DAY 2 - Data Types & String Manipulation
Data Types
Strings
"Hello" is a string - think "string of pearls" - each letter joined with another letter to form a string of letters.
We can identify and pull out an individual letter within the string - called SUBSCRIPTING
print ("Hello" [1] )
e The 'e' = position [1] - Left to Right (binary, start from 0) The 'e' = position [-4] - Right to Left (no binary, start from 1)
Integers - Whole Numbers
Floats - Numbers with decimal points
Boolean - True or False (Cpaital 'T' of 'F') - No quotes around them
Type Checking and Type Conversion
Functions
num_char = len(input("What is your name? ")) print("Your name has " + num_char + "characters")
What is your name? Wallis print("Your name has " + num_char + "characters") TypeError: can only concatenate str (not "int") to str
From the below output we get a TypeError saying you cannot concatenate integers and strings but you can on strings only
Type Checking
print(type(len(input("What is your name")))) <class 'int'>
---> Here we check what TYPE of class this is and it shows its an INTEGER
Type Conversion
num_char =
str
(len(input("What is your name? ")))
---> here we change the int to a str
print("Your name has " + num_char + "characters")
two_digit_number = input("Type in any 2 digit number: \n")
first_number = two_digit_number [0]
---> this type is a 'string' as shown below
print(type(first_number)
second_number = two_digit_number [1]
---> this type is a 'string' as shown
print(int(first_number) + int(second_number))
---> convert strings to integers
Type in any 2 digit number: 35 <class 'str'> shows us that this type is a 'string' 8
Mathematical Operations
Floor Division(//)
Floor division divides 2 numbers and returns the LARGEST integer less than or equal to the result, ie, it rounds down the result to the nearest WHOLE number.
7//2 # Result 3
---> this gives 3 because that's the largest integer less than or = to 3,5-7//2 #Result: -4 (rounded DOWN )
---> rounds down to the lower integer with negative numbers
Quotient
The quotient refers to the integer part of a division result, ignoring the remainder. Its the same as floor division
Remainder (%) Modulo
The modulo operator is carried out with a percent symbol (%) and is used to get the remainder of a division. For example:
print(20 % 6) 20 // 6 = 3 3*6 = 18 20-18 = 2 Modulo = 2.0
print(1.25 % 0.5) 1.25 // 0.5 = 2.0 2.0 * 0.5 = 1.0 1.25 -1 = 0.25 Modulo = 0.25
print(12 % 4.5) 12 // 4.5 = 2.0 2 * 4.5 = 9.0 12 - 9.0 = 3.0 Modulo = 3.0
Operators and their Binding
Don't forget PEMDAS
(Parentheses, Exponents, Multiplication and Division, Addition and Subtraction)
We do this fro LEFT to RIGHT
print(9 % 6 % 2)
---> starting from left hand side like PEMDAS
9 // 2 = 4.0
4 * 2 = 8
9 - 8 = 1 .0
so:
print(1 % 2)
1 //2 = 0.5
0.5 * 2 = 1
Modulo = 1 ----> not sure why this is an integer and not a float
because if I do
print(type(9 % 6 % 2))
<class 'int'>
Last updated
Was this helpful?