2 Python Basics Part 1
2 Python Basics Part 1
•
A memory location, or simply a container
where a specific data is stored.
For example:
a = 1 is an assignment statement.
if statement, for statement, while statement
if True:
print('Hello')
a = 5
if True: print('Hello'); a = 5
#This is a comment
#print out Hello
print('Hello')
A Variable is a memory location, or simply a
container where a specific data is stored.
•
number = 10
Identifiers
For example:
1stnum is invalid, but num1 is a valid one.
print(website)
a, b, c = 5, 3.2, "Hello"
print (a)
print (b)
print (c)
x = y = z = "same"
print (x)
print (y)
print (z)
What is Data Type?
•
A way to store multiple values in a single variable.
•
•
•
t = (5,'program’, 1.02)
print (t)
•
s = "This is a string"
print(s)
s = '''A multiline
string'''
print(s)
name = "Juan"
city = "Baybay City"
province = "Leyte“
penny = 100
change = 45.5
cash = 10000000
• An ordered collection key-value pairs.
• here each key is associated with a value
A = float(Num)
print(A)
What is an Error?
An error is a mistake
or an action that is incorrect.
Syntax Error Semantic Error Run-time Error
● Incorrect
Not following ● Issues found
●
operation.
the rules. during
● Inaccurate
execution.
results.
Syntax Error
● Example:
● # Missing colon (:) at the end of the if statement
if 5 > 3
● print("5 is greater than 3")
Syntax Error
● Example:
● Num = 8
● print("hi")
● A = float(Num)
● print(A.)
● print("hello")
Semantic Error
● Example:
● # Program to calculate the area of a rectangle
● Area = length + width # This should be length * width
Run-time Error
● Example:
● # Trying to divide by zero, which will cause a run-time
error
● a = 10
● b = 0
● print(a / b)
Run-time Error
● Example:
● Num = 8
● print("hi")
● A = float(Num)
● print(B)
● print("hello")
Run-time Error
● Example:
● # Accessing an element at an invalid index
● my_list = [1, 2, 3]