Python Session 2
Python Session 2
Students copy
Variable
A variable is user defined identifier to reserve memory locate to store values. That means when some
values are assigned it will allocate some space in memory.
Python is a dynamically typed language. No advance declaration of identifier with a particular data type.
You can store integers, decimals, or characters in these variables.
Akhil
Python Training session 2
Students copy
Multiple Assignments:
You can also assign a single value to several variables simultaneously. For example:
a=b=c=1
Here, an integer object is created with the value 1, and all three variables are assigned to the same
memory location. You can also assign multiple objects to multiple variables. For example:
a, b, c = 1, 5.4, "Hello World"
Here two integer objects with values 1 and 2 are assigned to variables a and b, and one string object
with the value "john" is assigned to the variable c.
In interactive mode, the last printed expression is assigned to the variable _. Useful when using python
as desk calculator.
Data Type:
Apart from True False None python has five standard Data types Number, String, List, Tuple and
Dictionary.
del is a statement to delete the reference to any allocated data type objects.
Python supports four different numerical types:
A complex number consists of an ordered pair of real floating point numbers denoted by a + bj, where a
is the real part and b is the imaginary part of the complex number.
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5.0*6) / 4
5.0
>>> 8 / 5.0
1.6
Besides numbers, Python can also manipulate strings, which can be expressed in several ways. They can
be enclosed in single quotes ('...') or double quotes ("...") with the same result, \ can be used to escape
quotes:
>>> 'spam eggs' # single quotes
'spam eggs'
>>> 'doesn\'t' # use \' to escape the single quote...
Akhil
Python Training session 2
Students copy
"doesn't"
>>> "doesn't" # ...or use double quotes instead
"doesn't"
>>> '"Yes," he said.'
'"Yes," he said.'
>>> "\"Yes,\" he said."
'"Yes," he said.'
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. End of lines are
automatically included in the string
Strings can be concatenated (or appended) with the + operator, and repeated with *:
The plus ( + ) sign is the string concatenation operator, and the asterisk ( * ) is the repetition operator.
'Python This feature is particularly useful when you want to break long strings:
>>> text = ('Put several strings within parentheses '
'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'
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. Also called as negative indexing.
Strings can be indexed (subscripted), with the first character having index 0. There is no separate
character type; a character is simply a string of size one:
Indices may also be negative numbers, to start counting from the right:
Akhil
Python Training session 2
Students copy
In addition to indexing, slicing is also supported. While indexing is used to obtain individual
characters, slicing allows you to obtain a substring:
Akhil
Python Training session 2
Students copy
Akhil