Professional Documents
Culture Documents
Python
Python
Python
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
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
Example
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:
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 :
example :
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
while loop
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)
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
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