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

Keywords, Operators in Python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Keywords, Operators in Python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Class

A class is a blueprint or template for creating objects. It defines a set of attributes (variables)
and methods (functions) that the objects created from the class will have.

• Attributes: Variables that belong to the class and define the properties of objects.
• Methods: Functions defined inside a class that describe the behaviors or actions of
the objects.

For example, think of a class like a blueprint for a car: it defines what a car is (color, model,
etc.) and what a car can do (drive, stop, etc.).

Object:

An object is an instance of a class. When you create an object, you're essentially building a
specific car from the blueprint (class). Each object can have its own specific values for the
attributes defined by the class.

Key Points:

• A class defines the structure and behavior.


• An object is an instance of a class with specific data.

Keywords:
Python has a set of keywords that are reserved words that cannot be used as variable names,
function names, or any other identifiers:

Here is a list of Python keywords, which are reserved words and cannot be used as identifiers,
variable names, or function names:

Here is a table of Python keywords:

Keyword Description
False Boolean value representing falsehood.
None Represents a null value or absence of a value.
True Boolean value representing truth.
and Logical operator; returns True if both operands are true.
as Used to create an alias, often in import or with statements.
assert Tests if a condition is true; raises an error if false.
async Declares an asynchronous function.
await Pauses execution in an async function until completion.
break Exits a loop prematurely.
Keyword Description
class Defines a class.
continue Skips the current loop iteration and moves to the next.
def Defines a function.
del Deletes objects, variables, or elements from a collection.
elif Else-if condition in conditional statements.
else Default condition in conditional statements.
except Catches and handles exceptions in a try block.
finally Executes code after try, regardless of exceptions.
for Starts a loop over a sequence.
from Imports specific parts of a module.
global Declares a variable as global.
if Starts a conditional statement.
import Imports a module or objects from a module.
in Tests membership in a sequence or iterable.
is Tests object identity.
lambda Creates a small anonymous function.
nonlocal Refers to a variable in an outer but non-global scope.
not Logical negation operator.
or Logical operator; returns True if either operand is true.
pass A placeholder that does nothing.
raise Raises an exception.
return Exits a function and optionally returns a value.
try Starts a block for exception handling.
while Starts a loop that continues as long as the condition is true.
with Wraps code in a context manager.
yield Pauses a function and returns a value; used in generators.

Python Operators
In Python, operators are special symbols used to perform operations on variables and values.
Here's a breakdown of the different types of operators:

1. Arithmetic Operators

These are used to perform basic mathematical operations.

Operator Name Description


+ Addition Adds two values: a + b
- Subtraction Subtracts one value from another: a - b
* Multiplication Multiplies two values: a * b
/ Division Divides one value by another: a / b (returns float)
// Floor Division Divides and returns the integer part: a // b
% Modulus Returns the remainder: a % b
** Exponentiation Raises a number to the power: a ** b

2. Comparison (Relational) Operators

Used to compare two values.

Operator Name Description


== Equal to Returns True if values are equal: a == b
!= Not equal to Returns True if values are not equal: a != b
> Greater than Returns True if the left value is greater: a > b
< Less than Returns True if the left value is smaller: a < b
>= Greater than or equal to Returns True if the left value is greater or equal: a >= b
<= Less than or equal to Returns True if the left value is less or equal: a <= b

3. Logical Operators

Used to combine conditional statements.

Operator Name Description


and Logical AND Returns True if both conditions are true: a and b
or Logical OR Returns True if one of the conditions is true: a or b
not Logical NOT Inverts the truth value: not a (returns True if a is False)

4. Assignment Operators

Used to assign values to variables.

Operator Name Description


= Assignment Assigns a value: a = 5
+= Add and assign Adds and assigns: a += 5 (equivalent to a = a + 5)
Operator Name Description
-= Subtract and assign Subtracts and assigns: a -= 5
*= Multiply and assign Multiplies and assigns: a *= 5
/= Divide and assign Divides and assigns: a /= 5
//= Floor divide and assign a //= 5
%= Modulus and assign a %= 5
**= Exponent and assign a **= 2
&= Bitwise AND and assign a &= b
` =` Bitwise OR and assign
^= Bitwise XOR and assign a ^= b
>>= Bitwise right shift and assign a >>= b
<<= Bitwise left shift and assign a <<= b

5. Bitwise Operators

These operators work on bits and perform bit-level operations.

Operator Name Description


& Bitwise AND Sets each bit to 1 if both bits are 1: a & b
` ` Bitwise OR
^ Bitwise XOR Sets each bit to 1 if only one of two bits is 1: a ^ b
~ Bitwise NOT Inverts all bits: ~a
<< Left Shift Shifts bits to the left: a << 2
>> Right Shift Shifts bits to the right: a >> 2

6. Identity Operators

Used to compare the memory locations of two objects.

Operator Name Description


is Identity Returns True if both variables point to the same object: a is b
is not
Not Returns True if variables do not point to the same object: a is not
Identity b

7. Membership Operators

Used to test if a value is found within a sequence.

Operator Name Description


in Membership Returns True if a value is found in the sequence: a in b
not in Not Membership Returns True if a value is not in the sequence: a not in b
These operators are the building blocks of Python expressions and allow you to perform a
wide variety of tasks such as arithmetic, logic, and comparisons.

You might also like