4. Python Variables
Variables are nothing but reserved memory
locations to store values. This means that when
you create a variable you reserve some space in
memory..
Python variables do not need explicit declaration
to reserve memory space. The declaration
happens automatically when you assign a value to
a variable.
python allows you to assign a single value to
several variables or multiple objects to multiple
variables simultaneously.
4
a = b = c = 1
a,b,c = 1,2,"john"
5. Data types
Python has five standard data types
Numbers
String
List
Tuple
Dictionary
5
6. Python Numbers
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)
6
var1 = 1 var2 = 10
del var del var_a, var_b
7. Python 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. Subsets of strings can be taken
using the slice operator ([ ] and [:] ) with
indexes starting at 0 in the beginning of the
string and working their way from -1 at the
end.
The plus (+) sign is the string concatenation
operator and the asterisk (*) is the repetition
operator.
7
8. 8
Example
User inputs:
Enter your name: John
Enter your age:25
Out put:
Welcome, John ! you are now 25
Write a program which accepts name and age input from
user and print it?
9. Python Operators
Assignment operators(=)
Arithmetic operators(+,*,/,-)
Comparison operators(<,>,<=)
Logical operators(and, or, not)
Bitwise operators(^,&,|)
Data Type Conversions: Implicit or explicit
9
10. 10
Arrays
An array is a data structure consisting of a collection of elements
(values or variables), each identified by at least one array index
or key
Arrays and Lists
Index Notation
Displaying Array Members
Multidimensional Arrays
11. Python Lists
The most basic data structure in Python is
the sequence. Each element of a
sequence is assigned a number - its
position or index. The first index is zero,
the second index is one, and so forth.
The list is a most versatile datatype
available in Python which can be written
as a list of comma-separated values
(items) between square brackets.
Important thing about a list is that items
in a list need not be of the same type.
Lists are mutable objects that can change
their values 11
list1 = ['physics', 'chemistry', 1997,
2000]; list2 = [1, 2, 3, 4, 5 ]; list3 =
["a", "b", "c", "d"]
12. Python Lists
Accessing Values in Lists: use
the square brackets for slicing
along with the index or indices
to obtain value available at that
index.
Updating Lists: You can update
single or multiple elements of
lists by giving the slice on the
left-hand side of the assignment
operator, and you can add to
elements in a list with the
append() method.
12
13. Python Lists
Delete List Elements: To remove a
list element, you can use either the
del statement if you know exactly
which element(s) you are deleting
or the remove() method if you do
not know.
Basic List Operations: Lists respond
to the + and * operators much like
strings; they mean concatenation
and repetition here too, except
that the result is a new list, not a
string.
13
16. Tuples
A tuple is a collection of
objects which ordered
and immutable.
The differences between
tuples and lists are, the
tuples cannot be
changed unlike lists and
tuples use parentheses,
whereas lists use square
brackets.
16
tup1 = ('physics', 'chemistry',
1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";
17. Tuples
Accessing Values in Tuples
Updating Tuples
Delete Tuple Elements
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.
17
19. Dictionaries
Each key is separated from its value by a colon (:), the items are
separated by commas, and the whole thing is enclosed in curly
braces.
Keys are unique within a dictionary while values may not be. The
values of a dictionary can be of any type, but the keys must be of an
immutable data type such as strings, numbers, or tuples.
19
21. Dictionaries
Properties of Dictionary Keys
More than one entry per key
not allowed. Which means
no duplicate key is allowed.
Keys must be immutable.
21
dict = {'Name': 'Zara', 'Age': 7, 'Name':
'Manni'}
print "dict['Name']: ", dict['Name']
dict = {['Name']: 'Zara', 'Age': 7} print
"dict['Name']: ", dict['Name']
#9: Arithmetic :Addition, Subtraction, division, multiplication, module, floor division
Assignment: =,+=,-+ and others
Comparison: strict greater and lessor, greater/less than or equal, equal or not equal
Logical: and, or and Not
Bitwise: or , and, xor