L2 Python Variables and Expression
L2 Python Variables and Expression
Programming
Variables
Expression
Function
Objects and Python Interpreter
The Python interpreter is a program that runs on a
● There are several techniques you can use to make Pascal Case
them more readable:
Each word starts with a
1. Camel Case
capital letter:
2. Pascal Case
3. Snake Case Snake Case
Each word is separated by
an underscore character
Objects and variables
Manipulating Variables
● Names are not permanently assigned
to objects.
● A name can be reassigned to another
object, and multiple names can refer
to the same object.
● The following animation illustrate:
● NOTE:
= means compute the value on the right, and then assign
that value into the variable on the left."
Each Python object has three defining
properties:
More on
1. Value: A value such as "20", "abcdef", or 55.
objects
2. Type: The type of the object, such as integer
or string.
Constant Example:
# Constant Expressions
Expressions x = 15 + 1.3
print(x)
Python Data Types
Data Types
Importance of
Python Data Types
• Variable Declaration
and Initialization
• Data Validation
• Memory Management
Python Functions
Strings
● A string is a sequence of characters.
Python Strings ● A character is simply a symbol.
● For example, the English language has 26
characters.
● Strings can be created by enclosing characters
inside a single quote or double-quotes
EXAMPLES:
Python
Comments
print() function
● prints the specified message to the screen, or
float() function
- used to convert a value to a floating-point
number (a number with a decimal point).
Integer or float?
-10 15 // 2 100
1000
[1.5, 2.7, 4.3, 0.9]
1k
2.5e3
Assigning a Variable
Assigning a variable
Simple Interest =
Principal Amount × Rate × Time /100
End of Slide
IF ELSE Laboratory Item #1
Creating a simple login program in Python involves taking a username and password
from the user and comparing them to predefined values
# Check if the provided username and password match the predefined values
if username == correct_username and password == correct_password:
print("Login successful!") • We define a correct username and password. In a real application, these values
else: would be stored more securely, such as in a database.
print("Login failed. Please check your username and password.")
• We use the input() function to get the username and password from the user.
• We compare the entered username and password to the predefined correct values.
• If the entered values match the correct values, we print "Login successful." Otherwise,
we print "Login failed."
• Please note that this is a very basic example, and in real-world applications, you
should consider more secure and sophisticated methods for handling user
authentication and password storage, such as hashing and salting passwords, using
databases, and implementing session management. Additionally, it's important to
handle exceptions and implement security measures to protect against potential
attacks.
for in Laboratory Item #1
• After creating the tree layers, we use a second loop to create the tree trunk. The trunk
is represented with vertical bars (|). You can adjust the trunk_height and trunk_width
to control the trunk's size.
• When you run this code, it will print a simple text-based Christmas tree to the console.
Feel free to modify and enhance it to create more complex and colorful trees with
ornaments or lights if you like.
Def function Laboratory Item #1
# Define the grading scale • # Get the score from the user
grading_scale = { • score = float(input("Enter the score: "))
'A': (90, 100),
'B': (80, 89), • # Define the grading scale
'C': (70, 79), • grading_scale = {
'D': (60, 69), • 'A': (90, 100),
'F': (0, 59) • 'B': (80, 89),
} • 'C': (70, 79),
• 'D': (60, 69),
# Determine the grade • 'F': (0, 59)
def compute_grade(score, grading_scale): • }
for grade, (min_score, max_score) in grading_scale.items():
if min_score <= score <= max_score: • # Determine the grade
return grade • def compute_grade(score, grading_scale):
return "Invalid Score" • for grade, (min_score, max_score) in grading_scale.items():
• if min_score <= score <= max_score:
# Calculate and print the grade • return grade
grade = compute_grade(score, grading_scale) • return "Invalid Score"
print(f"Grade: {grade}")
• # Calculate and print the grade
• grade = compute_grade(score, grading_scale)
• print(f"Grade: {grade}")