




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
programming this is programming docs
Typology: Exercises
1 / 365
This page cannot be seen from the preview
Don't miss anything!





























































































DURGASOFT, # 202, 2 nd^ Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038, Language Fundamentals Introduction Python is a general purpose high level programming language. Python was developed by Guido Van Rossam in 1989 while working at National Research Institute at Netherlands. But officially Python was made available to public in 1991. The official Date of Birth for Python is : Feb 20th 1991. Python is recommended as first programming language for beginners. Eg1: To print Helloworld: Java: 1 ) public class HelloWorld 2 ) { 3 ) p s v main(String[] args) 4 ) { 5 ) SOP("Hello world"); 6 ) } 7 ) } C: 1 ) #include<stdio.h> 2 ) void main() 3 ) { 4 ) print("Hello world"); 5 ) } Python: print("Hello World")
DURGASOFT, # 202, 2 nd^ Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038, Eg2: To print the sum of 2 numbers Java: 1 ) public class Add 2 ) { 3 ) public static void main(String[] args) 4 ) { 5 ) int a,b; 6 ) a = 10 ; 7 ) b= 20 ; 8 ) System.out.println("The Sum:"+(a+b)); 9 ) } 10 ) } C: 1 ) #include <stdio.h> 2 ) 3 ) void main() 4 ) { 5 ) int a,b; 6 ) a =10; 7 ) b=20; 8 ) printf("The Sum:%d",(a+b)); 9 ) } Python: 1 ) a= 10 2 ) b= 20 3 ) print("The Sum:",(a+b)) The name Python was selected from the TV Show "The Complete Monty Python's Circus", which was broadcasted in BBC from 1969 to 1974. Guido developed Python language by taking almost all programming features from different languages
DURGASOFT, # 202, 2 nd^ Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
Python is high level programming language and hence it is programmer friendly language. Being a programmer we are not required to concentrate low level activities like memory management and security etc..
Once we write a Python program,it can run on any platform without rewriting once again. Internally PVM is responsible to convert into machine understandable form.
Python programs are portable. ie we can migrate from one platform to another platform very easily. Python programs will provide same results on any paltform.
In Python we are not required to declare type for variables. Whenever we are assigning the value, based on value, type will be allocated automatically.Hence Python is considered as dynamically typed language. But Java, C etc are Statically Typed Languages b'z we have to provide type at the beginning only. This dynamic typing nature will provide more flexibility to the programmer.
Python language supports both Procedure oriented (like C, pascal etc) and object oriented (like C++,Java) features. Hence we can get benefits of both like security and reusability etc
We are not required to compile Python programs explcitly. Internally Python interpreter will take care that compilation. If compilation fails interpreter raised syntax errors. Once compilation success then PVM (Python Virtual Machine) is responsible to execute.
We can use other language programs in Python. The main advantages of this approach are:
DURGASOFT, # 202, 2 nd^ Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
We can use Python programs in any other language programs. i.e we can embedd Python programs anywhere.
Python has a rich inbuilt library. Being a programmer we can use this library directly and we are not responsible to implement the functionality. etc...
1.CPython: It is the standard flavor of Python. It can be used to work with C lanugage Applications
DURGASOFT, # 202, 2 nd^ Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
A name in Python program is called identifier. It can be class name or function name or module name or variable name. a = 10
DURGASOFT, # 202, 2 nd^ Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
Alphabet Symbols (Either Upper case OR Lower case)
If Identifier is start with Underscore (_) then it indicates it is private.
Identifier should not start with Digits.
Identifiers are case sensitive.
We cannot use reserved words as identifiers Eg: def=10
There is no length limit for Python identifiers. But not recommended to use too lengthy identifiers.
Dollor ($) Symbol is not allowed in Python.
1 ) 123total 2 ) total123 √ 3 ) java2share √ 4 ) ca$h 5 ) abc_abc √ 6 ) def 7 ) if
DURGASOFT, # 202, 2 nd^ Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038, Data Types Data Type represent the type of data present inside a variable. In Python we are not required to specify the type explicitly. Based on value provided,the type will be assigned automatically.Hence Python is Dynamically Typed Language. Python contains the following inbuilt data types
DURGASOFT, # 202, 2 nd^ Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
We can use int data type to represent whole numbers (integral values) Eg: a= type(a) #int Note: In Python2 we have long data type to represent very large integral values. But in Python3 there is no long type explicitly and we can represent long values also by using int type only. We can represent int values in the following ways
It is the default number system in Python The allowed digits are: 0 to 9 Eg: a =
The allowed digits are : 0 & 1 Literal value should be prefixed with 0b or 0B Eg: a = 0B a =0B a=b
The allowed digits are : 0 to 7 Literal value should be prefixed with 0o or 0O.
DURGASOFT, # 202, 2 nd^ Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038, Eg: 1 ) >>> oct( 10 ) 2 ) '0o12' 3 ) >>> oct(0B1111) 4 ) '0o17' 5 ) >>> oct(0X123) 6 ) '0o443'
We can use hex() to convert from any base to hexa decimal Eg: 1 ) >>> hex( 100 ) 2 ) '0x64' 3 ) >>> hex(0B111111) 4 ) '0x3f' 5 ) >>> hex(0o12345) 6 ) '0x14e5'
We can use float data type to represent floating point values (decimal values) Eg: f=1. type(f) float We can also represent floating point values by using exponential form (scientific notation) Eg: f=1.2e print(f) 1200. instead of 'e' we can use 'E' The main advantage of exponential form is we can represent big values in less memory. ***Note: We can represent int values in decimal, binary, octal and hexa decimal forms. But we can represent float values only by using decimal form. Eg: 1 ) >>> f=0B11. 01 2 ) File "
DURGASOFT, # 202, 2 nd^ Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038, 4 ) ^ 5 ) SyntaxError: invalid syntax 6 ) 7 ) >>> f=0o123. 456 8 ) SyntaxError: invalid syntax 9 ) 10 ) >>> f=0X123. 456 11 ) SyntaxError: invalid syntax
A complex number is of the form a and b contain intergers or floating point values Eg: 3+5j 10+5.5j 0.5+0.1j In the real part if we use int value then we can specify that either by decimal,octal,binary or hexa decimal form. But imaginary part should be specified only by using decimal form. 1 ) >>> a=0B11+5j 2 ) >>> a 3 ) ( 3 +5j) 4 ) >>> a= 3 +0B11j 5 ) SyntaxError: invalid syntax Even we can perform operations on complex type values. 1 ) >>> a= 10 +1.5j 2 ) >>> b= 20 +2.5j 3 ) >>> c=a+b 4 ) >>> print(c) 5 ) ( 30 +4j) 6 ) >>> type(c) 7 ) <class 'complex'>
Real Part Imaginary Part j2 = - 1 j =
DURGASOFT, # 202, 2 nd^ Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038, soft" For this requirement we should go for triple single quotes(''') or triple double quotes(""") s1='''durga soft''' s1="""durga soft""" We can also use triple quotes to use single quote or double quote in our String. ''' This is " character''' ' This i " Character ' We can embed one string in another string '''This "Python class very helpful" for java students'''
slice means a piece [ ] operator is called slice operator,which can be used to retrieve parts of String. In Python Strings follows zero based index. The index can be either +ve or - ve. +ve index means forward direction from Left to Right
DURGASOFT, # 202, 2 nd^ Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038, 1 ) >>> s[ 1 : 40 ] 2 ) 'urga' 3 ) >>> s[ 1 :] 4 ) 'urga' 5 ) >>> s[: 4 ] 6 ) 'durg' 7 ) >>> s[:] 8 ) 'durga' 9 ) >>> 10 ) 11 ) 12 ) >>> s* 3 13 ) 'durgadurgadurga' 14 ) 15 ) >>> len(s) 16 ) 5 Note:
DURGASOFT, # 202, 2 nd^ Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
We can use float() function to convert other type values to float type. 1 ) >>> float( 10 ) 2 ) 10. 3 ) >>> float( 10 +5j) 4 ) TypeError: can't convert complex to float 5 ) >>> float(True) 6 ) 1. 7 ) >>> float(False) 8 ) 0. 9 ) >>> float("10") 10 ) 10. 11 ) >>> float("10.5") 12 ) 10. 13 ) >>> float("ten") 14 ) ValueError: could not convert string to float: 'ten' 15 ) >>> float("0B1111") 16 ) ValueError: could not convert string to float: '0B1111'
We can use complex() function to convert other types to complex type.
We can use this function to convert x into complex number with real part x and imaginary part 0. Eg: 1 ) complex( 10 )==> 10 +0j 2 ) complex(10.5)===>10.5+0j 3 ) complex(True)==> 1 +0j 4 ) complex(False)==>0j 5 ) complex("10")==> 10 +0j 6 ) complex("10.5")==>10.5+0j 7 ) complex("ten") 8 ) ValueError: complex() arg is a malformed string
DURGASOFT, # 202, 2 nd^ Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
We can use this method to convert x and y into complex number such that x will be real part and y will be imaginary part. Eg: complex(10,-2)==>10-2j complex(True,False)==>1+0j
We can use this function to convert other type values to bool type. Eg: 1 ) bool( 0 )==>False 2 ) bool( 1 )==>True 3 ) bool( 10 )===>True 4 ) bool(10.5)===>True 5 ) bool(0.178)==>True 6 ) bool(0.0)==>False 7 ) bool( 10 - 2j)==>True 8 ) bool( 0 +1.5j)==>True 9 ) bool( 0 +0j)==>False 10 ) bool("True")==>True 11 ) bool("False")==>True 12 ) bool("")==>False