C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python TutorialPython tutorial provides basic and advanced concepts of Python. Our Python tutorial is designed for beginners and professionals. Python is a simple, general purpose, high level, and object-oriented programming language. Python is an interpreted scripting language also. Guido Van Rossum is known as the founder of Python programming. Our Python tutorial includes all topics of Python Programming such as installation, control statements, Strings, Lists, Tuples, Dictionary, Modules, Exceptions, Date and Time, File I/O, Programs, etc. There are also given Python interview questions to help you better understand Python Programming. What is PythonPython 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 is easy to learn yet powerful and versatile scripting language, which makes it attractive for Application Development. 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 not intended to work in a particular area, such as web programming. That is why it is known as multipurpose programming language because it can be used with web, enterprise, 3D CAD, etc. We don't need to use data types to declare variable because it is dynamically typed so we can write a=10 to assign an integer value in an integer variable. 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 2 vs. Python 3In most of the programming languages, whenever a new version releases, it supports the features and syntax of the existing version of the language, therefore, it is easier for the projects to switch in the newer version. However, in the case of Python, the two versions Python 2 and Python 3 are very much different from each other. A list of differences between Python 2 and Python 3 are given below:
Python HistoryPython was invented by Guido van Rossum in 1991 at CWI in Netherland. The idea of Python programming language has taken from the ABC programming language or we can say that ABC is a predecessor of Python language. There is also a fact behind the choosing name Python. Guido van Rossum was a fan of the popular BBC comedy show of that time, "Monty Python's Flying Circus". So he decided to pick the name Python for his newly created programming language. Python has the vast community across the world and releases its version within the short period. Why learn Python?Python provides many useful features to the programmer. These features make it most popular and widely used language. We have listed below few-essential feature of Python.
Where is Python used?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.
Python Basic SyntaxThere is no use of curly braces or semicolon in Python programming language. It is English-like language. But Python uses the indentation to define a block of code. Indentation is nothing but adding whitespace before the statement when it is needed. For example - def func(): statement 1 statement 2 ………………… ………………… statement N In the above example, the statements that are same level to right belong to the function. Generally, we can use four whitespaces to define indentation. Python First ProgramUnlike the other programming languages, Python provides the facility to execute the code using few lines. For example - Suppose we want to print the "Hello World" program in Java; it will take three lines to print it. public class HelloWorld { public static void main(String[] args){ // Prints "Hello, World" to the terminal window. System.out.println("Hello World"); } } On the other hand, we can do this using one statement in Python. print("Hello World") Both programs will print the same result, but it takes only one statement without using a semicolon or curly braces in Python. Python Popular Frameworks and LibrariesPython has wide range of libraries and frameworks widely used in various fields such as machine learning, artificial intelligence, web applications, etc. We define some popular frameworks and libraries of Python as follows.
Python print() FunctionThe print() function displays the given object to the standard output device (screen) or to the text stream file. Unlike the other programming languages, Python print() function is most unique and versatile function. The syntax of print() function is given below. print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) Let's explain its parameters one by one.
Let's understand the following example. Example - 1: Return a valueprint("Welcome to javaTpoint.") a = 10 # Two objects are passed in print() function print("a =", a) b = a # Three objects are passed in print function print('a =', a, '= b') Output: Welcome to javaTpoint. a = 10 a = 10 = b As we can see in the above output, the multiple objects can be printed in the single print() statement. We just need to use comma (,) to separate with each other. Example - 2: Using sep and end argumenta = 10 print("a =", a, sep='dddd', end='\n\n\n') print("a =", a, sep='0', end='$$$$$') Output: a =dddd10 a =010$$$$$ In the first print() statement, we use the sep and end arguments. The given object is printed just after the sep values. The value of end parameter printed at the last of given object. As we can see that, the second print() function printed the result after the three black lines. Taking Input to the UserPython provides the input() function which is used to take input from the user. Let's understand the following example. Example - name = input("Enter a name of student:") print("The student name is: ", name) Output: Enter a name of student: Devansh The student name is: Devansh By default, the input() function takes the string input but what if we want to take other data types as an input. If we want to take input as an integer number, we need to typecast the input() function into an integer. For example - Example - a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) print(a+b) Output: Enter first number: 50 Enter second number: 100 150 We can take any type of values using input() function. Python OperatorsOperators are the symbols which perform various operations on Python objects. Python operators are the most essential to work with the Python data types. In addition, Python also provides identify membership and bitwise operators. We will learn all these operators with the suitable example in following tutorial.
Python Conditional StatementsConditional statements help us to execute a particular block for a particular condition. In this tutorial, we will learn how to use the conditional expression to execute a different block of statements. Python provides if and else keywords to set up logical conditions. The elif keyword is also used as conditional statement.
Python LoopsSometimes we may need to alter the flow of the program. The execution of a specific code may need to be repeated several numbers of times. For this purpose, the programming languages provide various types of loops capable of repeating some specific code several times. Consider the following tutorial to understand the statements in detail.
Python Data StructuresData structures are referred which can hold some data together or we say that they are used to store the data in organized way. Python provides built-in data structures such as list, tuple, dictionary, and set. We can perform complex tasks using data structures. Python ListPython list holds the ordered collection of items. We can store a sequence of items in a list. Python list is mutable which means it can be modified after its creation. The items of lists are enclosed within the square bracket [] and separated by the comma. Let's see the example of list. L1 = ["John", 102, "USA"] L2 = [1, 2, 3, 4, 5, 6] If we try to print the type of L1, L2, and L3 using type() function then it will come out to be a list. print(type(L1)) print(type(L2)) Output: <class 'list'> <class 'list'> To learn more about list, visit the following tutorial.
Python TuplePython 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. Tuple can be defined as follows Example - tup = ("Apple", "Mango" , "Orange" , "Banana") print(type(tup)) print(tup) Output: <class 'tuple'> ('Apple', 'Mango', 'Orange', 'Banana') If we try to add new to the tuple, it will throw an error. Example - tup = ("Apple", "Mango" , "Orange" , "Banana") tup[2] = "Papaya" print(tup) Output: Traceback (most recent call last): File "C:/Users/DEVANSH SHARMA/PycharmProjects/Hello/gamewithturtle.py", line 3, in The above program throws an error because tuples are immutable type. To learn more about tuple, visit the Python Tuples.
Python StringPython string is a sequence of characters. It is a collection of the characters surrounded by single quotes, double quotes, or triple quotes. It can also define as collection of the Unicode characters. We can create a string as follows. Example - # Creating string using double quotes str1 = "Hi Python" print(str1) # Creating string using single quotes str1 = 'Hi Python' print(str1) # Creating string using triple quotes str1 = '''Hi Python''' print(str1) Output: Hi Python Hi Python Hi Python Python doesn't support the character data-type. A single character written as 'p' is treated as a string of length 1. Stings are also immutable. We can't change after it is declared. To learn more about the string, visit the following tutorial.
DictionariesPython Dictionary is a most efficient data structure and used to store the large amount of data. It stores the data in the key-value pair format. Each value is stored corresponding to its key. Keys must be a unique and value can be any type such as integer, list, tuple, etc. It is a mutable type; we can reassign after its creation. Below is the example of creating dictionary in Python. Example - employee = {"Name": "John", "Age": 29, "salary":250000,"Company":"GOOGLE"} print(type(employee)) print("printing Employee data .... ") print(employee) Output: <class 'dict'> Printing Employee data .... {'Name': 'John', 'Age': 29, 'salary': 250000, 'Company': 'GOOGLE'} The empty curly braces {} are used to create empty dictionary. To learn more, visit the complete tutorial of the dictionary.
Python SetsA Python set is a collection of unordered elements. Each element in set must be unique and immutable. Sets are mutable which means we can modify anytime throughout the program. Let's understand the example of creating set in Python. Example - # Creating Set Month = {"January", "February", "March", "April", "May", "June", "July"} print(Month) print(type(Month)) Output: {'March', 'July', 'April', 'May', 'June', 'February', 'January'} <class 'set'> To get the more information about sets, visit the following resources.
Python Functional ProgrammingThis section of Python tutorial defines some important tools related to functional programming such as lambda and recursive functions. These functions are very efficient in accomplishing the complex tasks. We define a few important functions, such as reduce, map, and filter. Python provides the functools module that includes various functional programming tools. Visit the following tutorial to learn more about functional programming.
Python File I/OFiles are used to store data in a computer disk. In this tutorial, we explain the built-in file object of Python. We can open a file using Python script and perform various operations such as writing, reading, and appending. There are various ways of opening a file. We are explained with the relevant example. We will also learn to perform read/write operations on binary files.
Python ModulesPython modules are the program files that contain a Python code or functions. There are two types of module in the Python - User-define modules and built-in modules. A module that the user defines, or we can say that our Python code saved with .py extension, is treated as a user-define module. Built-in modules are predefined modules of Python. To use the functionality of the modules, we need to import them into our current working program.
Python ExceptionsAn exception can be defined as an unusual condition in a program resulting in the interruption in the flow of the program. Whenever an exception occurs, the program stops the execution, and thus the further code is not executed. Therefore, an exception is the run-time errors that are unable to handle to Python script. An exception is a Python object that represents an error.
Python CSVA csv stands for "comma separated values", which is defined as a simple file format that uses specific structuring to arrange tabular data. It stores tabular data such as spreadsheet or database in plain text and has a common format for data interchange. A csv file opens into the excel sheet, and the rows and columns data define the standard format. Visit the following tutorial to learn the CSV module in detail.
Python Sending MailWe can send or read a mail using the Python script. Python's standard library modules are useful for handling various protocols such as PoP3 and IMAP. We will learn how to send a mail with the popular email service SMTP from a Python script.
Python Magic MethodsPython magic method is defined as the special method which adds "magic" to a class. It starts and ends with double underscores, for example, _init_ or _str_. The built-in classes define many magic methods. The dir() function can be used to see the number of magic methods inherited by a class. It has two prefixes and suffix underscores in the method name.
Python Oops ConceptsEverything in Python is treated as an object including integer values, floats, functions, classes, and none. Apart from that, Python supports all oriented concepts. Below is the brief introduction of oops concepts of Python.
To read the oops concept in detail, visit the following resources.
Python Advance TopicsPython includes many advance and useful concepts that help the programmer to solve the complex tasks. These concepts are given below. Python IteratorAn iterator is simply an object that can be iterated upon. It returns one object at a time. It can be implemented using the two special methods, __iter__() and __next__(). To learn more about the iterators visit our Python Iterators tutorial. Python GeneratorsThe Generators are an easiest way of creating Iterators. To learn more about, visit our Python Generators tutorial. Python DecoratorsThese are used to modify the behavior of the function. Decorators provide the flexibility to wrap another function to expand the working of wrapped function, without permanently modifying it. To learn more about, visit the Python Decorators tutorial. Python Database ConnectionsWe can use various databases along with Python. You can learn the full tutorial to visit below resources. Python DBI-API acclaims standard sets of functionality to be included in the database connectivity modules for respective RDBMS products. We explain all important database connectivity using Python DBI-API. Python MySQLEnvironment Setup Database Connection Creating New Database Creating Tables Insert Operation Read Operation Update Operation Join Operation Performing Transactions Python MongoDBPython MongoDB Python SQLitePython SQLite Python CGIPython CGI stands for "Common Gateway Interface", which is used to define how to exchange information between the webserver and a custom Python scripts. The Common Gateway Interface is a standard for external gateway programs to interface with the server, such as HTTP Servers. To learn more about Python CGI, visit the following tutorial.
PrerequisiteBefore learning Python, you must have the basic knowledge of programming concepts. AudienceOur Python tutorial is designed to help beginners and professionals. ProblemWe assure that you will not find any problem in this Python tutorial. But if there is any mistake, please post the problem in contact form.
Next TopicFeatures of Python
|