Basics Of Python

Python
          Python Logo

Firstly we leanred about Python, like the history of Python and which types of applications are created with Python.

Now, we will discuss the history ofthe Python programming language.

Python was designed by “Guido Van Rossum” at CWI (center for wisdom and informatics) located in Netherland.

In python we create windows and web based applications.

Python

  • Python is strongly high level language
  • Python is interpreted
  • Python is reflective
  • Dynamically type
  • Open source
  • Multi paradigm

Note: Sometimes we can say Python is scripting language and very easy to write a program in python

Python is dynamically typed, which means the data types can be defined at the time of execution of program.

Example

Value=2;
And value=’Ajay’;
(Show in picture below)

code

Interactive Execution of Expression

Python programming language allows users to do words just like a calculator, you type na expression and python executes the expression immediately (within seconds).

Let’s go and check how it works.

Open: python(command line) application shown in picture below,

After launching application type expression for execution

>>>2+3

5 // this is result of expression 2+3
(Shown in picture)

apps


Key: Three right arrows (>>>) are the expression prompt telling python system is waiting for your expression

You can see result of your expression, after pressing enter/return.

We can perform all arithmetic operations (shown in figure).

Comments

  • Like other languages, python also has the facilityto  comment on our statement and function etc so reader can understand functionality more easily.
  • We give comments in python using hash mark (#)
  • Comments are ignored by python and used as information for the reader
    Example : shown in picture below.
    >>>3+2 #sum of two number.

    sum

Data Types in Python

  • Integer
  • Floating (float)
  • String
  • Boolean

Integer

  • Integer stores any non fractional number counting.
  • Python does not upper bounary on the size of integer
  • Size is also dependant on the number
  • Example: size of 5 is 12 bytes on 32 bit operating system.
    Size of 9999999 is 20 bytes on 32 bit operating system.
  • Integer is positive and negative both
Floating (float)
  • Float stores any fractional number.
  • Python does not upper bound on the size of float.
  • Size of float is also depend on number
  • Python used scientific notation for float number.
    1.2e3 means 1200.0
String
  • String stores characters or sentence
  • Python does not upper bound on the size of string
  • Size of string also depends on length of string
Boolean
  • Boolean type represents a true or false value
Example

>>>15 < 21
True
>>> 2 > 5
False

Variables

Variables are just a name of data type but reserved memory locations to store values for different data type. This means that when you create a variable you reserve some space in memory.

Size of reserve memory is depend on the data type of variable

Python allows you to assign a more than one variable at a time

Example: A=b=c=d=10;

Operators

Operators are the constructs which can manipulate the value of operands

Types of Operator

Python supports the following type of operator lists below,
  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators

Arithmetic Operators

they are following listed table.
b % a = 0

Operator

Description

Example

+ (Addition)

Performing addition of number

a + b = 30

- (Subtraction)

Performing Subtraction of number

a – b = -10

* (Multiplication)

Performing multiplication of number

a * b = 200

/ (Division)

Performing division of number

b / a = 2

% (Modulus)

returns remainder

b % a = 0

** (Exponent)

Performs exponential (power) calculation on operators

a**b =10 to the power 20

Comparison Operators

assume a=10 and b=20

Operator

Description

Example

==

If the values of two operands are equal, then the condition becomes true.

(a == b) is not true.

!=

If values of two operands are not equal, then condition becomes true.

 

>

If the value of left operand is greater than the value of right operand, then condition becomes true.

(a > b) is not true.

<

If the value of left operand is less than the value of right operand, then condition becomes true.

(a < b) is true.

>=

If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.

(a >= b) is not true.

<=

If the value of left operand is less than or equal to the value of right operand, then condition becomes true

(a <= b) is true.

Logical operator

assume a=10 and b=20

Operator

Description

Example

and (Logical AND)

If both the operands are true then condition becomes true.

(a and b) is true.

or (Logical OR)

If any of the two operands are non-zero then condition becomes true

(a or b) is true.

not Logical NOT

Used to reverse the logical state of its operand.

Not(a and b) is false.

Bitwise Operators

bitwise operator work on bits.

assume a=60 and b=13

binary of a and b are

a=0011 1100
b=0000 1101

Operator

Description

Example

& (Binary AND)

Operator copies a bit to the result if it exists in both operands

a&b = 0000 1100

| (Binary OR)

It copies a bit if it exists in either operand.

a|b = 0011 1101

^ (Binary XOR)

It copies the bit if it is set in one operand but not both

a^b = 0011 0001

~ (Binary Ones Complement)

It is unary and has the effect of 'flipping' bits.

~a = 1100 0011

<< (Binary Left Shift)

The left operands value is moved left by the number of bits specified by the right operand

a << = 240 (means 1111 0000)

>> (Binary Right Shift)

The left operands value is moved right by the number of bits specified by the right operand

a >> = 15 (means 0000 1111)

Assignment Operators

Operator

Description

Example

=

Assigns values from right side operands to left side operand

c = a + b assigns value of a + b into c

Print statement

print something on screen in python we use.

print function

example(shown in picture)

print'c# corner'.

print

Input from user.

(shown in picture)

input

Read more articles on Python:

Up Next
    Ebook Download
    View all
    Learn
    View all