Python Basics

Python Basics

Introduction:-

Python is a programming language that is easy to read & write. It can be used for many purposes. Python is an Open source, general-purpose, high-level, and object-oriented programming language. It is free to use and works on different platforms. Python can create web applications, software, data analysis, automation, and more. It was created by Guido van Ros

Python consists of vast libraries and various frameworks like Django, Tensorflow, Flask, Pandas, Keras etc.

Python provides a rich set of modules. We can import modules to use in our code.

Example:- import calendar, import math etc.

Modules:-

Modules in Python or in any programming language are collections of software code that have been written by someone else and that we can use in our code to utilise the features of those modules.

Modules are very useful for reusing code and adding functionality to your programs.

For example, you can use the math module to perform mathematical operations, or the random module to generate random numbers. You can also create your own modules and import them in other files.

What is the difference between a module and a package in Python?

The module is a simple Python file that contains collections of functions and global variables and has a .py extension file. It is an executable file and to organize all the modules we have the concept called Package in Python.

A module and a package are both ways of organizing Python code.

A package can have sub-packages and modules inside it, creating a hierarchical structure of namespaces.

For example, the math module is a single file that you can import and use in your code, while the xml package is a directory that contains several sub-packages and modules, such as xml.etree.ElementTree. To import a module or a package, you can use the import statement, followed by the name of the module or package. You can also use the from ... import ... statement to import specific functions, classes, or variables from a module or package.

What is a Library in Python?

The library has a collection of related functionality of codes that allows you to perform many tasks without writing your code. It is a reusable chunk of code that we can use by importing it into our program, we can just use it by importing that library and calling the method of that library with a period(.). However, it is often assumed that while a package is a collection of modules, a library is a collection of packages.

How to Install Python?

Command: apt-get install python3.6

Data Types in Python:

  1. Numbers 🔢: These include integers (like 5, 10), floating point numbers (like 3.14, 9.81), and complex numbers (like 3+4j, 5-6j).

  2. Strings 📝: These are sequences of characters. In Python, you can create a string by enclosing characters in quotes. For example, "Hello, World!".

  3. Lists 📋: A list is an ordered collection of items which can be of any type. You can create a list by placing items inside square brackets [] separated by commas. For example, [1, 2, 3] or ["apple", "banana", "cherry"].

  4. Tuples 🧩: A tuple is similar to a list but it’s immutable, which means you can’t change its content once it’s defined. You can create a tuple by placing items inside parentheses () separated by commas. For example, (1, 2, 3) or ("apple", "banana", "cherry").

  5. Dictionaries 📖: A dictionary is an unordered collection of key-value pairs. You can create a dictionary by placing items inside curly braces {} in the form key: value separated by commas. For example, { "name": "John", "age": 30 }.

  6. Sets 🧮: A set is an unordered collection of unique items. You can create a set by placing items inside curly braces {} separated by commas. For example, {1, 2, 3} or {"apple", "banana", "cherry"}.

  7. Boolean ✅❌: This type has two values: True and False.

Note:- Python is dynamically typed, which means you don’t have to declare the type of a variable when you create it.

I hope you enjoy the blog post!

If you find the blog helpful, please do like it and share it with your friends. If you have any doubts or questions related to the topics covered in the blog, feel free to ask them in the comments section. I’ll be more than happy to help!