0% found this document useful (0 votes)
6 views59 pages

Lesson 1

The document provides an overview of variables, data types, and input methods in programming, including basic data types like strings, integers, and floats. It explains the use of identifiers, syntax for printing and inputting data, and the concept of casting between data types. Additionally, it covers arithmetic operators, lists, tuples, and various list operations such as appending, inserting, and deleting items.

Uploaded by

Luke .Period
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views59 pages

Lesson 1

The document provides an overview of variables, data types, and input methods in programming, including basic data types like strings, integers, and floats. It explains the use of identifiers, syntax for printing and inputting data, and the concept of casting between data types. Additionally, it covers arithmetic operators, lists, tuples, and various list operations such as appending, inserting, and deleting items.

Uploaded by

Luke .Period
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

Variables, Data type &

Input
Variables
Variables are used to store up data for later use.

Basic Data Types:


o String (sentences, phrases, characters): Identifier = “Hello
World”
o int (positive & negative numbers): identifier = 5
o float (decimal numbers) : identifier = 25.3237
o bool (true or false) : identifier = true

Syntax the concept of giving specific word sets in specific


orders to computers so that they do what we want them to
do.
Identifier
What is an identifier?
Identifier is the name of the variable which the user decides
to choose.
Syntax:
(identifier = value)
firstName = “SDPT”
lastName = “Solutions”
number = 10
money = 25.50
isTall = True
print( )
print( ) - used to display something in the console.

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)

firstName = input(“Enter Your First Name :”)


print (“Hi, “ + firstName)
(run enter a name)
Input ( )
number = input(“ Enter Name:”)
print (number)
Casting
A technique used to convert a datatype to another
datatype.

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)

Note: # means comment it can not affect our program code.


Calculator
Make one function calculator
Try to create a program that will perform one of the
arithmetic operators when the user inputs two number.
Sample output:
First Number: 25
Second number: 20
25 – 20 = 5
Calculator
Example:
firstNumber = input(“Enter a number: “)
secondNumber = input(“Enter 2nd number:”)
print(firstNumber + secondNumber)
Compare:
firstNumber = int( input(“Enter a number: “))
secondNumber = int(input(“Enter 2nd number:”))
print(firstNumber + secondNumber)
Calculator
Example:
firstNumber = float(input(“Enter a number: “))
secondNumber = float(input(“Enter 2nd number:”))

result = firstNumber * secondNumber


print(firstNumber + “ * “ +secondNumber + “ * “ +
result)

run
Calculator
Example:
firstNumber = float(input(“Enter a number: “))
secondNumber = float(input(“Enter 2nd number:”))

result = firstNumber * secondNumber


print(str(firstNumber )+ “ * “ +str(secondNumber) + “ * “
+
str(result) )

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”]

Reading Whole Lists


You can read a list by printing the whole list.
Syntax:
print(list)
Example:
courses = [“BSIT” , “BSCE” , “ BSIE”]
print(courses)
Reading Lists ITEMS
you can read a list by printing one of the items inside it by
using an INDEX.

Syntax:
print(list[index])

Index the number of where an item is on a collection.


+ INDEX: 0 1 2
- INDEX: -3 -2 -1
courses = [“BSIT” , “BSCE” , “BSIE”]
Reading Lists ITEMS
EXAMPLE:
# +INDEX - 0 1 2
courses = [“BSIT” , “BSCE” , “BSIE”]
print(courses[1])

# - 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:])

Note: endindex item is excluded


Reading Lists RANGE
Example:
# 0 1 2 3 4
courses =[“BSIT” , “BSCE” , “BSIE” , “BSME” , “ BSCpE”]
print(courses[2:])
if:
print(courses[:3])
if:
print(courses[1:4])
Assigning List ITEMS
You can assign a list item by using an INDEX and an
ASSIGNMENT Operator
“=“. OR TO CHANGE AN ITEM TO THE LIST OF COURSES.

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”))

Note: this is case sensitive.


List ADD ITEMS by APPEND( )
append( ) adds an item at the END OF THE LIST.

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)

Note: case sensitive.


List DELETING ITEMS by POP( )
pop( ) deletes an item based on their index but if index is not
specified it deletes the last item.

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])

Because @ INDEX 3 [“BSCpE”, “BSIE”] @index 3 there is


another index which is 0 and 1 [“BSCpE”, “BSIE”].

courses = [“BSIT”, “BSCS”, “BSCE”, [“BSCpE”, “BSIE”]]


print(courses[3][0])
Tuples
A Read-ONLY Collection of variables that may be used to sort
certain data.

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)

Convert Tuple to List


list(tuple)
Casting TUPLES and LISTS
Example:
courses = (‘’BSIT’’, ‘’ BSIE’’, ‘’BSCE’’)# this is tuple
convert to list
courses = list (courses)
print (courses)
Output: parenthesis to square bracket.

courses = [‘’BSIT’’, ‘’ BSIE’’, ‘’BSCE’’] # list convert to tuple


courses = tuple(courses)
print (courses)
Output: parenthesis

You might also like