Uploaded by zacuhry

ICT01 GROUP1NOTE.pdf

advertisement
๐Ÿ’ป
Group 1 - Notes (CO4.1-4.2)
Mul ti-select
ICT01
Course Outcome 4.1
Define Comparison
This is used for questioning if two values are equal. To use this, you
use the == (equal equal) operator.
What are the different types of Operators?
Equality: the equal to operator (==)
This compares the values of two operands. If they are equal,
then the result of the comparison is True. If, they are not equal,
then the result is False.
Group 1 ๎‚ˆ Notes ๎‚CO4.1๎‚ˆ4.2๎‚‚
1
Inequality: the not equal to operator ๎‚! ๎‚›๎‚‚
This also compares the values of two operands, but the
difference is that if the two values are equal, the result would be
False. But if they are not equal, the result would be True.
Comparison Operators: greater than
You can also ask a comparison question using the > (greater
than) operator.
True confirms it; False denies it.
Comparison Operators: greater than or equal to
The greater than operator has another special, non-strict variant,
but it's
denoted differently than in classical arithmetic notation: >=
(greater than or
equal to).
Comparison Operators: less than/less than or equal to
As you've probably already guessed, the operators used in this
case are:
the < (less than) operator and its non-strict sibling: <= (less than
or equal to).
Conditions and conditional execution
if statement
This is used for executing a block of code which will only run on
a specified condition. If the condition evaluates to true, then the
code inside the if block runs; if it evaluates to false, the block is
skipped.
if-else statement
This is used for executing a block of code which runs if a certain
condition is true, and another block of code if the condition is
false.
if condition_true:
run_condition
Group 1 ๎‚ˆ Notes ๎‚CO4.1๎‚ˆ4.2๎‚‚
2
else:
condition_false
elif statement
This is used to check more than just one condition, and to stop
when the first statement which is true is found.
Python Literals
It is a raw value directly assigned to a variable or used in code. It
represents fixed data.
x = 5
y = "Hello"
Numeric Literals
Numbers used directly in the code.
Integers: whole numbers without a decimal point
Float: numbers that have decimal points
Number Literal Formats
Binar y
Octal
Hexadecimal
String Literals
A sequence of characters enclosed in quotes, representing text in
Py thon.
String Formats:
Single Quotes
Double Quotes
Triple Quotes (utilized in multi-line texts)
Escape Characters in Strings
Used to include special characters inside strings.
The common escape characters are:
Group 1 ๎‚ˆ Notes ๎‚CO4.1๎‚ˆ4.2๎‚‚
3
\n - for a new line
\t - for a tab
\\ - for a backslash
\หฎหฎ - for double quotes inside a double-quoted string
String Concatenation and Replication
Concatenation:
Combining two or more strings using the + operator.
Replication:
Repeating a string multiple times using the * operator.
Boolean Literals
These represent the truth values.
Values: true ๎‚1๎‚‚ false ๎‚0๎‚‚
Special Literal ๎‚ˆ None
The None literal represents the absence of a value or a null value.
This is typically used as a placeholder for variables without an initial
value.
Variable
It is a container for storing data values. In Python, variables are
created when you assign a value to them.
A variable must: start with a letter or underscore, contain
letters/digits/underscore, and variable is case-sensitive.
Course Outcome 4.2
Loops
While loop
Syntax Similarity to If: The difference is in usage; ‘ifสผ runs once,
while ‘whileสผ continues as long as the condition is ‘Trueสผ.ฬ“
Key Points:
Indentation rules apply.
Group 1 ๎‚ˆ Notes ๎‚CO4.1๎‚ˆ4.2๎‚‚
4
The loop body must have conditions that can be changed to
prevent infinite loops.
Using a Counter: A counter variable can help control loop
iterations and determine when to exit.
For loop
Purpose: Useful for iterating over a sequence of elements, often
used when the number of iterations is predetermined.
Components:
‘forสผ keyword starts the loop.
‘inสผ keyword specifies the range or collection.
‘range()สผ function generates a sequence of numbers.
Example Usage: To loop a specific number of times or iterate
over a collection.
Break and Continue
Break: Exits the loop immediately.
Continue: Skips to the next iteration of the loop.
Useful for controlling loop execution and enhancing code
readability.
Logic and Bit
Logical Operator
‘andสผ Operator: Both conditions must be true for the result to
be true.
‘orสผ Operator: At least one condition must be true.
‘notสผ Operator: Inverts the truth value.
Bitwise Operator
‘&สผ๎‚’ฬ“ Bitwise AND; both bits must be 1.
‘|สผ๎‚’ฬ“ Bitwise OR; at least one bit must be 1.
‘^สผ๎‚’ฬ“ Bitwise XOR; only one bit must be 1.
‘~สผ๎‚’ฬ“ Bitwise NOT; inverts all bits.
Group 1 ๎‚ˆ Notes ๎‚CO4.1๎‚ˆ4.2๎‚‚
5
Note: Operate on integer values only.
Lists in Python
Why lists?
Used for handling collections of data without creating numerous
var iables.
Example: ‘numbers = ๎‚ƒ1, 2, 3, 4, 5๎‚„สผ creates a list with five
elements.
Indexing
Access or modify elements using indices (e.g., ‘numbers[0] ๎‚›
111สผ changes the first element to 111๎‚‚.
Group 1 ๎‚ˆ Notes ๎‚CO4.1๎‚ˆ4.2๎‚‚
6
Download