Lesson 1
Lesson 1
Input
Variables
Variables are used to store up data for later use.
Syntax:
print(variable)
print (firstName)
print(firstName + lastName)
print(firstName + “ ” + lastName)
Input ( )
Used to make the user input something in the console.
Syntax:
variable = input( )
variable = input (“Enter Something:”)
Example:
firstName = input( )
print(firstName)
Input ( )
Example:
firstName = input( )
print(firstName)
( run then enter a name)
Syntax:
Convert Numbers to String
str(number)
Convert String to Numbers
int(string)
float(string)
Casting
Syntax:
firstName = “SDPT”
number = 5
print(firstName + number)
error
number = “5”
By casting:
firstName = “SDPT”
number = 5
print(firstName + str(number))
exercise
• . We check in a patient named John Smith
• He is 20 years old
• He is a new patient.
exercise
Example:
input
Arithmetic Operators
Used to perform mathematical operations inside our
programming language.
Operator Syntax Result
Subtraction X-Y Difference
Addition X+y Sum
Division X/y quotient
Module X%y Remainder
Floor Division X // y Quotient ( Rounded
Off)
Exponent X**y Power
Multiplication X*Y product
Arithmetic Operators
# Subtraction
print(5-2)
#Addition
print(5+2)
run
Calculator
Example:
firstNumber = float(input(“Enter a number: “))
secondNumber = float(input(“Enter 2nd number:”))
run
Activity
Advanced Data types
list
tuple
dict
set
frozenset
bytes
bytearray
memoryview
Lists
A Read and Write collection of variables that maybe used to
sort certain data.
Syntax:
identifier = [value,value1,value2]
courses = [“BSIT”, “BSCS”, “BLIS”]
Lists
Example:
courses = [“BSIT” , “BSCE” , “ BSIE”]
Syntax:
print(list[index])
# - INDEX - -3 -2 -1
courses = [“BSIT” , “BSCE” , “BSIE”]
print(courses[-2])
Reading Lists RANGE
You can read a list’s range of items by specifying a range of
index.
Syntax:
print(list[startindex:endindex])
print(list[:endindex])
print(list[startindex:])
Syntax:
list[index] = value
list[0] = “BSCE”
List LENGTH
You can check the number of items in a list by using the len( )
function.
Syntax:
len(list)
Assigning List ITEMS & List LENGTH
Example:
# 0 1 2 3 4
courses =[“BSIT” , “BSCE” , “BSIE” , “BSME” , “ BSCpE”]
courses[0] = “ COT”
print(courses)
or:
print(len(courses))
List COUNT
You can count how many times an item occurs in a list by
using the count( ) function.
Syntax:
list.count(value)
List COUNT
Example:
# 0 1 2 3 4
courses =[“BSIT” , “BSCE” , “BSIE” , “BSIT” , “ BSCE”,
“BSIE”, “BSCE”]
print(courses.count(“BSCE”))
Syntax:
list.append(value)
list.append(“NSTP”)
List ADD ITEMS by APPEND( )
Example:
# 0 1 2 3 4
courses =[“BSIT” , “BSCE” , “BSIE” , “BSME” , “ BSCpE”]
courses.append(‘’ NSTP’’)
print(courses)
List ADD ITEMS by INSERT( )
insert( ) adds an item at the SPECIFIED INDEX.
Syntax:
list.insert(index.value)
list.insert(0,“NSTP”)
List ADD ITEMS by INSERT( )
Example:
# 0 1 2 3 4
courses =[“BSIT” , “BSCE” , “BSIE”]
courses.insert(1,‘’ NSTP’’)
print(courses)
List DELETING ITEMS by REMOVE( )
remove ( ) deletes an item based on their value.
Syntax:
list.remove(value)
list.remove(“BSIT”)
List DELETING ITEMS by REMOVE( )
Example:
# 0 1 2
courses =[“BSIT” , “BSCE” , “BSIE”]
courses.remove(‘’ BSCE’’)
print(courses)
Syntax:
list.pop( )
list.pop(index)
List DELETING ITEMS by POP( )
Example:
# 0 1 2
courses =[“BSIT” , “BSCE” , “BSIE”]
courses.pop( )
print(courses)
or
courses =[“BSIT” , “BSCE” , “BSIE”]
courses.pop(1)
print(courses)
List DELETING ITEMS by DEL( )
del deletes an item based on their index but if index is not
specified it deletes the whole list.
Syntax:
del list [index]
del list
List DELETING ITEMS by DEL( )
Example:
# 0 1 2
courses =[“BSIT” , “BSCE” , “BSIE”]
del courses [0]
print(courses)
Clearing a List
clear () deletes all the value in a list.
Syntax:
list.clear( )
Clearing a List
Example:
# 0 1 2
courses =[“BSIT” , “BSCE” , “BSIE”]
courses.clear ( )
print(courses)
Copying a List
copy( ) copies the whole list which can be assigned to a new
list.
Syntax:
listOne = [“BSIT” , “BSCE” , “BSIE”]
listTwo = listOne.copy ( )
Copying a List
Example:
# 0 1 2
courses =[“BSIT” , “BSCE” , “BSIE”]
x = courses.copy ( )
print(x)
Or
# 0 1 2
courses =[“BSIT” , “BSCE” , “BSIE”]
courses.pop( )
x = courses.copy ( )
print(x)
COMBINING List BY ADDING
You can use ‘+’ operator to combine lists.
Syntax:
listOne = [“BSIT” , “BSCE” , “BSIE”]
listTwo = [“BSCpE”, “BSME”]
listThree = listOne + listTwo
COMBINING List BY ADDING
Example:
# 0 1 2
courseOne =[“BSIT” , “BSCE” , “BSIE”]
courseTwo = [“BSCpE”, “BSME”]
print(courseOne + courseTwo)
Or
courseOne =[“BSIT” , “BSCE” , “BSIE”]
courseTwo = [“BSCpE”, “BSME”]
x= courseOne+courseTwo
print(x)
COMBINING List BY EXTEND ()
extend( ) combines lists by appending the specified list to the
end of the first list.
Syntax:
listOne = [“BSIT” , “BSCE” , “BSIE”]
listTwo = [“BSCpE”, “BSME”]
listOne.extend(listTwo)
COMBINING List BY EXTEND ()
Example:
courseOne =[“BSIT” , “BSCE” , “BSIE”]
courseTwo = [“BSCpE”, “BSME”]
courseOne.append(courseTwo)
print(courseTwo)
or
print (courseOne)
REVERSE Lists Items
reverse ( ) Reverses the order of the List’s Items.
Syntax:
list.reverse( )
REVERSE Lists Items
Example:
courseOne =[“BSIT” , “BSCE” , “BSIE”]
courseOne.reverse( )
print(courseOne)
SORT Lists Items
sort ( ) sort List’s Items by Alphabet or Value depending on
the datatype.
Syntax:
list.sort( ) #Ascending Order
list.sort(reverse = True) # Descending Order
SORT Lists Items
Example
alphabet = [“Z” , “A” , “C” , “B”]
alphabet.sort( )
print(alphabet)
Or
alphabet = [“Z” , “A” , “C” , “B”]
alphabet.sort(reverse=True )
print(alphabet)
NESTED Lists
A list inside a List also known as sublist.
Syntax:
courses = [“BSIT”, “BSCS”, “BSCE”, [“BSCpE”, “BSIE”]]
NESTED Lists
Example:
# 0 1 2 3
courses = [“BSIT”, “BSCS”, “BSCE”, [“BSCpE”, “BSIE”]]
print(courses[3])
Syntax:
identifier = (value,value1,value2)
courses = (“BSIT”, “BSCS”, “BSIT”)
Tuples
1. CAN be READ
2. CAN be COMBINED
3. CAN be DELETED COMPLETELY
4. CAN’T be ASSIGNED
5. CAN’T be DELETED ONE BY ONE
Tuples
Example:
# 0 1 2
courses = (“BSIE”, “BSCE”, “BSME”)
print(courses[0])
Or
# 0 1 2
courses = (“BSIE”, “BSCE”, “BSME”)
courses [0] =‘’BSIT ’’ # try to assigned BSIE to BSIE
print(courses[0])
# tuples can be READ can not be written to.
Casting TUPLES and LISTS
Convert List to Tuple
tuple (list)