Python




 

Python Author: Guido van Rossum

Python is a general purpose, dynamic, high-level and interpreted programming language. It supports Object Oriented programming approach to develop applications. It is simple and easy to learn and provides lots of high-level data structures.

Python makes the development and debugging fast because there is no compilation step included in Python development, and edit-test-debug cycle is very fast.

Python's syntax and dynamic typing with its interpreted nature make it an ideal language for scripting and rapid application development.Python supports multiple programming pattern, including object-oriented, imperative, and functional or procedural programming styles.

Python is a general-purpose, popular programming language and it is used in almost every technical field. The various areas of Python use are given below.

  • Data Science
  • Date Mining
  • Desktop Applications
  • Console-based Applications
  • Mobile Applications
  • Software Development
  • Artificial Intelligence
  • Web Applications
  • Enterprise Applications
  • Machine Learning
  • Computer Vision or Image Processing Applications.
  • Speech Recognitions

Features Of Python

(1) Easy

  • Python is easy to learn as compared to other programming languages. Its syntax is easy . There is no use of the semicolon or curly-bracket, the indentation defines the code block. It is the recommended programming language for beginners.

(2) Expressive

  • Python can perform complex tasks using a few lines of code. A simple example, the hello world program you simply type print("Hello World"). It will take only one line to execute, while Java or C takes multiple lines.

(3) Free and Open Source

  • Python is freely available for everyone. It is freely available on its official website www.python.org.It has a large  modules and functions. Anyone can contribute to the Python community. The open-source means, "Anyone can download its source code without paying it."

(4) High Level

  • Python is an interpreted high-level general-purpose programming language. Its design code readability with its use of significant indentation. Its  help programmers write clear, logical code for small and large-scale projects.

(5) Portable

  • Python is portable means  it runs on many Unix variants including Linux and macOS, and on Windows.

(6) Object Oriented

  • Python supports object-oriented language and concepts of classes and objects. It supports inheritance, polymorphism, and encapsulation, etc. The object-oriented procedure helps to programmer to write reusable code and develop applications in less code.

(7) Extensible

  • It implies that other languages such as C/C++ can be used to compile the code and thus it can be used further in our Python code. It converts the program into byte code, and any platform can use that byte code.

(8) Embeddable

  • The code of the other programming language can use in the Python source code. We can use Python source code in another programming language as well. It can embed other language into our code.

(9) Interpreted

  • Python is an interpreted language; it means the Python program is executed one line at a time. The advantage of being interpreted language, it makes debugging easy and portable.

(10) Large Standard Library

  • It provides a vast range of libraries for the various fields such as machine learning, web developer, and also for the scripting. There are various machine learning libraries, such as Tensor flow, Pandas, Numpy, Keras, and Pytorch, etc. Django, flask, pyramids are the popular framework for Python web development.

(11) GUI Programming

  • Graphical User Interface is used for the developing Desktop application. PyQT5, Tkinter, Kivy are the libraries which are used for developing the web application.

(12) Dynamically Typed

  • In Python, we don't need to specify the data-type of the variable. When we assign some value to the variable, it automatically allocates the memory to the variable at run time. Suppose we are assigned integer value 20 to x, then we don't need to write int x = 20. Just write x = 20.

Example of Python program

name = "Jignesh Patel"  
                branch = "Information Technology"  
                age = "22"  
                print("My name is: ", name, )  
                print("My age is: ", age)  

print :here print is used to display the messeage.

Python List

A list in Python is used to store the sequence of various types of data. Python lists are mutable type its mean we can modify its element after it created. However, Python consists of six data-types t, but the most common and reliable type is the list.

A list can be defined as a collection of values or items of different types. The items in the list are separated with the comma (,) and enclosed with the square brackets [].

A list can be define as below
L1 = ["Jignesh patel", 91, "India"]    
L2 = [1, 2, 3, 4, 5, 6]

Python List Built-in Functions

(1) cmp(list1, list2)
It compares the elements of both the lists.
This method is not used in the Python 3 and the above versions.

(2) len(list)
It is used to calculate the length of the list.
L1 = [1,2,3,4,5,6,7,8]
print(len(L1))
Output-8

(3) max(list)
It returns the maximum element of the list.
L1 = [12,34,26,48,72]
print(max(L1))
Output-72

(4) min(list)
It returns the minimum element of the list.
L1 = [12,34,26,48,72]
print(min(L1))
Output-12

(5) min(seq)
It converts any sequence to the list.
str = "Python"
s = list(str)
print(type(s))
Output-<class list>

Example:- Write the program to remove the duplicate element of the list.
list1 = [1,2,2,3,55,98,65,65,13,29]  
list2 = []  
for i in list1:  
    if i not in list2:  
        list2.append(i)  
print(list2)  
Output:[1, 2, 3, 55, 98, 65, 13, 29] 

Python Tuple

Python Tuple is used to store the sequence of immutable Python objects. The tuple is similar to lists since the value of the items stored in the list can be changed, whereas the tuple is immutable, and the value of the items stored in the tuple cannot be changed.

A tuple can be written as the collection of comma-separated (,) values enclosed with the small () brackets. The parentheses are optional but it is good practice to use. A tuple can be defined as follows.

A tuple can be defined as below
T1 = (101, "Peter", 22)    
T2 = ("Apple", "Banana", "Orange")     
T3 = 10,20,30,40,50  
print(type(T1))  
print(type(T2))  
print(type(T3))  

The indexing and slicing in the tuple are similar to lists. The indexing in the tuple starts from 0 and goes to length(tuple) - 1.

The items in the tuple can be accessed by using the index [] operator. Python also allows us to use the colon operator to access multiple items in the tuple.

Characteristics of Tuple

  • Tuples are defined in the same way as lists.
  • They are enclosed within parenthesis and not within square braces.
  • Elements of the tuple must have a defined order.
  • Negative indices are counted from the end of the tuple, just like lists.
  • Tuple also has the same structure where commas separate the values..

Python Tuple Built-in Functions

(1) cmp(tuple1, tuple2)
It compares two tuples and returns true if tuple1 is greater than tuple2 otherwise false.

(2) len(tuple)
It calculates the length of the tuple.
T1 = (1,2,3,4,5,6,7,8)
print(len(T1))
Output-8

(3) max(tuple)
It returns the maximum element of the tuple.
T1 = (12,34,26,48,72)
print(max(T1))
Output-72

(4) min(tuple)
It returns the minimum element of the tuple.
T1 = (12,34,26,48,72)
print(min(T1))
Output-12

(5) min(tuple)
It converts any sequence to the tuple.
str = "Python"
s = tuple(str)
print(type(s))
Output - <class tuple>

List vs Tuple

ListTuple
The literal syntax of list is shown by the [].The literal syntax of the tuple is shown by the ().
The List is mutable.The tuple is immutable.
The List has the a variable length.The tuple has the fixed length.
The list provides more functionality than a tuple.The tuple provides less functionality than the list
The list is used in the scenario in which we need to store the simple collections with no constraints where the value of the items can be changed.The tuple is used in the cases where we need to store the read-only collections i.e., the value of the items cannot be changed. It can be used as the key inside the dictionary
The lists are less memory efficient than a tuple.The tuples are more memory efficient because of its immutability.


Python Function


Functions are the most important aspect of an application. A function can be defined as the organized block of reusable code, which can be called whenever required.

Python allows us to divide a large program into the basic building blocks known as a function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the Python program.

The Function helps to programmer to break the program into the smaller part. It organizes the code very effectively and avoids the repetition of the code. As the program grows, function makes the program more organized.

Python provide us various inbuilt functions like range() or print(). Although, the user can create its functions, which can be called user-defined functions.

There are mainly two types of functions.
User-define functions - The user-defined functions are those define by the user to perform the specific task.
Built-in functions - The built-in functions are those functions that are pre-defined in Python.


Advantages Of Function

  • Using functions, we can avoid rewriting the same logic/code again and again in a program.
  • We can call Python functions multiple times in a program and anywhere in a program.
  • We can track a large Python program easily when it is divided into multiple functions.
  • Reusability is the main achievement of Python functions.
  • However, Function calling is always overhead in a Python program.


Python provides the def keyword to define the function. The syntax of the define function is given below.

Syntax:
def my_function(parameters):  
      function_block  
return expression  

Let's understand the syntax of functions definition.
  • The def keyword, along with the function name is used to define the function.
  • The identifier rule must follow the function name.
  • A function accepts the parameter (argument), and they can be optional.
  • The function block is started with the colon (:), and block statements must be at the same indentation.
  • The return statement is used to return the value. A function can have only one return



Comments

Popular posts from this blog

C#

JAVA