Sunday 28 February 2021

Python’s Inbuilt Modules to Make Life Easier

Python has a number of built-in modules and packages that are included with the standard distribution. Here are some popular ones:

  1. math: A module that provides mathematical functions and constants, such as trigonometric functions, logarithms, and the constant pi.
  2. random: A module that provides various random number generators and probability distributions.
  3. os: A module that provides a way to interact with the operating system, such as navigating the file system, creating and removing directories, and executing shell commands.
  4. time: A module that provides functions for working with time and dates, such as measuring time intervals and parsing and formatting date and time strings.
  5. datetime: A module that provides classes for working with dates and times, such as date, time, datetime, timedelta, etc.
  6. json: A module for working with JSON data, it provides functions for encoding and decoding JSON data.
  7. re : A module that provides regular expression matching operations.
  8. sys: A module that provides access to some variables and functions that are used or maintained by the Python interpreter, such as the command-line arguments passed to a script.
  9. statistics: A module for mathematical statistics functions, like mean, median, variance and etc.
  10. sys: A module that provides access to system-specific parameters and functions, like interpreter version, command line arguments and etc.
  11. urllib : A module that provides an API for using the basic HTTP and FTP protocols, it also used for opening and reading URLs.
  12. argparse : A module that makes it easy to write user-friendly command-line interfaces.

These are just a few examples of the built-in modules and packages available in Python. Each of them provides a specific set of functionalities that can be used to solve different problems.

Do comment your favorite module.

Monday 1 February 2021

Python Tips and Tricks for Beginners - Part 1

Here are a few tips and tricks for Python beginners:

  1. Use list comprehensions instead of for loops: List comprehensions are a concise way to create lists and are often faster than for loops.

    They have the syntax: [expression for item in iterable if condition]. Example

    original_list = [1, 2, 3, 4, 5]
    squared_list = [x**2 for x in original_list]
    print(squared_list)
    

    output:

    [1, 4, 9, 16, 25]
    
  2. Use the "with" statement when working with files: The "with" statement is used when working with files and automatically takes care of closing the file after it's been used. This eliminates the need to explicitly call the close() method and can help prevent errors.

    Example:

    with open('example.txt', 'r') as file:
        contents = file.read()
        print(contents)
    

    Once the code inside the with block is finished executing, the file will automatically be closed. This ensures that the file is closed even if an exception is raised inside the with block. The same can be used when writing to a file.

  3. Use the built-in function enumerate(): The enumerate() function is used to loop over a list while keeping track of the index of the current item. It returns both the index and the value of the current item.

    Example:

    fruits = ['apple', 'banana', 'orange']
    for index, value in enumerate(fruits):
        print(f"{index}: {value}")
    
  4. Use the ternary operator for simple if-else statements: The ternary operator allows you to write simple if-else statements in a single line of code. The syntax is: value_if_true if condition else value_if_false.

    value_if_true if condition else value_if_false.
    

    Example

    x = 10
    y = 20
    
    bigger = x if x > y else y
    print(bigger)
    

    Output:

    20
    

    A more complex example:

    age = 20
    status = "minor" if age < 18 else "adult" if age < 65 else "senior"
    print(status)
    

    output:

    adult
    
  5. Get familiar with modules and packages: Python has a large number of modules and packages, many of which are designed to perform specific tasks. It is important to familiarize yourself with the available modules and packages and to use the ones that are appropriate for your task.

    some of the modules for example (read more Python's Inbuilt Modules for beginner)

    1. random
    2. os
    3. DateTime and time
    4. json
  6. Get into the habit of writing documentation: Good documentation is essential for any code you write. This is even more important for beginners, who are still learning the best practices for writing code.

  7. Practice and practice, write more code, and get yourself involved in open-source projects that will help you to improve your skills.

  8. Make use of Python Tutors and Mentors, who can guide and help you to overcome any issues you face while learning.

  9. Get any function doc string in python shell buy adding ? at last and press enter.

    Example:

    sum?
    

output:

Signature: sum(iterable, /, start=0)
Docstring:
Return the sum of a 'start' value (default: 0) plus an iterable of numbers

When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.
Type:      builtin_function_or_method

By implementing these tips and tricks, you can write more efficient and readable Python code, and also it will help you to improve your coding skills.

Do check out Python’s official documentation time to time. Do comment your favorite tricks.

Friday 29 January 2021

Python Beginners Resource

 Regardless of the topic, There is much information on the internet. If you’ve just started or are just about to begin your learning in python, you will find a lot of articles, courses, books, and YouTube channels.

But “what is the best for you” is a good question you should ask yourself. Because if you started following what the internet is saying, you will end up nowhere. Spending time figuring out “Functional programming vs Parallel vs Async programming” is also a waste of time.

There are many resources available for learning Python here I am putting out some of them for the absolute beginner. No prior experience in programming required a little bit of familiarity with the computer required.

1. Codecademy's "Learn Python" course: This interactive course is a great way to get started with the basics of Python. It includes exercises and quizzes to help you practice and reinforce what you've learned. Nothing to install on system. Everything on browser.

2. LearnPython.org: This website offers a variety of tutorials and exercises for learning Python, including a beginner-friendly tutorial that covers the basics of the language.

 3. https://www.python.org/'s "BeginnersGuide/Programmers" page: This page, provided by the creators of Python, provides a comprehensive guide to the basics of Python programming, as well as links to more advanced resources.

4. "Automate the Boring Stuff with Python" by Al Sweigart: This popular book is aimed at beginners who want to learn how to use Python to automate repetitive tasks. Get the book or read it free online on their website automatetheboringstuff.com/.

I would suggest to start with Codecademy's "Learn Python" course and then check out other.

Codecademy’s course give you a very basic understanding of Python. With 2 and 3 courses you will learn more about Python. In the “Automate the Boring Stuff” you will learn some of the possible things you can do with python. Checkout the 4th is must.

Along with learning python, In parallel, you can give a try to “Data Structure and Algorithm(DSA)”. It’s not like that if you don’t know the DSA, you won’t able to learn python. But trust me it will add a lot of value to your programming career. Read more about DSA's resources data-structures-and-algorithms-learning-resource.

Let us know how you starting your python journey.