Python Revision Day-1

Nitesh PatelNitesh Patel
6 min read

Python :

Python is a high-level, interpreted programming language. It was created in the late 1980s by Guido van Rossum and was designed to be easy to read and write, and to be highly extensible.

Python is a general-purpose language, which means it can be used to build almost any type of software, from web applications to desktop applications to scientific applications and more.

Python is known for its simplicity and ease of use, and it has a large and active community of users and developers. It is also open source, which means that the source code is available to the public and can be freely used and modified.

Python is often used as a scripting language, but it is also used to build standalone applications. It has a large standard library, which means that it comes with a lot of pre-built functionality, and it also has a large ecosystem of third-party libraries and frameworks that can be used to build more complex applications.

Why Python??

There are many reasons why Python is a popular programming language:

  1. It is easy to learn and use: Python has a simple and readable syntax, which makes it a great language for beginners.

  2. It is versatile: Python can be used for a wide range of applications, including web development, scientific computing, data analysis, and artificial intelligence.

  3. It has a large and active community: Python has a large and active community of users and developers, which means that there is a lot of support and resources available online.

  4. It has a large standard library: Python comes with a large standard library that includes many useful modules and functions, which means that you can do a lot of things without having to install third-party libraries.

  5. It has a large ecosystem of third-party libraries and frameworks: There are many third-party libraries and frameworks available for Python that you can use to build more complex applications.

Overall, Python is a powerful, easy-to-use, and versatile programming language that is well-suited for many different types of projects.

Basics to keep in mind:

  1. Python Shell: Python is an interpreted scripting language, so it does not need to be compiled. It means it executes the code line by line. Python comes with a Python Shell (Python Interactive Shell). It is used to execute a single python command and get the result.

    Python Shell waits for the Python code from the user. When you enter the code, it interprets the code and shows the result in the next line.

  2. Indentation: In Python, indentation is used to indicate block structure. Unlike some other languages, Python does not use curly braces { } to enclose blocks of code; instead, it uses indentation.

if x < 0:
    print("x is negative")
    x = 0
else:
    print("x is non-negative")

In this code, the lines print("x is negative") and x = 0 are indented, which indicates that they are both parts of the block of code that is executed if x < 0. Similarly, the line print("x is non-negative") is not indented, which indicates that it is not part of that block of code.

In Python, it is important to use the correct indentation, because the indentation determines the structure of the code. Indentation is usually four spaces, but it can also be a tab. It is important to be consistent in your use of indentation, either using only spaces or only tabs.

Overall, Python's use of indentation to indicate block structure helps to make the code more readable and easy to understand.

  1. Comment: A comment is a part of the code which is not executed by python. So we can leave some text in our code to make our code more readable. Python does not run the comment part. A comment in python starts with hash (#) symbol. This is how you write a comment in python
 # comment starts with hash
 # this is a python comment, because it starts with a (#) symbol

Data Types:

In Python, there are several different types of data that you can use in your programs. Some of the most common data types include:

  1. Numbers: Python supports several types of numbers, including integers (e.g. 1, 2, 3), floating-point numbers (e.g. 3.14, 1.23), and complex numbers (e.g. 3 + 4j).

  2. Strings: Strings are sequences of characters, and are used to represent text in Python. They can be enclosed in single or double quotes, and can be concatenated and repeated using the + and * operators.

     'Namaste'
     'Python'
    
  3. Lists: Lists are ordered sequences of items. They are similar to arrays in other languages, but can contain items of different types. Lists are enclosed in square brackets ([]), and the items are separated by commas.

     [0, 1, 2, 3, 4, 5]  # all are the same data types - a list of numbers
     ['Banana', 'Orange', 'Mango', 'Avocado'] # all the same data types - a list of strings (fruits)
     ['Banana', 10, False, 9.81] # different data types in the list - string, integer, boolean and float
    
  4. Tuples: Tuples are similar to lists, but they are immutable, meaning that once they are created, their items cannot be modified. Tuples are also enclosed in parentheses, but their items are separated by commas.

     ('Earth', 'Jupiter', 'Neptune', 'Mars', 'Venus', 'Saturn', 'Uranus', 'Mercury') # planets
    
  5. Dictionaries: Dictionaries are unordered collections of items, where each item is a key-value pair. They are enclosed in curly braces ({}), and the items are separated by commas.

     {
     'first_name':'John',
     'last_name':'Cena'
     }
    
  6. Sets : Sets are unordered collections of unique items, similar to dictionaries but keys should be unique.

     {2, 4, 3, 5}
     {3.14, 9.81, 2.7} # order is not important in set
    
  7. Booleans: Booleans represent the logical values of true and false.

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

These are the most common data types you'll use in Python, but there are many other data types available in the language and libraries such as Decimal , Fractions, datetime and many more. It's important to understand the different data types that Python provides and when to use them, in order to write efficient and readable code.

Checking Data Types:

The type() function in Python is used to determine the type of an object. When you pass a string as an argument to the type() function, it will return the type str, which stands for "string".

print(type(10))          # Int
print(type(3.14))        # Float
print(type(1 + 3j))      # Complex number
print(type('John'))  # String
print(type([1, 2, 3]))   # List
print(type({'name':'John'})) # Dictionary
print(type({9.8, 3.14, 2.7}))    # Set
print(type((9.8, 3.14, 2.7)))    # Tuple

If you have any suggestion or feedback, do comment :)

0
Subscribe to my newsletter

Read articles from Nitesh Patel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nitesh Patel
Nitesh Patel