E N D
UNIT-01: INTRODUCTION AND SYNTAX OF PYTHON PROGRAM (MARKS: 08) BY MS. A. V. WANKAR
PYTHON FEATURES 1. Easy to learn and Use: • Since it has few keywords, simple structure and clearly defined syntax which allows the user to learn and use easily. 2. Interactive: • Python has support for an interactive mode which allows interactive testing and debugging of snippets of code. 3. object-oriented • Python is object oriented programming languages which follows concepts of classes and objects, Inheritance, data abstraction etc. 4. Interpreted: • Python code is interpreted by interpreter line by line at a time.
PYTHON FEATURES (CONT…) 5. Platform Independent: • It is platform independent programming language. Its code easily run on any platform such as windows, Linux, Unix etc. 6. Portable: • Python can run on a wide variety of hardware platforms and has the same interface on all platforms. • Portable applications are those which runs on every OS and on every processor without considering their architectures and vendors. • i.e. portability=platform independent +Architectural neutral 7. High level language: • Python is general purpose high level language. 8. Free and Open Source: • Python is open source so you can freely download and use it.
PYTHON FEATURES (CONT…) 9. GUI Programming Support: • Graphical User interfaces can be made using a module such as PyQt5, PyQt4, wxPython (cross-platform GUI toolkit), or Tk GUI toolkit in python. • PyQt5 is the most popular option for creating graphical apps with Python. 10. Large Standard Library: • Python has a large standard library which provides a rich set of module and functions so you do not have to write your own code for every single thing. • There are many libraries present in python for such as regular expressions, unit-testing, web browsers, etc.. 11. Dynamically Typed Language: • It means the type (for example- int, double, long, etc.) for a variable is decided at run time not in advance because of this feature we don‟t need to specify the type of variable. 12. Garbage collection • It supports automatic garbage collection. 13. Integrated language: • Python is an Integrated language because we can easily integrated python with other languages like c, c++, etc.
APPLICATIONS OF PYTHON 1. Machine Learning 2. GUI Applications(like Kivy,Tkniter,pyQt etc.) 3. Web frameworks (like Django used by youtube, instagram, dropbox etc.) 4. Image processing (like OpenCV, Pillow) 5. Web scraping 6. Multimedia 7. Scientific Computing 8. Gaming 9. Robotics 10. System programming 11. Text Processing etc.
PYTHON KEYWORDS Keywords are the reserved words in Python. • • We cannot use a keyword as a variable name, function name or any other identifier. • In Python, keywords are case sensitive. • There are 33 keywords in Python 3.7. May vary with time. • All the keywords except , and are in lowercase.
PYTHON IDENTIFIERS An identifier is a name given to entities like class, functions, variables, etc. Rules for writing identifiers In Python: Identifiers can be a combination of letters in lowercase(a to z) or uppercase(A to Z) or digits(0 to 9) or an underscore( _ ). • E.g. Add, simple, area_circle, add12 – all valid • E.g. $new, area circle – all invalid An identifier cannot start with a digit. Keywords cannot be used as identifiers. We cannot use special symbols like !, @, #, $, % etc. in our identifier. An identifier can be of any length. • • • • • • •
Memory x = 3 y = 4 z = x + y y x 4 3 7 z
COMMENTS IN PYTHON It is a container for storing values. • An entity whose value can change • A name given to a location in memory •
COMMENTS IN PYTHON • Single line comment: •Single line comment starts with # in python •E.g. #print(“Hello Word”) • Multi line comment: •Multiline comment can be given by by adding # on each line. •By using „ „ „ ……..….‟ ‟ ‟ or “ “ “………. ” ” ” • E.g. „„„ a=10; b=20 print(a+b) ‟‟‟
SYNTAX OF PRINT() IN PYTHON print(value(s), sep= ‘ ‘, end = ‘\n’, file=sys.stdout, flush=False) Parameters: value(s) : Any value, and as many as you like. Will be converted to string before printed sep=’separator’ :(Optional) Specify how to separate the objects, if there is more than one. Default : ‟ „ end=’end’:(Optional) Specify what to print at the end.Default : „\n‟ file : (Optional) An object with a write method. Default :sys.stdout flush : (Optional) A Boolean, specifying if the output is flushed (True) or buffered (False). Default: False Returns: It returns output to the screen.
EXAMPLES 1. print(“This is gp ambad”) 2. a=5 print(“Value of a=”,a) 3. print(1,2,3,4) 4. print(1,2,3,4,sep=„#‟) 5. print(1,2,3,4,sep=„#‟,end=„&‟)
FORMATTING THE OUTPUT 1. Sometimes we might want to look out O/P attractive, It is possible by using str.format() method. 2. format() function can be used with any string object. e.g. x=12.3456789;y=10 Print(“The value of x is {} and y is {}”.format(x,y)) 3. print(“I love {0} and {1} both”.format(“python”,”PHP”) 4. print(“I love {1} and {0} both”.format(“python”,”PHP”) 5. print(“Hello {name},{greeting}”.format(“Shyam”,”Good Morning”)) 6. print(“The value of x is %d” %x) 7. print(“The value of x is %3.2f” %x)
NUMERIC TYPES •To store numeric type of values data types int, float, complex are used. •Ex. •01. a=10; print(type(a)) •02. a=10.5; print(type(a)) •03. a=3+2j; print(type(a))
BOOLEAN TYPE •It is used to control program. •E.g. a=True print(type(a))# bool •type() function gives type of variable.
COMPLEX NUMBERS •It is used to store complex type of numbers. •E.g. 1. a=3j print(type(a))# complex 2. a=2+3j;b=3+5j c=a+b print(c)
STRINGS IN PYTHON • Contiguous set of characters enclosed in single quote or double quote. • Strings are immutable. • Strings index starts from 0 from left hand side and -1 from RHS. • Multiline strings can be written by using \ at the end of line. • Ex. “”” The quick \ brown fox \ jumps over the \ lazy dog “””
STRINGS IN PYTHON(CONT..) •+ operator can be used for concatenation of two strings. Ex. Print(„Hello‟+‟ „ +‟world‟) •Repeatition operator (*) is used to repeat left operand string according to right operand numeric value. • Ex. Str=“Hello” Print(Str*3)# prints Hello for 3 times.
STRINGS IN PYTHON(CONT..) •We can use slicing operator or cut operator ([] or [:]) to retrieve substrings from the string.
SEQUENCE TYPE: LIST • Lists are used to store multiple items in a single variable which are isolated by commas and enclosed in square bracket([]). • List can have elements of different data types. • Lists are ordered and indexed. • Values stored in a list can be accessed by using index. • We can also use cut operator([:]) with indexes beginning at 0 in the start of the list and working their approach to end-1 • Syntax: list[ start_index : end_index, step ] • End_index in compulsory. Start_index and step are optional. Default value of start_index is 0 and default value of step is 1.
SEQUENCE TYPE: LIST(CONT..) • + is used as concatenation operator. • * is used as repetition • Ex. • List1=[„abcd‟,6,4.87,‟john‟,70.2] • List2=[3,5.7] • Print(List1) • Print(List1[0]) • Print(List1[1:3])#[6,4.87] • Print(List1[2:])#[4.87,‟john‟,70.2] • Print(List1[:3])#[„abcd‟,6,4.87] • Print(List1[-2:])#[„john‟,70.2] • Print(list[:-3])#[„abcd‟,6] • Print(List1+List2)#[„abcd‟,6,4.87,‟john‟,70.2,3,5.7] • Print(List1*2)#[3,5.7,3,5.7]
SEQUENCE TYPE: TUPLE • Tuples are used to store sequence of data of various types they are enclosed in parenthesis (). • Tuples are immutable. • Ex. • tuple=(„abcd‟,6,4.87,‟john‟,70.2) • tuple1=(123,‟john‟) • Print(tuple) • Print(tuple[0]) • Print(tuple[1:3]) • Print(tuple{2:]) • Print(tuple[:3]) • Print(tuple[-2:]) • Print(tuple[:-3]) • Print(tuple+tuple1) • Print(tuple1*2)
SEQUENCE TYPE: SET • A set is a collection which is unordered and unindexed. In python sets are written with curly brackets. • Ex. S={„apple‟,‟banana‟,‟cherry‟} • Print(S) • For i in S: • Print(i)
SEQUENCE TYPE: DICTIONARY • Pythons dictionaries are like kind of hash table type. • They work like associative arrays or hashes & consists of key-value pairs. • A dictionary key can be almost any python type, but are usually numbers or strings. Values on the other hand can be any arbitrary python object. • Dictionaries are enclosed in curly brackets({}) & values can be assigned & accessed using square brackets([]) • E.g. dict={} • dict[1]=„one‟;dict[2]=„two‟;dict[6]=„six‟ • Print(dict)#{1:‟one‟,2:‟two‟,6:‟six‟}
DATA TYPE CONVERSION •Sometimes we need to perform conversion between the data types. To convert between types, you simply use the type name as a function. •There are several built-in functions to perform conversion from one data type to another.
DATA TYPE CONVERSION int(x[,base]): Converts any DT to int (base: base in which string is if DT is string) float(): Converts any DT to float. eval(x) : evaluates string and returns object. ord(): char to int chr(x): int to char hex(): int to hexadecimal string. oct(): int to octal string
DATA TYPE CONVERSION tuple() : converts any DT to tuple. set(): convert to set. list(): any DT to list type. dict(): converts tuple of order(key,value) into dictionary. str(): int to string. complex(real,imag): converts to complex of real+imagj