Python

You might also like

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

Variables

are nothing but reserved memory locations to store values. This


means that when you create a variable you reserve some space
in memory.

Assigning Values to Variables

The equal sign (=) is used to assign values to variables. The operand to the left of
the = operator is the name of the variable and the operand to the right of the =
operator is the value stored in the variable.
value_1 = 100
value_2 = 1000.0
value_3 = "John"

Multiple Assignment
Python allows you to assign a single value to several variables simultaneously.
a=b=c=1
id,age,name = 1001,25,"Amit"

Data Types
Python has five standard data types −
Numbers
String
List
Tuple
Dictionary

Numbers :
Number data types store numeric values. Number objects are created when you
assign a value to them.

var1 = 1
var2 = 10
Python supports four different numerical types
int (signed integers)
long (long integers, they can also be represented in octal and hexadecimal)
float (floating point real values)
complex (complex numbers)

Strings

Strings in Python are identified as a contiguous set of characters represented in


the quotation marks. Python allows for either pairs of single or double quotes
The plus (+) sign is the string concatenation operator and the asterisk (*) is the
repetition operator.

str = 'Hello Radiant'

print str # Prints complete string


print str[0] # Prints first character of the string
print str[2:5] # Prints characters starting from 3rd to 5th
print str[2:] # Prints string starting from 3rd character
print str * 2 # Prints string two times
print str + "TEST" # Prints concatenated string
Tuple
A tuple is a collection which is ordered and unchangeable. In Python tuples are
written with round brackets.

Example
thistuple = ("apple", "banana", "cherry")
print(thistuple)

Access Tuple Items You can access tuple items by referring to the index
number, inside square brackets:
Example: Return the item in position 1:
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])

Change Tuple Values Once a tuple is created, you cannot change its values.
Tuples are unchangeable.

Example

You cannot change values in a tuple:

thistuple = ("apple", "banana", "cherry")


thistuple[1] = "blackcurrant"
# The values will remain the same: print(thistuple)

Example

tup1 = ('physics', 'chemistry', 1997, 2000);


tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";
Accessing Values in Tuples

To access values in tuple, use the square brackets for slicing along with the
index or indices to obtain value available at that index.

example:

tup1 = ('physics', 'chemistry', 1997, 2000);


tup2 = (1, 2, 3, 4, 5, 6, 7 );
print "tup1[0]: ", tup1[0];
print "tup2[1:5]: ", tup2[1:5];

Updating Tuples

Tuples are immutable which means you cannot update or change the values of
tuple elements. You are able to take portions of existing tuples to create new
tuples as the following

example :

tup1 = (12, 34.56);


tup2 = ('abc', 'xyz');

# Following action is not valid for tuples


# tup1[0] = 100;

# So let's create a new tuple as follows


tup3 = tup1 + tup2;
print tup3;

Delete Tuple Elements


Removing individual tuple elements is not possible. There is, of course, nothing
wrong with putting together another tuple with the undesired elements
discarded. To explicitly remove an entire tuple, just use the del statement.

example :

tup = ('physics', 'chemistry', 1997, 2000);


print tup;
del tup;
print "After deleting tup : ";
print tup;

Basic Tuples Operations

Tuples respond to the + and * operators much like strings; they mean
concatenation and repetition here too, except that the result is a new tuple,
not a string.
Python Expression Results Description

len((1, 2, 3)) 3 Length

(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) Concatenation

('Hi!',) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!') Repetition

3 in (1, 2, 3) True Membership

for x in (1, 2, 3): print x, 123 Iteration


Python - Loops

A loop statement allows us to execute a statement or group of statements


multiple times.
There may be a situation when you need to execute a block of code several
number of times.
Python programming language provides following types of loops to handle
looping requirements. Like while loop, for loop, nested loops.

while loop

A while loop statement in Python programming language repeatedly executes a target


statement as long as a given condition is true.

Syntax:
while expression:
statement(s)

count = 0
while (count < 9):
print 'The count is:', count
count = count + 1
for Loop
It has the ability to iterate over the items of any sequence, such as a list or a
string.

Syntax:
for iterating_var in sequence:
statements(s)

Example :
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)

nested loops
Python programming language allows to use one loop inside another loop.

Syntax :
for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s)
Example :
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)

Python IF...ELIF...ELSE Statements

An else statement can be combined with an if statement. An else statement


contains the block of code that executes if the conditional expression in the if
statement resolves to 0 or a FALSE value.

Syntax

if expression:
statement(s)
else:
statement(s)

Python Conditions :

Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b

Example :
a = 33
b = 200
if b > a:
print("b is greater than a")
ELSE Statements

An else statement can be combined with an if statement. An else statement


contains the block of code that executes if the conditional expression in the if
statement resolves to 0 or a FALSE value.

Syntax :

if test_expression1:
statements(1)

elif test_expression2:
statements(2)

elif test_expression3:
statements(3)

else:

statements(4)

Modules
A module allows you to logically organize your Python code. Grouping related
code into a module makes the code easier to understand and use. A module is
a Python object with arbitrarily named attributes that you can bind and
reference.

def raja(name):
print("Hello" + name)

import radiant

radiant.raja("Class XI-A and B Students...")

Note : Import is a keyword.

You might also like