·t.2 Featu res of Python .
Follow i ng are some of the feature s of Python :
1, Easy to learn 2. Interpr eted
3. Platfor m indepe ndent 4. Cross Platfor m and Portabl e
5. Object Oriente d 6. Large Standard Library
u
7. Free and Open-source 8. Auto-destructive (autom atic memo ry m anage ment)
9. Suppo rts except ion handlin g
2.4.1 The wh ile Loop
Pyth on ' s while-loop is exactly same as that we used and C, C++ and Java . The only genera l sy ntax is
changed. Its
syn t ax is :
imtrnhzatio n
whil e conditi on :
statement - I
statement-2
statement-n
increment/decrement
main.py :J. Output
1 s= "Sharad Institute"
2 print (s[ 1 :4 : 2])
3 print (s[ 10 : 2 : - 1J)j
Output ,,, <
·O·
, I '
'
Ascending Order : (0, I, 2, 3, 5)
Descending Order : [5, 3, 2, I, OJ
Programmi ng with 'Python' 1.22 trrtroduct lon and Syntax o1 Pytho,n Progra~
1111 Numbers Data Type
• Numbe r data types store numeric values. Numbe r obj,ects ar e created wh en we assign
a value to
them.
• Integer s, floating point numbe rs and complex numbe rs falls under Pyth on numbers
categor y. They
are defined as int, float and complex in Python ,
1. Intege r,:
• An int data type represe nts an integer nu mber. An integer number is a numbe r withou
t any decimal
or fraction al point.
• For exampl,e, a = 57, here a is c~lled the int type variable and stores integer value 57.
• These represe nt numbe rs in the range - 214748 3648 to 214748 3647.
2. Floating Point Numbe rs:
• The float data type represe nts the floating point numbe r. The floating point numbe r is
a num ber that
contain s a decima l point. Examples of floating point numbe rs, 0.5, - 3.445, 330.44.
For example,
num == 2.345.
3. Complex Numbe rs:
• A comple x numbe r is a numbe r that is written in the form of a+bj. Here, a represe nts the
real part of
the number and b r epresen ts the imagin ary part of the number.
• The suffix J or j after b represe nts the square root value of - 1. The part a and b may
contain the
integer s or floats. For example, 3+5j, 0 .2+10.Sj are comple x numbe rs.
• For exampl e, in C=-1-5.Sj, the complex numbe r
is -1-5.Sj and is assigne d to the variable C.
Hence, the Python interpr eter takes the data
type of the variable Casa complex type. Fig. t.27; Types of Numbers Data Type
• We can use the typeO functio n to kn ow which class a variabl e or a value belongs
to and the
Jsinstan ceO functio n to check if an object belongs to a particu lar class.
.
B String Data Type
• String is a collection of group of characters. Strings are identified as a contiguous set of characters
enclosed in single quotes (' ') or double quotes (" ").
• Any letter, a number or a symbol could be a part of the string. Strings are unchangeable
(immutable). Once a string is created, it cannot be modified.
• Strings in python support Unicode cha~acters. The default encoding for Python source code is
UTF-8. So, we can also say that String is a sequence of Unicode characters.
• Strings are ordered. Strings preserved the order of characters inserted.
__ J ___ _
- List Data Type
• Ust is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible.
• List can contain heterogeneous values such as integers, floats, strings, tuples, lists and dictionaries
but they are common_ly used to store collections of homogeneous objects.
• The list data type in Python programming is Just like·an array that can store a group of elements and
we can refer to these elements using a single name.
• Declaring a list is pretty straight forward. Items. separated by commas (,) are enclosed within
brackets [ ]. · · · ·
- Tuple Data Type
• Tuple is an ordered sequence of items same as list. The only difference is that tuples are immutable.
Tuples once created cannot be modified.
• Tuples are used to write-protect data and are usually faster than list as it cannot change
dynamically. It is defined within parentheses () where items are separated by commas (,).
• A tuple data type in python programming is similar to a list data type, which also- contains
heterogeneous items/ elements.
5. ·wntc a Python pmg,ra m to calculate !Um of di~t of gi\i'ffll numlber.
Enter c1 nun1·oer: l2J
Tiu: swn a f d1 g1 ts nf the number :ht 6
6. \Vnte II Py1hon 1•roar1 m to And po Hnd rome or &i\,ltll number.,
Eruer a m:1umber: l 2 l
l 2 l is a palindrome.
Output ,•1
-0-I '
1
<
student ={ .•
'I
1 . ' I
' 1 ' I
I I
I
•
• I
. '
'}
2 pt-int (student)
3
4 student [ ] = I'. . .
5 p r- in t ( student )
6
7 student. pop ( )
8 p 1- i n t ( student ) I
~
Output -o-
1
,•1
• '
{ 1 : 'Vijay', 2: 'Santosh', 3: 'Yogita'}
{ 1 : 'Vijay', 2: 'Santosh' 3: 'Ram'} I
{ 1 : 'Vijay', 3: 'Ram'}