py notes

You might also like

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

Python

●Variables are the temporary stories faces in which data can be stored.

●Available has a name along with the address which will be in hexadecimal form.

●Function is a block of code which performs a specific.

●Whenever a function is required in action it can be invoked by function call.

●Every object around us has two characteristics; one is property and another one is behaviour.

●In object oriented programming "class" is a template or blueprint for real world entities.

●When we combine the 2 characteristics of an object under a template or blueprint it becomes a


class.

●Now to specifically define an object, an object is one which has a specific template (or) nothing but
specific instances of a class.

●A problem is solved in step by step approach known as algorithm.

●We have a special data type called "complex". It is generally used to deal with complex numbers. We
know that a complex number consists of two parts one is real part and second one is imaginary part. The
left side part is called real part and the coefficient of 'i' is called imaginary part which will be situated at
right. E.g: 5+2i

●Syntax: Var_name=complex(a,b) where a,b are two integers.

Operators in Python

1. Arithmetic operator:

a.It works on int, float, complex data types.

b.Arithmetic operators are: Addition (+), subtraction (-), multiplication (*), division (/), floor division
(//), modulo (%), exponentiation (**).

c.In python whenever division operator is used the output will be in float data type. Tho the inputs for
the division operator are integer the output will be in float. So to get only integer type of output we use
floor division operator. E.g: 3//5

d.Exponential operator is the power operator, it's a syntax is : a ** b . E.g: 10 ** 3 # expected


output is 1000.

2.Relational operators:
■Relational operators in Python are used to compare two values and determine the relationship
between them. These operators evaluate to a Boolean value (`True` or `False`) based on whether the
comparison is true or false.

■Here are the relational operators in Python:

■ Equal to (`==`): Checks if two values are equal. It returns `True` if they are equal, and `False`
otherwise.

■Not equal to (`!=`): Checks if two values are not equal. It returns `True` if they are not equal, and
`False` if they are equal.

■Greater than (`>`): Checks if the left operand is greater than the right operand. It returns `True` if it is,
and `False` otherwise.

■Less than (`<`): Checks if the left operand is less than the right operand. It returns `True` if it is, and
`False` otherwise.

■Greater than or equal to (`>=`): Checks if the left operand is greater than or equal to the right
operand. It returns `True` if it is, and `False` otherwise.

■Less than or equal to (`<=`): Checks if the left operand is less than or equal to the right operand. It
returns `True` if it is, and `False` otherwise.

■Relational operators can be used with various data types in Python, including numbers (integers,
floats), strings, and other objects that support comparison operations. They are commonly used in
conditional statements and loop constructs to make decisions based on the comparison results.

3. Logical Operators:

Logical operators in Python are used to perform logical operations on Boolean values or expressions.
These operators allow you to combine multiple conditions and determine the overall truth value of the
combined expression. Python provides three logical operators: `and`, `or`, and `not`.

Here's an explanation of each logical operator:


1. Logical AND (`and`): The `and` operator returns `True` if both operands or expressions on the left and
right are `True`. Otherwise, it returns `False`. It short-circuits the evaluation, meaning that if the left
operand is `False`, the right operand is not evaluated.

2. Logical OR (`or`): The `or` operator returns `True` if at least one of the operands or expressions on the
left and right is `True`. If both operands are `False`, it returns `False`. Similar to `and`, it also short-circuits
the evaluation.

3. Logical NOT (`not`): The `not` operator is a unary operator that returns the opposite Boolean value of
the operand or expression. If the operand is `True`, `not` returns `False`, and if the operand is `False`,
`not` returns `True`.

Here are some examples to illustrate the usage of logical operators:

```python

x=5

y = 10

z=3

print(x < y and y < z) # Output: False, both conditions are not True

print(x < y or y < z) # Output: True, at least one condition is True

print(not(x < y)) # Output: False, x < y is True, not False is False

```

In the first example, the `and` operator is used to check if both `x < y` and `y < z` are `True`. Since `x` is
not less than `y`, the entire expression evaluates to `False`.

In the second example, the `or` operator is used to check if at least one of the conditions `x < y` or `y < z`
is `True`. Since `x` is less than `y`, the expression evaluates to `True`.
In the third example, the `not` operator is used to negate the result of the condition `x < y`. Since `x < y`
is `True`, `not(x < y)` evaluates to `False`.

Logical operators are commonly used in conditional statements, loops, and Boolean expressions to
control program flow based on the truth values of conditions.

4.Bitwise Operators:

Bitwise operators in Python are used to perform operations on individual bits of integer values. These
operators treat the operands as binary numbers and operate on their corresponding bits. Bitwise
operations can be useful in low-level programming, binary manipulation, and certain optimization
techniques.

Python provides the following bitwise operators:

1. Bitwise AND (`&`): Performs a bitwise AND operation on the corresponding bits of two operands. Each
bit in the result is set to 1 only if the corresponding bits in both operands are 1; otherwise, it is set to 0.

2. Bitwise OR (`|`): Performs a bitwise OR operation on the corresponding bits of two operands. Each bit
in the result is set to 1 if at least one of the corresponding bits in either operand is 1.

3. Bitwise XOR (`^`): Performs a bitwise exclusive OR (XOR) operation on the corresponding bits of two
operands. Each bit in the result is set to 1 if the corresponding bits in the operands differ; otherwise, it is
set to 0.

4. Bitwise NOT (`~`): Performs a bitwise complement operation on a single operand. It flips all the bits,
converting 0s to 1s and 1s to 0s.

5. Left Shift (`<<`): Shifts the bits of the left operand to the left by the number of positions specified by
the right operand. The vacant bits on the right are filled with 0s.

6. Right Shift (`>>`): Shifts the bits of the left operand to the right by the number of positions specified by
the right operand. The vacant bits on the left are filled with the sign bit (for signed integers) or with 0s
(for unsigned integers).
Bitwise operations are commonly used when working with binary flags, setting or clearing specific bits,
and optimizing certain algorithms that deal with binary representations.

Here's an example to illustrate the usage of bitwise operators:

```python

a = 12 # binary: 1100

b = 7 # binary: 0111

bitwise_and = a & b # binary: 0100 (decimal: 4)

bitwise_or = a | b # binary: 1111 (decimal: 15)

bitwise_xor = a ^ b # binary: 1011 (decimal: 11)

bitwise_not_a = ~a # binary: -1101 (decimal: -13)

left_shift = a << 2 # binary: 110000 (decimal: 48)

right_shift = a >> 2 # binary: 0011 (decimal: 3)

print(bitwise_and)

print(bitwise_or)

print(bitwise_xor)

print(bitwise_not_a)

print(left_shift)

print(right_shift)

```

In the example above, we perform various bitwise operations on the values of `a` and `b`. The results
demonstrate the effects of each bitwise operator.

Note that Python integers are represented using more than the standard 32 bits, so the actual bit
patterns can be much longer. However, for simplicity, we used shorter representations in the example.
It's Important to consider the binary representation and the impact of signed versus unsigned integers
when working with bitwise operations, as it can affect the results.

You might also like