Thursday 4 July 2019

python variables and operators

Python Variables


unlike other programming languages we dont need to creae any variables in python.

just assign value to it. automatically that variable will become that particular data type variable.

ex:

a = 1 (intiger variable)

a = 1.1 (float variable)

a = "abc" ( string variable.)

this concept is called as dynamic type casting.

There  are 4 basic data types available in python.

  • int (ex: 1,2,3,....etc)
  • float (ex: 1.1,2.1,....etc
  • str ("nihal","aruna"...etc)
  • boolean (True,False)

Operators in python:

     arithmetic operators in python:

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

assignment operators:

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

explanation for bitwise operators

12 << 2
48
Actual binary value of 12 is "00 1100" when we execute the above statement Left shift ( 2 places shifted left) returns the value 48 its binary value is "11 0000".
48 >> 2
12
The binary value of 48 is "11 0000", after executing above statement Right shift ( 2 places shifted right) returns the value 12 its binary value is "00 1100".

Python Logical Operators

Logical operators are used to combine conditional statements:

Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Python Membership Operators

Membership operators are used to test if a sequence is presented in an object:

Operator Description Example
in  Returns True if a sequence with the specified value is present in the object x in y
not in Returns True if a sequence with the specified value is not present in the object x not in y






introduction to python

How to Get Started With Python?

Python is a cross-platform programming language, meaning, it runs on multiple platforms like Windows, MacOS, Linux and has even been ported to the Java and .NET virtual machines. It is free and open source.

Even though most of today’s Linux and Mac have Python preinstalled in it, the version might be out-of-date. So, it is always a good idea to install the most current version.

Install Python Separately
  1. Download the latest version of Python.
  2. Run the installer file and follow the steps to install Python
    During the install process, check Add Python to environment variables. This will add Python to environment variables and you are able to run Python from any part of the computer.
    Also, you can choose the path where Python is installed.
Once you finish the installation process, you can run Python.

1. Run Python in Immediate mode

Once Python is installed, typing python in the command line will invoke the interpreter in immediate mode. We can directly type in Python code and press enter to get the output.
Try typing in 1 + 1 and press enter. We get 2 as the output. This prompt can be used as a calculator. To exit this mode type quit() and press enter.

2. Run Python in the Integrated Development Environment (IDE)

We can use any text editing software to write a Python script file.
We just need to save it with the .py extension. But using an IDE can make our life a lot easier. IDE is a piece of software that provides useful features like code hinting, syntax highlighting and checking, file explorers etc. to the programmer for application development.
By the way, when you install Python, an IDE named IDLE is also installed. You can use it to run Python on your computer. It's a decent IDE for beginners.
When you open IDLE, an interactive Python Shell is opened.
open the file type the below contents
print("hello world")
Now you can create a new file and save it with .py extension. For example, hello.py
to run the python file. open the command prompt, 
python hello.py in the command prompt you can see the output.
hello world
congratulations..! you have successfully ran your first python file