Python was developed by Guido van Rossum in early 1990’s and its latest version is 3.6, we can simply call it as Python3. Python 3.0 was released in 2008.
In python3 t print any line we use 'print("text")'.
n other programming languages like C, C++ and Java, you will need to declare the type of variables but in Python you don’t need to do that. Just type in the variable and when values will be given to it, then it will automatically know whether the value given would be a int, float or char or even a String. There are two type of variable: - Local - Global.
Local variable: If we create a variavble inside a function i.e. local variable, we can only access local variable inside the function.
Global variable: If we are using same value multiple time in different-different function then it recommanded to make variable global because in this case less memory consumed and performance increses.
List is the most basic Data Structure in python. List is mutable data structure it means items can be added to list later after the list creation. Its like you are going to shop at local market and made a list of some items and later on you can add more and more items to the list.append() function is used to add data to the list.
input() function is used to take input from the user. print() functiom is used to give output.
In python selection is made using 'if' and 'elif'(here we use elif on the place of elseif).
You can think of functions like a bunch of code that is intended to do a particular task in the whole Python script. Python used the keyword ‘def’ to define a function. Advantage of function is Reusability.
Types of function : - In-Built - User define
User define: We use syntex "def" ex:
def function_name(parameters):
_______
_______
return()
For loops can iterate over a sequence of numbers using the "range" and "xrange" functions. The difference between range and xrange is that the range function returns a new list with numbers of that specified range, whereas xrange returns an iterator, which is more efficient.
While loop repeat as long as a certain bollean condition is met.The while loop is to perform iteration. A while statement repeatedly execute a single statement untill a true value occure.
while test:
code statement
else:
final code statements
You’ll store values in variables with an assignment statement. An assignment statement consists of a variable name, an equal sign (called the assignment operator), and the value to be stored.
break is used to exit a for loop or a while loop, whereas continue is used to skip the current block, and return to the "for" or "while" statement.
Python has a very rich module library that has several functions to do many tasks. 'import’ keyword is used to import a particular module into your python code.
Operator Operation Example
** Exponent 2 ** 2 = 8
% Modules/remainder 22 % 8= 6
// Integer division/floored quotient 22 //8 = 2
/ Division 22 / 8 = 2.75
* Multiplication 2 * 5 = 10
- Subtraction 5 - 3 = 2
+ Addition 7 + 3 = 10
If the condition "condition_1" is True, the statements in the block statement_block_1 will be executed. If not, condition_2 will be executed. If condition_2 evaluates to True, statement_block_2 will be executed, if condition_2 is False, the statements in statement_block_3 will be executed.
if condition_1:
statement_block_1
elif condition_2:
statement_block_2
else:
statement_block_3
range() allow us to create a list of numbers ranging from a starting point up to an ending point . We can also specifi step size.
In CLA we import "sys" library and "argv" class. Whatever argument we are passing from cmd are CLA. CLA will be availabel in argv(list) variable. argv is not an array it is list object. First element of argv is always Name of file. argv variable is present in sys module so import sys first. Initially all CLA will be available is string, and give input like : test.py 10 20 30. Double quote input affects the output with quote it will be one input otherwise it will be fit in defferent index like: test.py Note_book and test.py "Note_book".
from sys import argv
print(argv)
for i in argv:
print(i)
These operator will allow us to compare variables and output a boolean value.
An interestion feature of Pyton is the ability to chain multiple comperision to perform a more complex test. You can use these chained compariso as a shorthand for large boolean expression.
Methods are essentially fuction built into objects. Methods will perform specifi actions on the object and can also take arguments, just like a fuction.
object.methods(arg1,arg2,etc....)
- Append
- Count
- Extend
- insert
- pop
- remove
- reverse
- sort
Lambda fuction allow us to create "anonymos" fuctions.This basically mean we can quickly make ad-hoc fuction without needing to properly define a fuction using def. Fuction object return by running lambda expression work exectely the same as those created and assigned by defs.
<function__main__.<lambda>>
Lambda expression really shine when used in conjuction with map(),filter() and redce().
When you create a veriable name in Python the name is store in namespace. Veriable name also have a scope, the scope determines the visbility of that veriable name to other parts of your code. Name assignment will create and change local name by default.Name references at most four scope:
- Local
- Enclosing fuction
- Global
- Built-in
Name assigned in any way in fuction, and not declared in global in that function.
Name in the local scope of any amd all enclosing fuction, from inner to outer.
Name assigned at the top-level of a module file, or declared global in a def within the file.
Name preassigned in the built-in name module : open, range, syntex....
This import a module into your current work-place. It's important that you have to either tell Python what function you want to import, or state the module you are importing from. To download newmodule:
for anaconda
conda install *module name*
pip install *module name*(optional)
Syntax errors, also known as parsing errors.The parser repeats the offending line and displays a little ‘arrow’ pointing at the earliest point in the line where the error was detected. The error is caused by (or at least detected at) the token preceding the arrow, the error could be detected at the function print(), since a colon (':') is missing before it and etc. File name and line number are printed so you know where to look in case the input came from a script.
If a statement or expressions is syntectically correct, it may cause an error when an attempt is made to execute it. Error detected during execution is called Exceptions.
Try is used in Error and Exceptional handling. The basic terminology and syntex used to handle the error in Python is the try and except statements.
try:
you do your options here...
...
except ExceptionI:
If there is ExceptionI, then execute this block
except ExceptionII:
If there is ExceptionII, then execute this block
...
else:
If there is no exception then execute this block.
Object Oriented Programming(OOP) tends to be one of the major obstacles for beginner when they first starting to learn Python. OOP in Python by building on the following topics:
-
Objects
-
Using the Class keyword
-
Creating calss attributes
-
Creating method in class
-
learning about inheritances
-
Object In Python everything is Object.
-
Class The user defined objects are created using the class keyword. The class is a blueprint that defines a nature of a future object. From classes we can construct instances. An instance is a specific object created from a perticular classes.
-
Attribute An attribute is characteristic of an object. A method is an operation we can perform with the object. The syntex for creating Atribute is:
self.attribute = something
- Methods Methods are function defined inside the body of the class.They are used to perform operations with the attribute of our objects. Methods are essential in encapsulationconcept of OOP paragidm. This is essential in dividing reposibilities in programming, specially in large applications.
- Inheritance Inheritance is a way to form using new classes using classes that have already been defined. A language feature would not be worthy of the name “class” without supporting inheritance. The newly formed classes are called drived classes, the classes that we drive from are called base classes. Important benefits of inheritance are code reuse and reduction of complexcity of a program.
- Special Methods Classes in Python can implement certain operations with special method names. These methods are not actually called direct but Python specific lanuage syntac.