Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

2 Python Basics Part 1

Uploaded by

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

2 Python Basics Part 1

Uploaded by

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

Outline:


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

• An identifier serves as identification to entities.


• It is a name given to the variable, classes,
functions, etc.
Rules as to how you define identifiers in Python
and must be followed at all times:
1. An identifier could be a combination of
• letters, either uppercase (A to Z) or lowercase (a to z),
• a digit (0-9),
• and underscore (_) symbol in any length.

For example, num1, sum_Of_x_and_y, studentGrade are all


valid.
Rules as to how you define identifiers in Python
and must be followed at all times:
2. Python identifiers should not start with a digit.

For example:
1stnum is invalid, but num1 is a valid one.

Note: A number can be placed anywhere in an identifier


except at the beginning.
Rules as to how you define identifiers in Python
and must be followed at all times:
3. Keywords should not be used as an identifier.

4. Other symbols than underscore (_) is not allowed


when writing identifier, e.g. !, @,#,$,%,-, etc. Thus, this
declaration is invalid:
$a = 5 or sum-of-two-numbers
Rules as to how you define identifiers in Python
and must be followed at all times:
5. Identifiers should not also contain white spaces.
• Such as the “sum of two numbers” or “first num”.
• You may use camel case (capitalize each word) or
underscore to fill in white spaces.
For example:
firstNum or first_num
sumOfTwoNumbers or sum_of_two_numbers
Rules as to how you define identifiers in Python
and must be followed at all times:
1. Combinations of alphabet, numbers,
and underscores.
2. Should not start with a number.
3. Keywords should not be used.
4. Other symbols are not allowed.
5. Should not contain white space
6. Words are separated by underscore.
number = 10
number = 1.1
website = "apple.com"
print(website)

# assigning a new value to website


website = “python.com"

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 means to classify data


in order to tell the computer
how it will be used.












A way to store multiple values in a single variable.

It can do the following:


● can contain any type of values, and
● can be modified or re-ordered.
a = [1, 2.2, 'python']
print (a)
#display the data type
print (type(a))
Lists are mutable, meaning, the value of elements
of a list can be altered.

a = [1, 2.2, 'python’]


a[1] = 5
print (a)




t = (5,'program’, 1.02)
print (t)

#change value for t[1]


t = (5,'program', 1.02)
t[1] = 1
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

juan = {"age": 22}


my_dict is a dictionary with three keys ('a', 'b', and 'c’)
and their associated values (1, 2, and 3, respectively).
You can access the value associated with a particular key in
a dictionary using square brackets [], or using the get()
method.
For example:
float(12)
int(10.6)
int(-10.6)
float('2.5')
str(12)
int('1p')
Num = 8

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

● A syntax error occurs when the code doesn't follow


the correct structure or "syntax" of the Python
language.

● 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

● A semantic error happens when the code is syntactically


correct but does not do what the programmer intended,
resulting in incorrect logic.

● Example:
● # Program to calculate the area of a rectangle
● Area = length + width # This should be length * width
Run-time Error

● A run-time error occurs when the code compiles and runs,


but an error is encountered during execution, such as
dividing by zero or accessing a non-existent variable.

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

● # Trying to access an index that does not exist


● print(my_list[5])

You might also like