Python Variables Data types operators

Python Variables
A variable is a named location used to store data in the memory. It is helpful to think of variables as a container that holds data which can

be changed later throughout programming
keen=5;
keen=5.5
training='keeninfotech'
a, b, c = 5, 3.2, "Hello"
x = y = z = "same"


Data types in Python
Every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables

are instance (object) of these classes.


Type Conversion:
The process of converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion. Python has

two types of type conversion.

Implicit Type Conversion
Explicit Type Conversion

Python Input, Output and Import
print("Welcome python keen infotech");
name=input("Enter the name");
print(name);

What are operators in python

Arithmetic operators


+ Add two operands or unary plus x + y
+2
- Subtract right operand from the left or unary minus x - y
-2
* Multiply two operands x * y
/ Divide left operand by the right one (always results into float) x / y
% Modulus - remainder of the division of left operand by the right x % y (remainder of x/y)
// Floor division - division that results into whole number adjusted to the left in the number line x // y
** Exponent - left operand raised to the power of right x**y (x to the power y)

Comparison operators
Operator Meaning                                                                          Example
> Greater that - True if left operand is greater than the right x > y
< Less that - True if left operand is less than the right                    x < y
== Equal to - True if both operands are equal                                    x == y
!= Not equal to - True if operands are not equal                    x != y
>= Greater than or equal to - True if left operand is greater than or equal to the right x >= y
<= Less than or equal to - True if left operand is less than or equal to the right x <= y

Logical operators
Logical operators are the and, or, not operators.

Logical operators in Python
Operator Meaning Example
and True if both the operands are true x and y
or True if either of the operands is true x or y
not True if operand is false (complements the operand) not x


Bitwise operators
Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name.

For example, 2 is 10 in binary and 7 is 111.

Bitwise operators in Python
Operator Meaning Example
& Bitwise AND x& y = 0 (0000 0000)
| Bitwise OR x | y = 14 (0000 1110)
~ Bitwise NOT ~x = -11 (1111 0101)
^ Bitwise XOR x ^ y = 14 (0000 1110)
>> Bitwise right shift x>> 2 = 2 (0000 0010)
<< Bitwise left shift x<< 2 = 40 (0010 1000)

Assignment operators
Assignment operators are used in Python to assign values to variables.

Operator Example Equivatent to
= x = 5 x = 5
+= x += 5 x = x + 5
-= x -= 5 x = x - 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
%= x %= 5 x = x % 5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
&= x &= 5 x = x & 5
|= x |= 5 x = x | 5
^= x ^= 5 x = x ^ 5
>>= x >>= 5 x = x >> 5
<<= x <<= 5 x = x << 5

Special operators

Identity operators
is and is not are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the

memory. Two variables that are equal does not imply that they are identical.

Identity operators in Python
Operator Meaning Example
is True if the operands are identical (refer to the same object) x is True
is not True if the operands are not identical (do not refer to the same object) x is not True

Membership operators
in and not in are the membership operators in Python. They are used to test whether a value or variable is found in a sequence (string, list,

tuple, set and dictionary).


Operator  Meaning                                                                  Example
in True if value/variable is found in the sequence 5 in x
not in True if value/variable is not found in the sequence 5 not in x

What is Name in Python

If you have ever read 'The Zen of Python' (type "import this" in Python interpreter), the last line states, Namespaces are one honking great

idea -- let's do more of those! So what are these mysterious namespaces? Let us first look at what name is


What is a Namespace in Python
So now that we understand what names are, we can move on to the concept of namespaces.
In Python, you can imagine a namespace as a mapping of every name, you have defined, to corresponding objects

Comments

Popular posts from this blog

What Is Python

Numbers

Date and Time