Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Operators in C

Python Arithmetic Operators


Arithmetic operators are used with numeric values to perform common mathematical operations to the
left:

Operator Name Example


+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Python Assignment Operators
Assignment operators are used to assigning values to variables:

Operator Example Same As


= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3

Python Comparison Operators


Comparison operators are used to comparing two values:

Operator Name Example


== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal x >= y
to
<= Less than or equal to x <= y
Python Logical Operators
Logical operators are used to combining conditional statements:
Operator Description Example
and Returns True if both statements x < 5 and x < 10
are true
or Returns True if one of the x < 5 or x < 4
statements is true
not Reverse the result, returns False not(x < 5 and x < 10)
if the result is true

Python Bitwise Operators


Bitwise operators are used to compare (binary) numbers:

Operator Name Description


& AND Sets each bit to 1 if both bits are
1
| OR Sets each bit to 1 if one of two
bits is 1
^ XOR Sets each bit to 1 if only one of
two bits is 1
~ NOT Inverts all the bits
<< Zero fill left shift Shift left by pushing zeros in
from the right and let the
leftmost bits fall off
>> Signed right shift Shift right by pushing copies of
the leftmost bit in from the left,
and let the rightmost bits fall off

Data Types
Python has the following data types built-in by default, in these categories:

Text Type: str


Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType

Getting the Data Type


You can get the data type of any object by using the type() function.
Setting the Data Type
In Python, the data type is set when you assign a value to a variable.

Example Data Type


x = "Hello World" str
x = 20 int
x = 20.5 float
x = 1j complex
x = ["apple", "banana", "cherry"] list
x = ("apple", "banana", "cherry") tuple
x = range(6) range
x = {"name" : "John", "age" : 36} dict
x = {"apple", "banana", "cherry"} set
x = frozenset({"apple", "banana", "cherry"}) frozenset
x = True bool
x = b"Hello" bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
x = None NoneType
Setting the Specific Data Type
If you want to specify the data type, you can use the following constructor functions:

Example Data Type


x = str("Hello World") str
x = int(20) int
x = float(20.5) float
x = complex(1j) complex
x = list(("apple", "banana", "cherry")) list
x = tuple(("apple", "banana", "cherry")) tuple
x = range(6) range
x = dict(name="John", age=36) dict
x = set(("apple", "banana", "cherry")) set
x = frozenset(("apple", "banana", "cherry")) frozenset
x = bool(5) bool
x = bytes(5) bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview

Control Structures
Python has three types of control structures:

• Sequential - default mode


• Selection - used for decisions and branching
• Repetition - used for looping, i.e., repeating a piece of code multiple times.
Sequential
Sequential statements are a set of statements whose execution process happens in a sequence. The
problem with sequential statements is that if the logic has broken in any one of the lines, then the
complete source code execution will break.

Selection/Decision control statements


In Python, the selection statements are also known as Decision control statements or branching
statements.

The selection statement allows a program to test several conditions and execute instructions based on
which condition is true.

Some Decision Control Statements are:

1. Simple if
2. if-else
3. nested if
4. if-elif-else

Simple if: If statements are control flow statements that help us to run a particular code, but only when
a certain condition is met or satisfied. A simple if only has one condition to check.

if-else: The if-else statement evaluates the condition and will execute the body of if if the test condition
is True, but if the condition is False, then the body of else is executed.
nested if: Nested if statements are an if statement inside another if statement.

if-elif-else: The if-elif-else statement is used to conditionally execute a statement or a block of


statements.
Repetition
A repetition statement is used to repeat a group(block) of programming instructions.

In Python, we generally have two loops/repetitive statements:

• for loop
• while loop

for loop: A for loop is used to iterate over a sequence that is either a list, tuple, dictionary, or a set. We
can execute a set of statements once for each item in a list, tuple, or dictionary.

while loop: In Python, while loops are used to execute a block of statements repeatedly until a given
condition is satisfied. Then, the expression is checked again and, if it is still true, the body is executed
again. This continues until the expression becomes false.

You might also like