You are currently viewing Introduction to Python Programming

Introduction to Python Programming

What is Python?

Python is a high-level, platform-independent language invented in 1991 by Guido van Rossum. Python Programming has found its application in various fields, including web development, application development, machine learning, and artificial intelligence, because of its code readability and ease of understanding.

Why Python?

  1. We can execute python on any platform (Windows, Linux, Raspberry Pi, macOS, ubuntu, etc.)
  2. It is a fourth-generation language; hence, its code is more like English statements, which everyone can understand.
  3. For the exact logic implementation, python requires fewer lines than other programming languages like c, C++, and java.
  4. It comprises many libraries with which we can quickly implement complex ideas and concepts in just a few lines.
  5. We can use Python as a scripting language (JavaScript, PHP), procedural language (C), as well as an object-oriented language (java, C++).

Understanding with an example –

           We can see how easily we have printed the word hello without calling any function. Another noticeable feature is that we do not need a semi-colon to terminate a line; it uses a new line to execute a command.

Similarly, we can observe that in this example, no parenthesis was used for the if statement. Instead, we use a colon to define the region of the if information. Along with the colon, Python Programming also checks indentation. Here in this example, we can see that the last two lines are on the same indentation.

As of now, we have discussed the features and what makes it unique from the crowd. More we will see as we will discuss different sub-topics of python.

Version History Of Python Programming

  • Python 0.9.0 was the inaugural version which was released in February 1991.
  • Various versions  came after the regular interval to improve the functionality of python,
  • But a significant update came in December 2008 when python  3.0 was released. In this version, various flaws were removed, many new libraries were added, and finally, in 2020, python 2 became obsolete.
  • Currently, python 3.10.7 is the late version available to the users.

Installing Python Interpreter

To install the desired interpreter, we will go to https://www.python.org/downloads/ and click the Download Python 3.10.7 button, as shown in the figure below.

There are options for non-windows users on this website to download and install the required interpreter on their system.

IDEs of Python Programming

Integrated Development Environment (IDE) is an application or webpage that edit, debug, and compile our code in one place.

For Python, we have a few best-suited IDE, such as:

  • Visual Studio Code
  • PyCharm
  • Sublime
  • IDLE
  • Atom
  • Vim

We can also use online notebooks to type and run our python code. Jupyter notebook and Google Colab are two such popular notebooks.

To make our task easier, we have many websites that provide an online compiler for free that we can use in case of low system compatibility or other issues.

In this article, we will use the Jupyter notebook to run our code; you can use any other IDE per your comfort.

Python Variables

Variables are the container in which we store the data and use it further in our program.

For example, if we have to store the age of a man in our program, we will do it in the following way –

age = 30

Here ‘age’ is the name given to the variable that stores the age of the man.

name = ‘Sneha’

Here ‘name’ is the name given to the variable that stores the name Sneha.

There is a catch, we can give different names to different variables, but we have certain restrictions. To name a variable, we have to follow some rules or conventions so that our compiler does not get confused. Usually, we have certain words reserved in the memory of the compiler whose meanings are pre-defined; therefore, we cannot use those names. To overcome this, we have certain restrictions.

  • Variable names are case-sensitive in nature. E.g., name, nAme, and NAME are three names and will be considered different every time.
  • No special character is allowed except underscore (_).
  • Variable names cannot proceed with a number. E.g., 9age, and 7name are invalid variable names, whereas name1, and age3 are valid.
  • Some examples of valid variable names:  name_1, AGE, h_eight_234.
  • Some examples of invalid variable names:  name-1, 4AGE, h_eight_&4.

You May Also Like to Read About: List of keywords in Python with examples in detail

Python Data Types

We can store different data types in other variables depending on their type. So, the question arises what exactly are the data types of a variable? Let’s discuss data types.

The data we store in a variable also has a kind, which makes one type of data different from another. Some commonly used data types are integer, float, string, Boolean, etc.

We will see some examples:

  • Integer – 2,3, -9, -8, 10
  • Float – 2.3, 9.6, 5.6248
  • String – ‘hello,’ ‘A,’ ‘Good morning!’
  • Boolean – True and False

Here comes another benefit of the Data Science with Python Programming language. Unlike other programming languages, we need not mention the variable’s data type while declaring the variable. We just have to write the variable’s name and assign the data. Python itself does the rest of the job.

Examples:

username = ‘Raghu’

user_age = 30

user_height = 5.11

Operators in Python

Operators are symbols with a unique meaning and carry out a specific operation on the operands.

Example:

p + n

Here m and n are operands, and + is the operator. + operator adds the data of two operands.

Types of Operators

  • Arithmetic operators
OperatorNameExample
+           Addition        P + n
SubtractionP – n
*Multiplicationp * n
/Divisionp / n
%Modulusp % n
**Exponentiationp** n
//Floor divisionp// n
  • Assignment operators
OperatorExampleSimilar to
=p= 4p= 4
+=p+= 4p= p+ 4
-=p-= 4p= p– 4
*=p*= 4p= p* 4
/=p/= 4p= p/ 4
%=p%= 4p= p% 4
//=p//= 4p= p// 4
**=p**= 4p= p** 4
&=p&= 4p= p& 4
|=p|= 4p= p| 4
^=p^= 4p= p^ 4
>>=p>>= 4p= p>> 4
<<=p<<= 4p= p<< 4
  • Logical operators
OperatorDescriptionExample
and Returns True if both statements are truep< 4 and p< 9
orReturns True if one of the statements are truep< 5 or p< 4
notReverse the result, returns False if the result is truenot (p < 5 and p < 10)
  • Comparison operators
OperatorNameExample
==Equalp == n
!=Not equal p != n
Greater thanp > n
Less thanp < n
>=Greater than or equal toP >= n
<=Less than or equal toP <= n
  •  
NameOperatorExample
&ANDp & n
|ORp | n
<< Zero fill left shift.p << n  
~NOT  p ~ n  
^XORP ^ n
>> Signed right shiftp >> n

Data Structure

Array

An array is a collection of a homogeneous data type where we can store more than one data in a contiguous manner. Python does have any built-in array data structure. We can use List in python instead.

Lists

It is an ordered collection where we can store multiple data of identical or different data types (heterogeneous data) in one place. It is one of the handiest data structures of python. We can declare a list simply by using []. The list follows an indexing system like an array. Here also, the indexing starts from zero (0).

Example:

Student_details = [‘Suman’, 15, ‘Boy’]

  • We can also store a child list inside a parent list.

Example:

Demo= [15, [14,13]]

  • We can traverse through the list easily using the index number.

   Example:

list_example = [13,5,66,17]

print (list_example[1])

Output:

5

  • Append feature is used to add new value to the existing list

Example:

ls1 = [‘riya’, ‘rohan’]

ls1.append(‘shyam’)

print(ls1)

output:

[‘riya’, ‘rohan’, ‘shyam’]

  • We can use the len() function to find the length of the list.

thelist = [‘apple’, ‘banana’, ‘cherry’]

print(len(thelist))

output:

3

Tuples –

Tuples are much like lists. But unlike a list it is immutable; the value, once assigned, cannot be changed.

  • Tuples are written with round brackets.

Example:

Student_details = (‘Suman’, 15, ‘Boy’)

  • An example where tuples are helpful is when your program needs to store the names of the days of a week or some fixed-ordered list.
  • We can access the individual values of a tuple using their indexes, just like with a list.

 Example:

tuple_example = (13,5,66,17)

print (tuple_example[1])

Output:

5

  • We can use the len() function to find the length of the tuple

thetuple = [‘apple’, ‘banana’, ‘cherry’]

print(len(thetuple))

output:

3

Dictionary

Dictionary is an ordered collection of similar data types in pairs or key: value form.

  • They are mutable but do not allow duplicates.
  • They are written with curly braces.

Example:

mydict = {“Peter”:28, “Jenny”:21, “Tom”:15}

  • len() function here also returns the length of the dictionary:

Example:

mydict = {“Peter”:28, “Jenny”:21, “Tom”:15}

print(len(mydict))

Output:

3

  • we can access items in a dictionary by referring to its key name inside square brackets:

Example:

thedict = {

  “brand”: “Suzuki”,

  “model”: “Alto”,

  “year”: 2000

}

x = thedict[“model”]

Output:

Alto

  • get() that will also give us the same result.

Example:

thedict = {

  “brand”: “Suzuki”,

  “model”: “Alto”,

  “year”: 2000

}

x = thedict.get(“model”)

Output:

Alto

In Python version 3.7, dictionaries are ordered. In earlier versions, dictionaries were unordered.

Set –

Set is a collection of unordered, different values separated by commas within curly braces.

Example:

Myset={1,2,3}

  • In Python Programming, sets are unordered; that is, they do not have any fixed order, and we cannot receive them using index numbers.
  • Once declared, we cannot change the settings, but we can add and remove items. Therefore, it is called unchangeable or immutable.
  • Sets in python do not allow duplicate values.
  • Here, the len() function is also used to give the set’s length.
  • Like tuples, list it also stores heterogeneous data types 

You May Also Like to Raed About : Data Science with Python

This Post Has 10 Comments

  1. Divanshu

    Great insight

  2. Ekta Kumari

    Amazing explanation 💫

  3. CA

    Really great Insights

  4. CA

    The website’s content is clearly written by professionals in their respective fields, who have taken the time to thoroughly research and present their ideas in a concise and understandable manner.
    Furthermore, the website’s design and layout are visually appealing and intuitive, making it a pleasure to explore and interact with. The attention to detail and dedication to providing a seamless user experience is evident throughout the website, demonstrating a high level of professionalism and expertise.

  5. Karthik

    This particular session by Brainalyst is worth the time spend. It helped me in developing valuable insights.

    1. Vivek Maurya

      That was really great content. Thanks for sharing this insightful and informative content.

  6. Bhupendra Singh

    Great information!

  7. Bhupendra Singh

    Great!

  8. Dikshita

    Insightful

  9. Kartikey

    I think this is pretty much all I needed to know about it! Wonderful article!

Leave a Reply