Category Archives: Python

Python Numbers

Python Numbers

The Number data type is used to hold the numeric values. Python supports following numerical data types

Type Format Description
int a = 10 Signed Integer
long a = 345L (L) Long integers, they can also be represented in octal and hexadecimal
float a = 45.67 (.) Floating point real values
complex a = 3.14J (J) Contains integer in the range 0 to 255.

Python Operators

Python Operators

In Python programming, an operator is a special symbol that is used to carry out some specific operation on its operand. In Python, we have rich set of built in operators to carry out different type of operations. There are operators for assignment, arithmetic operations, logical operations and comparison operations etc. Python operators can be used with many types of variables or constants, but some of the operators are restricted to work on specific data types. Most operators are binary, meaning they take two operands, but a few are unary and only take one operand.

Type of operators in Python

Python supports the following types of operators –

  • Arithmetic Operators
  • Assignment Operators
  • Comparison (Relational) Operators
  • Logical Operators
  • Identity Operators
  • Bitwise Operators
  • Membership Operators

Arithmetic Operators In Python

Arithmetic Operators are used to perform arithmetic operations like addition, subtraction, multiplication, division, %modulus, exponent, etc. For example assume a=5 and b=10, then –

Arithmetic operators in Python
Symbol Name Description Example
+ Addition It perform the addition of both the operands. a + b = 15
Subtraction It subtract right operand from the left a – b = -5
* Multiplication It performs the multiplication of the given operands a * b = 50
/ Division It performs the division of the left operand by the right operand b / a = 2
% Modulus It returns the remainder of the division of left operand by the right b % a = 0(remainder of b/a)
// Floor Division It performs the division of operands where it returns the quotient in which the digits after the decimal point are removed. 15 // 4 = 3, 19//5=3
** Exponent Left operand raised to the power of right a**b = (a to the power b), 3**4=81

Assignment Operators In Python

Assignment operators are used to assign value to a variable, you can assign a variable value or the result of an arithmetical expression.

Assignment operators in Python
Symbol Name Description Example
= Assignment Used to assigns a value to variable(s). c = a + b assigns value of a + b into c
+= Add and assign It is shorthand to add right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a
-= Subtract and assign It is shorthand to subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c – a
*= Multiply and assign It is shorthand to multiply right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a
/= Divide and assign It is shorthand to divide left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / ac /= a is equivalent to c = c / a
%= Modulus and assign It is shorthand to take modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a
**= Exponent and assign It is shorthand to perform exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a
//= Floor Division and assign It is shorthand to performs floor division on operators and assign value to the left operand c //= a is equivalent to c = c // a

Comparison (Relational) Operators In Python

Comparison Operators are used evaluate a comparison between two operands. The result of a comparison operation is a Boolean value that can only be true or false. Comparison Operators are also referred as relational operators.

Here is full list of Relational operators available in Python Programming Language –

Relational operators in Python
Operator Meaning Example Description
== Equal to a == b 1 if a equal to b; 0 otherwise
!= Not equal to a != b 1 if a not equal to b; 0 otherwise
> greater than a > b 1 if a > b; 0 otherwise
< less than a < b 1 if a < b; 0 otherwise
>= greater than or equal to a >= b 1 if a >= b; 0 otherwise
<= less than or equal to a <= b 1 if a <= b; 0 otherwise

Logical Operators In Python

Logical operators are used to combine expressions with conditional statements using logical (AND,OR,NOT) operators, which results in true or false.

There are 3 logical operators available in Python Programming Language

Logical operators in Python
Operator Meaning Example Description
and Logical AND a and b Logical AND of a and b
or Logical OR a or b Logical OR of a and b
not Logical NOT not a Logical NOT of a

Identity Operators In Python

Identity Operators are used to compare the memory location of two objects.
There are two identify operators available in Python (is, is not).

Identity operators in Python
Operator Name Meaning Example
is Name It returns True if both the operands are identical and refers to the same object x is True
is not Name It returns True if both the operands are not identical or they refer to different objects x is not True

Bitwise Operators In Python

Bitwise operator are used to perform bit level operation over its operand.

Let A = 60; and B = 13;

Binary equivalent

A = 0011 1100

B = 0000 1101

Bitwise operators in Python
Operator Meaning Example Description
& Binary AND (A & B) It returns 12 which is 0000 1100
| Binary OR (A | B) It returns 12 which is 0000 1100
^ Binary XOR (A ^ B) It returns 49 which is 0011 0001
~ Ones Complement (~A ) It returns -60 which is 1100 0011
<< shift left A << 2 It returns 240 which is 1111 0000
>> shift right A >> 2 It returns 15 which is 0000 1111

 

Membership Operators In Python

Membership operators are used to test membership in a sequence, such as strings, lists, or tuples.
There are two membership operators available in python.

Membership operators in Python
Operator Name Meaning Example
in Name It returns True if value/variable is found in the given sequence, FALSE otherwise 5 in x
not in Name It returns True if value/variable is not found in the given sequence, FALSE otherwise 5 not in x

Python Operators Precedence

Highest precedence at top, lowest at bottom.
Operators in the same box evaluate left to right.

Operator precedence rule in Python
Operators Meaning
() Parentheses
** Exponent
+x, -x, ~x Unary plus, Unary minus, Bitwise NOT
*, /, //, % Multiplication, Division, Floor division, Modulus
+, – Addition, Subtraction
<<, >> Bitwise shift operators
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
==, !=, >, >=, <, <=, is, is not, in, not in Comparisions, Identity, Membership operators
not Logical NOT
and Logical AND
or Logical OR

Python Keywords & Identifiers

Python Identifier

An identifiers is a name given to program elements such as variables , array, class and functions etc. An identifier is a sequence of letters, digits, and underscores, the first character of which can not be a digit. Following rules must be kept in mind while naming an identifier.

  • The first character must be an alphabet (uppercase or lowercase) or can be an underscore.
  • An identifier can not start with a digit.
  • All succeeding characters must be alphabets or digits.
  • No special characters like !, @, #, $, % or punctuation symbols is allowed except the underscore”_”.
  • No two successive underscores are allowed.
  • Keywords can not be used as identifiers.

Note :- Python is a case-sensitive programming language, which means “Abc” and “abc” are not the same.

Python Keywords

Python keywords are set words that cannot be used as an identifier in program. These words are known as “Reserved Words” or “Keywords”. Python Keywords are standard identifiers and their meaning and purpose is predefined by the compiler.

Below is list of available keywords in Python programming Language.

Python Keywords List
and assert in
del else raise
from if continue
not pass finally
while yield is
as break return
elif except def
global import for
or print lambda
with class try
exec

Note :-We cannot use keywords for declaring Variable Name,For Function Name and for declaring Constant Variable

Python Hello World Program

Creating Python Hello World Program

The “Hello world!” program is a simplest program that will display some text on the screen. The “Hello world!” program is a simple yet complete program for beginners that illustrates the basic syntax of any programming language. The “Hello world!” program gives you a way to test systems and programming environment.

This tutorial will guide you through writing a basic “Hello, World” program in Python.

Prerequisites

Before starting with this tutorial we assume that you already have Python installed (If you do not have Python installed then install it before you get started) as well as a local programming environment set up on your computer.

Step 1:- Create a file called “hello_world.py” using a text editor program of your choice. The .py file extension is used to specify Python language file.

Step 2:- Let’s open the “hello_world.py” file that we created, and put the following line of code in it and save it.

The print() is a function that tells Python to display or output the content inside the parentheses.

Step 3:- Now, open terminal or command prompt and switch to the folder where our “hello_world.py” file is saved. Run the program by typing the following command –

Once the program is executed it will print “Hello World!”.

Output:-