Python

Python in 30 Days: Introduction


Python in 30 Days: Day 1

 

Introduction:

A high-level language for general-purpose programming is called Python. It’s an object-oriented, interpreted, free-source programming language. Guido van Rossum, a Dutch programmer, invented Python. The British sketch comedy series Month Python’s Flying Circus served as the inspiration for the name of the Python programming language. February 20, 1991, saw the release of the first edition. You can gain step-by-step knowledge of the newest Python version, Python 3, with the help of this 30-day Python challenge. The themes are divided into 30 days, with multiple topics per day that include clear explanations, examples from real-world situations, and lots of practical activities and tasks.

Why Python?

It is a programming language that is easy to learn and use since it is quite similar to human language. Many different sectors and businesses (including Google) use Python. It has been utilized in the development of machine learning libraries, desktop and web applications, and system administration. In the fields of data science and machine learning, Python is widely used. I hope this is sufficient to persuade you to begin studying Python. You are murdering Python before it eats you because it is consuming the globe.

Environment Setup

Installing Python

To run a Python script, you need to install Python. Let’s download and install Python. If you are a Windows user, click here to install the latest version of Python.

If you are a Mac user, click here to install the latest version of Python.

If Python is installed, write the below-mentioned command on your device terminal to check.

# python –version

As you can see in our terminal, it shows Python version 3.9.7, which means Python is installed.

Python Shell

Python does not require compilation because it is an interpreted scripting language. It indicates that the code is run line by line. The Python Shell (Python Interactive Shell) is included with Python. It is used to run one Python command and obtain the outcome.

The Python shell awaits the user’s Python code. It decodes the code that you enter and displays the outcome in the line that follows. Launch the command prompt (cmd) or terminal and type: python

You can now write Python code (Python scripts) by using the Python interactive shell. Next to this >>> symbol, you will type your Python script, and then press Enter. Let’s use the Python programming shell to write our first script.

You’ve successfully written your first Python script using the interactive shell in Python. The Python interactive shell can be closed in what way? Next to this symbol, type the exit() command and hit Enter to end the shell.

Now, you know how to open and exit the Python interactive shell.
Python gives you results if you write a Python-understandable script; if not, it returns an error.

The returned message, which states that we made a mistake and that the error was a syntax message: invalid syntax, is proof that Python is a very sophisticated language. Because (x) is not a viable syntax in Python, using x as a multiplication is a syntax mistake. For multiplication, we use an asterisk (*) in place of (x). The returned error makes it evident what has to be fixed.

Debugging is the process of finding and fixing flaws in software. Let’s debug it by substituting * for x.


After we addressed our bug, the code was executed and produced the desired outcome. You will encounter these kinds of mistakes daily as a coder. Being able to debug is useful. Understanding the types of faults you are encountering is essential for proficient debugging. SyntaxError, IndexError, NameError, ModuleNotFoundError, KeyError, ImportError, AttributeError, TypeError, ValueError, ZeroDivisionError, and other mistakes are some of the Python issues that you could run into. Later sections will cover more about the various forms of Python errors.

Let’s perform several fundamental mathematical operations, including division, addition, subtraction, multiplication, modulus, and exponential.

First, let’s conduct some math before writing any Python code:

  • 4 + 5 = 9
  • 5 – 4 = 1
  • 5 * 6 = 30
  • 3 / 2 = 1.5
  • 3 ^ 2 = 3 x 3 = 9

We have the following additional operations: In Python,

  • 3 % 2 = 1 => that means finding the remainder
  • 3 // 2 = 1 => that means removing the remainder

Let us convert the mathematical formulas above into a Python script. We can now put a comment at the very beginning of the Python shell since it has been opened.

A comment is a section of code that Python does not run. Thus, to make our code more legible, we might leave some words in it. Python does not execute the comment part. In Python, a comment begins with the hash(#) symbol. This is how a Python comment is written.

# comment starts with hash
# this is a python comment because it starts with a (#) symbol


Let’s continue honing our skills in the Python interactive shell before moving on to the next phase. Let’s practice writing a text on the Python shell by closing the open shell, writing exit() on it, and then opening it again.


Text editor, code editor

Visual Studio Code editor

How to install Visual Studio code

Jupyter Notebook (anaconda3)

Basic Python

Python Syntax

Python files have the.py extension, and scripts can be written in the code editor or with the Python interactive shell.

Python Indentation

A text’s white space is an indentation. Indentation in many languages is used to promote code readability, whereas Python uses indentation to form blocks of code. Instead of using indentation to form code blocks, curly brackets are utilized in various programming languages. One of the common errors when developing Python code is improper indentation.

Comments

Comments are essential for adding notes to our code and for making it easier to read. Python does not execute the code’s comment sections. In Python, a text that begins with a hash(#) is considered a comment.

Single-line comment

    # This is the first comment
    # This is the second comment
    # Python is eating the world

Multiline Comment
In the multiline comment triple quote can be used if it is not assigned to a variable

"""This is multiline comment
multiline comment takes multiple lines.
python is eating the world
"""

Data types

There are various kinds of data types in Python. Let’s begin with the most typical ones. Further sections will address various data types in more detail. Let’s just go over and become acquainted with the various data kinds for the time being.

Number

  • Integer:  -2, -1, 0, 1, 2,
  • Float: Decimal number Example …  -2.25, -1.0, 0.0, 1.1, 2.2
  • Complex Example 1 + j, 2 + 4j

String

A collection of one or more characters under a single or double quote. If a string is more than one sentence then we use a triple quote.

Example:

'Tech'
'India'
'Python'
'I love teaching'
'I hope you are enjoying the first day of python'

Booleans

A boolean data type is either a True or False value. T and F should be always uppercase.

Example:

    True  #  Is the fan on? If it is on, then the value is True
    False # Is the fan on? If it is off, then the value is False

List

Python list is an ordered collection that allows to storage of different data type items. A list is similar to an array in JavaScript.

Example:

[0, 1, 2, 3, 4, 5]  # all are the same data types - a list of numbers
['Apple', 'Orange', 'Mango', 'Avocado'] # all the same data types - a list of strings (fruits)
['England','America', 'India','Bhootan'] # all the same data types - a list of strings (countries)
['Banana', 20, False, 7.81] # different data types in the list - string, integer, boolean and float

Dictionary

A Python dictionary object is an unordered collection of data in a key-value pair format.

Example:

{
'first_name':'Tech',
'last_name':'G',
'country':'India', 
'age':25, 
'is_married':True,
'skills':['C++', '.Net', 'Node', 'Python']
}

Tuple

A tuple is an ordered collection of different data types like a list but tuples can not be modified once they are created. They are immutable.

Example:

('Tech', 'Pawl', 'Brooks', 'Abra', 'Lida') # Names
('Earth', 'Jupiter', 'Neptune', 'Mars', 'Venus', 'Saturn', 'Uranus', 'Mercury') # planets

Set

A set is a collection of data types similar to a list and tuple. Unlike a list and tuple, a set is not an ordered collection of items. Like in Mathematics, set in Python stores unique items.

In later sections, we will go into detail about every Python data type.

Example:

{2, 4, 3, 5}
{3.14, 9.81, 2.7} # order is not important in set

Checking Data types

To check the data type of certain data/variables we use the type function. In the following terminal you will see different Python data types:

Exercise: Level 1

  1. Check the Python version you are using
  2. Open the Python interactive shell and do the following operations. The operands are 3 and 4.
    • addition(+)
    • subtraction(-)
    • multiplication(*)
    • modulus(%)
    • division(/)
    • exponential(**)
    • floor division operator(//)
  3. Write strings on the Python interactive shell. The strings are the following:
    • Your name
    • Your family name
    • Your country
    • I am enjoying 30 days of python
  4. Check the data types of the following data:
    • 10
    • 9.8
    • 3.14
    • 4 – 4j
    • [‘Asabeneh’, ‘Python’, ‘Finland’]
    • Your name
    • Your family name
    • Your country

Day2>>
 

 

Tech G

View Comments

Recent Posts

Python in 30 Days: Day 30- Conclusions

Day 30 Conclusions In the process of preparing this material, I have learned quite a…

10 months ago

Python in 30 Days: Day 29 – Building an API

Day 29: Building API In this section, we will cover a RESTful API that uses HTTP…

10 months ago

Python in 30 Days: Day 28 – API

Day 28: Application Programming Interface (API) API API stands for Application Programming Interface. The kind…

10 months ago

Python in 30 Days: Day 27 – Python with MongoDB

Day 27: Python with MongoDB Python is a backend technology, and it can be connected…

10 months ago

Python in 30 Days: Day 26 – Python for web

Day 26: Python for Web Python is a general-purpose programming language, and it can be…

10 months ago

Python in 30 Days: Day 25 – Pandas

Day 25: Pandas Pandas is an open-source, high-performance, easy-to-use data structure, and data analysis tool…

10 months ago