Pyhton Foundation 1
Pyhton Foundation 1
Exercise : 1
Write a print function that prints "I am new to python!"
Help Command
• shows the description about the object that is given to the command
• lets see how the help() command works for the print() command
help(print)
Comments
• comments are text in code that is not executed by the interpreter
– in Python, comments begin with #
• comments may be used creatively to comment out pieces of code to
stop the interpreter from running it
© 2024 TECHBLUME. ALL RIGHTS RESERVED.
• shortcut to toggle comment on a line – CMD + / (macOS)
– Ctrl + / (Windows)
Exercise : 2
To the program where you printed out, I am Learning python
Variables
Let's try making a variable that contains your name then print it !
Exercise : 3
Create a variable as called age and add your age to it Next print out your
age in your notebook !
Simple Program
• input: uses the input command
• output: used the print command
Let's create input function that not only takes the name of the user but also
their age and then let's print it
Exercise : 4
Create two inputs with one taking the name and the other taking the age
And then print out Hey <name> I am glad you started learning python at
the age of <age>
x = 7 # 7 is an integer
type(x)
type(x)
# define a string
python_string = 'TECHBLUME is a awesome Bootcamp!'
# check type
type(python_string)
Exercise : 5
• discount = 43.8
• height = 143
• address = '#301, Pelican Street'
Sequence Types
# define a list
print(list_example)
print(len(list_example))
# check type
type(list_example)
Tuple
# define a tuple
tuple_example = (5,'11','a',2.86)
print(tuple_example)
print(len(tuple_example))
# check type
type(tuple_example)
Lists Tuples
ordered group of comma ordered group of comma
separated values in square separated values in parenthesis
brackets
square brackets are mandatory parenthesis not mandatory
mutable immutable
once created, content changes once created, content cannot be
can be made changed
should be chosen if content is chosen if content is fixed and
not fixed, and keeps changing never changes
cannot be used as keys for can be used as keys for
dictionaries dictionaries
are not hashable and are hashable and immutable
immutable
Exercise : 6
Make a list of your favourite colour, food and place and save it in a list
called my_favs
# define a range
range_example = range(7)
print(range_example)
print(len(range_example))
# check type
type(range_example)
range_example = range(0,20,2)
print(range_example)
print(len(range_example))
# check type
type(range_example)
Exercise : 7
Using the range function from 4 to 40 and then check the length
Mapping Types
• dictionary is a mapping data type
– it is mutable
– unordered
print(b)
# check type
type(b)
What can we do with dictionaries?
Exercise : 8
Boolean Types
• state of truth
–may be True or False
boolean = True
# check type
type(boolean)
boolean = False
# check type
type(boolean)
set_of_numbers = {0,9,1,5,7}
print(set_of_numbers)
names_set = set(names)
unique_names = list(names_set)
print(unique_names)
Operators
• popular kinds of operators
– numeric operators
– comparison operators
– logical operators
– assignment operators
– identity operators
– membership operators
– bitwise operators
• some are discussed below
Numeric Operators
•used with numeric data types to perform mathematical operations
– +: addition
– -: subtraction
– *: multiplication
– /: division
– //: floor division
x = 5 # integer value
#(run this cell to initialize values for the next few cells!)
# sum of x and y
print(x + y)
# difference of x and y
print(x - y)
# product of x and y
print(x * y)
# quotient of x and y
print(x / y)
print(x // y)
# remainder of x / y
print(x % y)
# absolute value
print(abs(z))
print(x ** y)
print(round(3.7))
print(bin(37))
Operators are the lifelines of any programming language, let’s put them
to good use
Exercise : 9
Comparison Operators
•used to compare two values
– <: less than
– >: greater than
– <=: less than or equal to
– >=: greater than or equal to
– ==: equality
– !=: inequality
i=3
j=7
print(i > j)
i=3
j=7
print(i >= j)
# Less Than
i=3
j=7
print(i < j)
i=3
j=7
print(i <= j)
# Equality Test #1
i=3
j=7
# Equality Test #2
i=3
j=3
print(i == j)
# Inequality Test #1
i=3
j=7
print(i != j)
# Inequality Test #2
i=3
j=3
print(i != j)
i=3
j=7
print(i > j)
i=3
j=7
print(i >= j)
# Less Than
i=3
j=7
print(i < j)
i=3
j=7
print(i <= j)
# Equality Test #1
i=3
j=7
print(i == j)
# Equality Test #2
i=3
j=3
print(i == j)
# Inequality Test #1
i=3
print(i != j)
# Inequality Test #2
i=3
j=3
print(i != j)
Exercise : 10
In the last exercise you created a program to get the area of a circle ,
create a print function that checks if the area that your program created is
bigger than 50 or not
not Operator
# ‘not’ operator
a = True
b = False
print( not a)
print( not b)
a = True
b = True
print( a and b)
# only a is true
a = True
b = False
print( a and b)
# only b is true
a = False
b = True
print( a and b)
a = False
b = False
print( a and b)
a = True
b = True
print( a or b)
# only a is true
a = True
b = False
print( a or b)
# only b is true
a = False
b = True
print( a or b)
a = False
b = False
print( a or b)
operators
Exercise : 11
Create a print function for our circle area generator to check if the area is
between 20 AND 50
Assignment Operators
• used to assign values to variables
–=
• following operators combine numerical operations at the same time
as assignment
– +=
– -=
– *=
– /=
– //=
– %=
– **=
x = 21
print(x)
# += operator
x += 3
print(x)
# -= operator
x -= 3
print(x)
# *= operator
x *= 3
print(x)
# /= operator
x /= 3
print(x)
# //= operator
x //= 3
print(x)
# %= operator
x %= 3
print(x)
# define list
x = [“apple” , “banana” ]
y = [“apple” , “banana” ]
z=x
# ‘is’ test
print( x is z)
# ‘is’ test
print( x is y)
# equality test
print( x == y)
print( x is not y)
# define list
x = [“apple” , “banana” ]
# ‘in’ test
print(“banana” in x)
# ‘in’ test
print(“pineapple” in x)
print(“pineapple” not in x)