Python Variables Datatypes Input
Python Variables Datatypes Input
P Y T H O N
PYTHON 3
VARIABLES, DATA TYPES, & INPUT
OBJECTIVES
Declare Variables
Understanding Variable Datatypes and Identifiers
Printing Variables
User Input to Variables
Casting Variables
Arithmetic Operations
Make a ONE Function Calculator
VARIABLES
Variables are used to store up data for later use.
firstName = “Juan”
lastName = “Dela Cruz”
ADVANCED DATA TYPES
list frozenset
tuple bytes
dict bytearray
set memoryview
print()
Used to display something in the console.
Syntax:
print(variable)
input()
Used to make the user input something in the
console.
Syntax:
variable = input()
variable = input(”Enter Something: ”)
Casting Variables
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)
Arithmetic Operators
Used to perform mathematical operations inside
our programming language.
Arithmetic Operators
Make a One Function Calculator
Try to create a program that will perform one of the
arithmetic operators when the user inputs 2 numbers.
Sample Output:
First Number: 25
Second Number: 20
25-20 = 5
Lists
A Read and Write Collection of variables that may
be used to sort certain data.
Syntax:
identifier= [value, value1, value2]
courses= [”BSIT”, “BSCS”, “BSCPE”]
ADVANCED DATA TYPES
list frozenset
tuple bytes
dict bytearray
set memoryview
Lists
A Read and Write Collection of variables that may
be used to sort certain data.
Syntax:
identifier= [value, value1, value2]
courses= [”BSIT”, “BSCS”, “BSCPE”]
Reading WHOLE Lists
You can read a list by printing the whole.
Syntax:
print(list)
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”, “BSCS”, “BSCPE”]
Reading Lists RANGE
You can read a list’s range of items by specifying a
range of indexes.
Syntax:
print(list[startIndex : endIndex])
print(list[ : endIndex])
print(list[startIndex : ])
Note: endIndex Item is excluded.
Assigning List ITEMS
You can assign a list item by using an INDEX and
an Assignment Operator “=”
Syntax:
list[index] = value
list[0] = “Rose”
List LENGTH
You can check the number of items in a list by using
the len() function.
Syntax:
len(list)
List COUNT
You can count how many times an item occurs in a
list by using the count() function.
Syntax:
list.count(value)
List ADD ITEMS by APPEND()
append() adds an item at the END OF THE LIST.
Syntax:
list.append(value)
list.append(”Rose”)
List ADD ITEMS by INSERT()
insert() adds an item at the SPECIFIED INDEX.
Syntax:
list.insert(index, value)
list.insert(0, ”Rose”)
List DELETING ITEMS by REMOVE()
remove() deletes an item based on their value.
Syntax:
list.remove(value)
list.remove(”BSIT”)
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 DEL()
del deletes an item based on its index but if the
index is not specified it deletes the whole list.
Syntax:
del list[index]
del list
Clearing a List
clear() deletes all the values in a list.
Syntax:
list.clear()
Copying a List
copy() copies the whole list which can be assigned
to a new list.
Syntax:
listOne = [’’BSIT”, “BSCS”, “BSCPE”]
listTwo = listOne.copy()
COMBINING Lists BY ADDING
You can use ‘+’ operator to combine lists.
Syntax:
listOne = [’’BSIT”, “BSCS”, “BSCPE”]
listTwo = [’’Rose”, “Twinkle”, “Daisy”]
listThree = listOne + listTwo
COMBINING Lists BY EXTEND()
extend() combines lists by appending the specified
list to the end of the first list.
Syntax:
listOne = [’’BSIT”, “BSCS”, “BSCPE”]
listTwo = [’’Rose”, “Twinkle”, “Daisy”]
listOne.extend(listTwo)
REVERSE Lists Items
reverse() Reverses the order of the List’s Items.
Syntax:
list.reverse()
SORT Lists Items
sort() Sort’s List’s Items by Alphabet or Value
depending on the datatype.
Syntax:
list.sort() #Ascending Order
list.sort(reverse=True) #Descending Order
NESTED Lists
A List inside a List also known as sublist.
Syntax:
courses = [”BSIT”, “BSCS”, “BSCPE”, [”Rose”, “Daisy”]]
Tuples
A Read-ONLY Collection of variables that may be
used to sort certain data.
Syntax:
identifier = (value, value1, value2)
courses = (”BSIT”, “BSCS”, “BSCPE”)
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
Casting TUPLES and LISTS
Convert List to Tuple
tuple(list)
Convert Tuple to List
list(tuple)
Casting TUPLES and LISTS
Convert List to Tuple
tuple(list)
Convert Tuple to List
list(tuple)
ASSIGNMENT
Create a Quiz Game using Python
Display a Welcome Message
Ask the User if he/she wants to play
If the answer is ‘No’, quit the program
If the answer is ‘Yes’, proceed to the next
Display now the questions for your Quiz Game
Tell the User if he/she got the answer to the question
correct or wrong
Create 10 questions that are related only to IT
Display the number of correct answers of the User then
quit the program