#### EXAMPLE OF CREATING A VARIABLE ####
# You use # to add comment, it won’t run or affect the code
# You use = to assign a value to the name of the variable.
# Each code starts on the new line. No semicolon or {}
var_name = 1
str_var2 = 'Hundred'
int_var = 10
float_var = 12.8
type(int_var)
type(float_var)
# Numeric Operations
# Addition
1 + 100
# Multiplication
1 * 100
# Division
1 / 100
# Modular (%)
# This is the remainder or a value remaining after dividing two numbers
# 100 / 1 = 100, remainder is 0
100 % 1
1 % 100
10 % 2
# Powers
# 1 power any number is 1 always
1 ** 100
2 ** 2
# We use print() to display the results of the operations or a variable
print(1 + 100)
str_var = 'One'
str_var2 = 'Hundred'
str_var + str_var2
str_var + ' ' + 'and' + ' '+ str_var2+ '.'
## We can use print() to display a string
print(" This is a string")
sentence = 'This is a String'
# Case capitalization
# It return the string with first letter capitalized and the rest being lower cases.
sentence.capitalize()
# Given a string, convert it into title (each word is capitalized)
another_sentence = 'this is a string to be titled'
another_sentence.title()
# Converting the string to upper case
sentence.upper()
# Converting the string to upper case
sentence.lower()
# Splitting the string
sentence.split()
# Example of list ####
week_days = ['Mon', 'Tue', 'Wed', 'Thur','Fri']
even_numbers = [2,4,6,8,10]
mixed_list = ['Mon', 1, 'Tue', 2, 'Wed', 3]
#### Accessing the elements of the list
week_days[0]
print(even_numbers[2])
print(even_numbers[-1])
# Slicing the list
week_days = ['Mon', 'Tue', 'Wed', 'Thur','Fri']
week_days[0:2]
week_days[-4:]
# -1 starts at 'Fri', -2 to `Thur'..... -4 to 'Tue'
# Updating the value of the list
even_numbers = [2,4,6,8,10]
# Change the value at index or position 3 (position starts at 0) to 12
even_numbers[3] = 12
even_numbers
even_numbers[2:4] = [16,20]
even_numbers
# Adding the new elements in list
odd_numbers = [1,3,5,7,9]
odd_numbers = [1,3,5,7,9]
odd_numbers.append(11)
odd_numbers.append(13)
odd_numbers
## Concatenating two lists
a = [1,2,3]
b = [4,5,6]
c = a + b
c
# Nesting a list in other list
nested_list = [1,2,3, ['a', 'b', 'c']]
nested_list[3]
# Calculating the length of the list
odd_numbers = [1,3,5,7,9]
len(odd_numbers)
# Sorting a list
even_numbers = [2,14,16,12,20,8,10]
even_numbers.sort()
even_numbers
# Reversing a string
even_numbers.reverse()
even_numbers
# Adding other elements to a list
even_numbers = [2,14,16,12,20,8,10]
even_numbers.append(40)
even_numbers
# Removing the first element of a list
even_numbers.remove(2)
even_numbers
## Return the element of the list at index x
even_numbers = [2,14,16,12,20,8,10]
## Return the item at the 1st index
even_numbers.pop(1)
# pop() without index specified will return the last element of the list
even_numbers = [2,14,16,12,20,8,10]
even_numbers.pop()
# Count a number of times an element appear in a list
even_numbers = [2,2,4,6,8]
even_numbers.count(2)
# Example of Dictionary
# Countries codes: https://countrycode.org
countries_code = { "United States": 1,
"India": 91,
"Germany": 49,
"China": 86,
"Rwanda":250
}
countries_code
countries_code.items()
countries_code.keys()
countries_code.values()
countries_code['India']
countries_code['Rwanda']
# Adding new country and code
countries_code['Cuba'] = 53
countries_code
tup = (1,4,5,6,7,8)
# Indexing
tup[4]
## Tuples are not changeable. Running below code will cause an error
# tup[2] = 10
# You can not also add other values to the tuple. This will be error
#tup.append(12)
set_1 = {1,2,3,4,5,6,7,8}
set_1
set_2 = {1,1,2,3,5,3,2,2,4,5,7,8,8,5}
set_2
# List Vs Set
odd_numbers = [1,1,3,7,9,3,5,7,9,9]
print("List:{}".format(odd_numbers))
print("#####")
set_odd_numbers = {1,1,3,7,9,3,5,7,9,9}
print("Set:{}".format(set_odd_numbers))
## Greater than
100 > 1
## Equal to
100 == 1
## Less than
100 < 1
## Greater or equal to
100 >= 1
## Less or equal to
100 <= 1
'Intro to Python' == 'intro to python'
'Intro to Python' == 'Intro to Python'
100 == 100 and 100 == 100
100 <= 10 and 100 == 100
100 == 10 or 100 == 100
100 == 10 or 100 == 10
not 1 == 2
not 1 == 1
if 100 < 2:
print("As expected, no thing will be displayed")
if 100 > 2:
print("As expected, no thing will be displayed")
if 100 < 2:
print("As expected, no thing will be displayed")
else:
print('Printed')
# Let's assign a number to a variable name 'jean_age' and 'yannick_age'
john_age = 30
luck_age = 20
if john_age > luck_age:
print("John is older than Luck")
else:
print(" John is younger than Luck")
# Let's use multiple conditions
john_age = 30
luck_age = 20
yan_age = 30
if john_age < luck_age:
print("John is older than Luck")
elif yan_age == luck_age:
print(" Yan's Age is same as Luck")
elif luck_age > john_age:
print("Luck is older than John")
else:
print("John's age is same as Yan")
# Example 1: Return 'Even' if below num is 'Even' and `Odd` if not.
num = 45
'Even' if num % 2 == 0 else 'Odd'
# Example 2: Return True if a given element is in a list and False if not
nums = [1,2,3,4,5,6]
True if 3 in nums else False
even_nums = [2,4,6,8,10]
for num in even_nums:
print(num)
week_days = ['Mon', 'Tue', 'Wed', 'Thur','Fri']
for day in week_days:
print(day)
sentence = "It's been a long time learning Python. I am revisiting the basics!!"
for letter in sentence:
print(letter)
sentence = "It's been a long time learning Python. I am revisiting the basics!!"
# split is a string method to split the words making the string
for letter in sentence.split():
print(letter)
# For loop in dictionary
countries_code = { "United States": 1,
"India": 91,
"Germany": 49,
"China": 86,
"Rwanda":250
}
for country in countries_code:
print(country)
for code in countries_code.values():
print(code)
for number in range(10):
print(number)
for number in range(10, 20):
print(number)
letters = []
for letter in 'MachineLearning':
letters.append(letter)
letters
letters = [letter for letter in 'MachineLearning']
letters
a = 10
while a < 20:
print('a is: {}'.format(a))
a = a + 1
# Function to add two numbers and return a sum
def add_nums(a,b):
"""
Function to add two numbers given as inputs
It will return a sum of these two numbers
"""
sum = a+b
return sum
add_nums(2,4)
add_nums(4,5)
# Displaying the doc string noted early
print(add_nums.__doc__)
def activity(name_1, name_2):
print("{} and {} are playing basketball!".format(name_1, name_2))
activity("Chris", "Francois")
activity("Kennedy", "Kleber")
## Sum of two numbers
def add_nums(a,b):
sum = a+b
return sum
add_nums(1,3)
sum_of_two_nums = lambda c,d: c + d
sum_of_two_nums(4,5)
# using len() to count the length of the string
message = 'Do not give up!'
len(message)
odd_numbers = [1,3,5,7]
len(odd_numbers)
# Using max() to find the maximum number in a list
odd_numbers = [1,3,5,7]
max(odd_numbers)
# Using min() to find the minimum number in a list
min(odd_numbers)
# Sorting the list with sorted()
odd_numbers = [9,7,3,5,11,13,15,1]
sorted(odd_numbers)
def cubic(number):
return number ** 3
num_list = [0,1,2,3,4]
# Applying `map` to the num_list to just return the list where each element is cubed...(xxx3)
list(map(cubic, num_list))
def odd_check(number):
return number % 2 != 0
# != is not equal to operation
num_list = [1,2,4,5,6,7,8,9,10,11]
list(filter(odd_check, num_list))
# Given a list of numbers, can you make a new list of even numbers from the list nums?
# Even numbers are numbers divisible by 2, and they give the remainder of 0
nums = range(1,20)
even_nums = []
# A traditional way to do it is:
for num in nums:
if num % 2 == 0:
even_nums.append(num)
print(even_nums)
even_nums = [num for num in nums if num % 2 == 0]
print(even_nums)
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
day_T = []
# Make a list of days that start with `T`
for day in days:
if day[0] == 'T':
day_T.append(day)
print(day_T)
day_T = [day for day in days if day[0] == 'T']
print(day_T)
seasons = ['Spring', 'Summer', 'Fall', 'Winter']
list(enumerate(seasons))
list(enumerate(seasons, start=1))
class_names = ['Rock', 'Paper', 'Scissor']
for index, class_name in enumerate(class_names, start=0):
print(index,'-',class_name)
name = ['Jessy', 'Joe', 'Jeannette']
role = ['ML Engineer', 'Web Developer', 'Data Engineer']
zipped_name_role = zip(name, role)
zipped_name_role
list(zipped_name_role)