




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































By sid
Introduction to Python and installation, data types: Int, float, Boolean, string, and list; variables, expressions, statements, precedence of operators, comments; modules, functions--- function and its use, flow of execution, parameters and arguments. UNIT II CONTROL FLOW, LOOPS Conditionals: Boolean values and operators, conditional (if), alternative (if-else), chained conditional (if-elif-else); Iteration: while, for, break, continue. UNIT III FUNCTIONS, ARRAYS Fruitful functions: return values, parameters, local and global scope, function composition, recursion; Strings: string slices, immutability, string functions and methods, string module; Python arrays, Access the Elements of an Array, array methods. UNIT IV LISTS, TUPLES, DICTIONARIES Lists: list operations, list slices, list methods, list loop, mutability, aliasing, cloning lists, list parameters, list comprehension; Tuples: tuple assignment, tuple as return value, tuple comprehension; Dictionaries: operations and methods, comprehension; UNIT V FILES, EXCEPTIONS, MODULES, PACKAGES Files and exception: text files, reading and writing files, command line arguments, errors and exceptions, handling exceptions, modules (datetime, time, OS , calendar, math module), Explore packages.
Why to use Python: The following are the primary factors to use python in day-to-day life:
1. Python is object-oriented Structure supports such concepts as polymorphism, operation overloading and multiple inheritance. 2. Indentation Indentation is one of the greatest feature in python 3. It’s free (open source) Downloading python and installing python is free and easy 4. It’s Powerful • Dynamic typing - Built-in types and tools - Library utilities - Third party utilities (e.g. Numeric, NumPy, sciPy)
Without passing python script file to the interpreter, directly execute code to Python prompt. Once you’re inside the python interpreter, then you can start. >>> print("hello world") hello world
>>> x=[0,1,2]
x #If a quantity is stored in memory, typing its name will display it. [0, 1, 2] >>> 2+ 5 The chevron at the beginning of the 1st line, i.e., the symbol >>> is a prompt the python interpreter uses to indicate that it is ready. If the programmer types 2+6, the interpreter replies
Running Python in script mode: Alternatively, programmers can store Python script source code in a file with the .py extension, and use the interpreter to execute the contents of the file. To execute the script by the interpreter, you have to tell the interpreter the name of the file. For example, if you have
a script name MyFile.py and you're working on Unix, to run the script you have to type: python MyFile.py Working with the interactive mode is better when Python programmers deal with small pieces of code as you can type and execute them immediately, but when the code is more than 2- 4 lines, using the script for coding can help to modify and use the code in future. Example: Data types: The data stored in memory can be of many types. For example, a student roll number is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard data types that are used to define the operations possible on them and the storage method for each of them. Int: Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. >>> print(24656354687654+2) 24656354687656 >>> print(20) 20 >>> print(0b10) 2 >>> print(0B10) 2 >>> print(0X20) 32
print(type(y)) print(type(z)) Output:
Boolean: Objects of Boolean type may have one of two values, True or False: >>> type(True)
>>> type(False) String:
>>> print('mrcet college') mrcet college >>> " " ' ' If you want to include either type of quote character within the string, the simplest way is to delimit the string with the other type. If a string is to contain a single quote, delimit it with double quotes and vice versa:
>>> print("mrcet is an autonomous (') college") mrcet is an autonomous (') college >>> print('mrcet is an autonomous (") college') mrcet is an autonomous (") college Suppressing Special Character: Specifying a backslash () in front of the quote character in a string “escapes” it and causes Python to suppress its usual special meaning. It is then interpreted simply as a literal single quote character: >>> print("mrcet is an autonomous (') college") mrcet is an autonomous (') college >>> print('mrcet is an autonomous (") college') mrcet is an autonomous (") college The following is a table of escape sequences which cause Python to suppress the usual special interpretation of a character in a string: >>> print('a ....b') a....b >>> print('a b\ c') abc >>> print('a \n b') a b >>> print("mrcet \n college") mrcet college Escape Sequence Usual Interpretation of Character(s) After Backslash (^) “Escaped” Interpretation ' Terminates string with single quote opening delimiter^ Literal single quote (') character
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. Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables. Rules for Python variables:
print (a) print (b) print (c) This produces the following result − 100
John Multiple Assignment: Python allows you to 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,2,"mrcet“ Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one string object with the value "john" is assigned to the variable c. Output Variables: The Python print statement is often used to output variables. Variables do not need to be declared with any particular type and can even change type after they have been set. x = 5 # x is of type int x = "mrcet " # x is now of type str print(x) Output: mrcet To combine both text and a variable, Python uses the “+” character: Example x = "awesome" print("Python is " + x) Output Python is awesome You can also use the + character to add a variable to another variable: Example x = "Python is " y = "awesome" z = x + y print(z) Output: Python is awesome Expressions: An expression is a combination of values, variables, and operators. An expression is evaluated using assignment operator.
Operator Token add + subtract - multiply * Integer Division / remainder % Binary left shift << Binary right shift >> and & or Less than <
Greater than > Less than or equal to <= Greater than or equal to >= Check equality == Check not equal != Some of the python expressions are: Generator expression: Syntax: ( compute(var) for var in iterable ) >>> x = (i for i in 'abc') #tuple comprehension >>> x at 0x033EEC30> >>> print(x) at 0x033EEC30> You might expect this to print as ('a', 'b', 'c') but it prints as at 0x02AAD710> The result of a tuple comprehension is not a tuple: it is actually a generator. The only thing that you need to know now about a generator now is that you can iterate over it, but ONLY ONCE. Conditional expression: Syntax: true_value if Condition else false_value
a = 20 b = 10 c = 15 d = 5 e = 0 e = (a + b) * c / d #( 30 * 15 ) / 5 print("Value of (a + b) * c / d is ", e) e = ((a + b) * c) / d # (30 * 15 ) / 5 print("Value of ((a + b) * c) / d is ", e) e = (a + b) * (c / d); # (30) * (15/5) print("Value of (a + b) * (c / d) is ", e) e = a + (b
Output: C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/comm.py 30 Modules: Modules: Python module can be defined as a python program file which contains a python code including python functions, class, or variables. In other words, we can say that our python code file saved with the extension (.py) is treated as the module. We may have a runnable code inside the python module. A module in Python provides us the flexibility to organize the code in a logical way. To use the functionality of one module into another, we must have to import the specific module. Syntax: import
Every module has its own functions, those can be accessed with. (dot) Note: In python we have help () Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type "quit". Some of the modules like os, date, and calendar so on…… >>> import sys >>> print(sys.version) 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] >>> print(sys.version_info) sys.version_info(major=3, minor=8, micro=0, releaselevel='final', serial=0) >>>