Modul 2 : variables,
expression and statements
Muh. Sulkifly Said, S.SI., M.Eng
Variable
● Variables are used to store and manage data in a program.
They are symbolic names that reference or hold a
Comments
● In Python, comments are used to include explanatory notes or annotations
within the code
● Comments are ignored by the Python interpreter during the execution of the
program and serve as documentation for developers, making the code more
understandable
● There are two main types of comments in Python
a. Single-Line Comments (#)
b. Multi-Line Comments (“””)
Rules for Naming Variables
● Variable names must start with a letter (a-z, A-Z) or an underscore _.
● The remaining characters can be letters, numbers (0-9), or
underscores.
● Variable names are case-sensitive (age and Age are different
variables).
Conventions for Variable Names
● Use descriptive and meaningful names.
● Use lowercase with underscores (snake_case) for most
variables.
● Use CamelCase for class names.
● Avoid using single-character names unless they represent
iterators (i, j, k).
Examples of Valid Variable Names
Examples of Invalid Variable Names
Keywords
Operator and Operands
● Arithmetic Operators
● Comparison Operators
● Logical Operators
● Assignment Operators
● Bitwise Operators
● Membership Operators
● Identity Operators
Arithmetic Operators
Comparison Operators
Logical Operators
Assignment Operators
Bitwise Operators
Membership Operators
In Operator:
Not in Operator
“In” Operators
“Not In” Operators
Identity Operators
● The identity operator (is) in Python is used to check if two objects
are the same object in memory. It returns True if the two objects are
the same object, and False otherwise.|
● The identity operator is different from the equality operator (==),
which checks if two objects have the same value. The equality
operator can return True for objects that are not the same object in
memory, as long as they have the same value.
Identity Operators
Modulus Operator
● In Python, the modulus operator is represented by the
percent sign (%)
● The modulus operation returns the remainder when the
left operand is divided by the right operand
● Common use case for “checking for Even or Odd”
Sample Modulus Operator
Order of Operations (Precedence of Operators)
1. ()
2. exponent **
3. multiplication and others * / ** % //
4. addition and subtraction + -
Sample Precedence of Operators
String Operations
● Concatenation
● Repetition
● String Length
● Indexing and Slicing
● String Methods
● String Formatting
● Checking Substring
Concatenation
● Concatenation involves combining two or more strings to
create a new string
● This operation is performed using the concatenation operator,
which is the + symbol
Example Concatenation
Repetition
● You can repeat a string by using the multiplication operator
● This operation is performed using the repetition operator,
which is the * (asterisk) symbol
Sample Repetition
String Length
In Python, you can find the length of a string (i.e., the number of
characters in the string) using the len()
Indexing & Slicing
Access individual characters using indexing, and extract substrings using
slicing
String Methods
● upper(): Converts all characters to uppercase.
● lower(): Converts all characters to lowercase.
● capitalize(): Capitalizes the first character.
● title(): Capitalizes the first character of each word.
● replace(old, new): Replaces occurrences of the old substring with the
new substring.
● find(substring): Returns the index of the first occurrence of the
substring (or -1 if not found).
● count(substring): Returns the number of occurrences of the substring.
String Formatting
● String formatting in Python refers to the various ways in which
you can create formatted strings by combining variables,
expressions, and literal text.
● There are multiple approaches to string formatting in Python
● Three common methods: concatenation, % formatting, and f-
strings.
String Formatting with Concatenation
You can concatenate strings and variables by using the +
operator. While straightforward, this method can become less
readable when dealing with multiple variables or expressions.
String Formatting with % Format
The % operator is another way to format strings, often referred to
as "old-style formatting."
%s is a placeholder for a string.
%d is a placeholder for an integer.
String Formatting with % Format
The % operator is another way to format strings, often referred to
as "old-style formatting."
%s is a placeholder for a string.
%d is a placeholder for an integer.
String Formatting with f-Strings
Introduced in Python 3.6, f-strings provide a concise and
expressive way to format strings. You can embed expressions and
variables directly within the string using curly braces ‘{}’
Asking the User for Input
In Python, you can use the input() function to get user input from
the console.
The input() function takes a prompt (a string) as an optional
argument, displays the prompt to the user, and then waits for the
user to enter some text
Sample Input()