What is python?
Python is a programming language. It can be used on a server to create web applications and it is an interpreted, high-level and general-purpose programming language. Python was devoloped by Guido van Rossum in 1990. It is suitable for both freshers who are new to programming and those who have good knowledge as well as experience in programming.
Features of python:
1. Easy to code:
2. Free and Open Source:
Python language is freely available on the official website and you can download it from that website without any cost.
Since it is open-source, this means that source code is also available to the public
3. Object-Oriented Language:
One of the key features of python is Object-Oriented programming. Python supports object-oriented language and concepts of classes, objects encapsulation, etc.
4. GUI Programming Support:
Graphical User interfaces can be made using a module such as PyQt5, PyQt4, wxPython, or Tk in python.
PyQt5 is the most popular option for creating graphical apps with Python.
5. High-Level Language:
Python is a high-level language. When we write programs in python, we do not need to remember the system architecture, nor do we need to manage the memory.
6. Extensible feature:
Python is an Extensible language. We can write some Python code into C or C++ language and also we can compile that code in C/C++ language.
7. Python is a Portable language:
Python language is also a portable language. For example, if we have python code for windows and if we want to run this code on other platforms such as Linux, Unix, and Mac then we do not need to change it, we can run this code on any platform.
8. Python is an Integrated language:
Python is also an Integrated language because we can easily integrated python with other languages like c, c++, etc.
9. Interpreted Language:
Python is an Interpreted Language because Python code is executed line by line at a time. like other languages C, C++, Java, etc. there is no need to compile python code this makes it easier to debug our code. The source code of python is converted into an immediate form called bytecode.
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:
Python is a dynamically typed language. That 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
Interpretation in python:
Python is neither compiler nor an Interpreter language. It compiles the code line by line and if there are any errors during the compilation it gives the output until the line which contains that error. The python code you write is compiled into python bytecode, which creates a file with extension .pyc and the .pyc file, created in the compilation step, is then executed by appropriate virtual machines.
Python — Variables:
Variables are reserved storage in the memory. when you declare a variable, you reserve some space for it in the memory. It may vary according to the data type of the variable.
Rules for creating the variable:
There are certain rules while creating/declaring a variable, which is mentioned below:
- A variable name must start with a letter or the underscore character.
- A variable name cannot start with a number.
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0–9, and _ ).
Creating a variable is a very easy task in python all you have to do is to create a variable and assign a value to it and they do not need to be declared with any particular type we can even change their type after they set.
y=’hello’ → while declaring strings you have to put it between single or double-quotes.
you can also create multiple variables with many values or assign a single value for multiple variables.
You can get the type of the variable using an in build function in python called type().
As we previously saw we can change the type of the variable irrespective of its declared type using some functions like int(), str(), float().
Python id() function returns an identity of an object. Two variables with the same value return the same id. (only if they are immutable).
Immutable value: Mutable value:
Python — Strings :
Strings in python are nothing but immutable arrays. They can be surrounded by either single or double quotation marks.
a = ‘hello’ or a = “hello”
Length of a string:
You can find the length of the string using a simple function called len().
a = ‘hello’
len(a) #returns the length of the string
As basically string is an array its indexing starts from zero and square brackets can be used to access the elements of the string.
You can return a specific range of characters by using the slice syntax. mention the start index and the end index, separated by a colon, to return a part of the string [start: end].
In string, you can also use negative indexing to get the end or reverse order elements.
It is a technique of combining two strings you can either use + operator or join function to perform string concatenation.
As we previously saw strings are immutable, we cannot assign or change any
characters of a string once it declared.