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.

Friday, 1 January 2021

Data Structures and Algorithms: Learning Resources

 There are many resources available to learn Data Structures and Algorithms (DSA), including:

  1. "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein: This is a classic textbook that covers a wide range of DSA topics, including sorting algorithms, data structures, and algorithm design techniques.
  2. "Algorithms, Part I" and "Algorithms, Part II" on Coursera: These online courses, taught by Robert Sedgewick and Kevin Wayne of Princeton University, cover a wide range of DSA topics and include video lectures, quizzes, and programming assignments.
  3. "Data Structures and Algorithms" on Udemy: This online course covers a wide range of DSA topics and includes video lectures, quizzes, and coding exercises.
    1.  Master the coding interview data structures Algorithms 
    2. Data structure and Algorithm
  1. "Data Structures and Algorithms" on HackerRank/LeetCode: This website provides a wide range of coding challenges and tutorials on DSA topics, with a focus on practical implementation.
  2. "The Algorithm Design Manual" by Steven Skiena: This book provides an in-depth look at algorithm design techniques and strategies.
  3. "Algorithms" by Robert Sedgewick and Kevin Wayne, on the book website or on the publisher's website, it includes the full content of the book, as well as additional materials, such as lecture slides and exercises.
  4. "Algorithms and Data structures" from OpenCourseWare from MIT: This resource provides free online access to a wide range of DSA materials, including lecture notes, assignments, and exams.

Some of the courses are not in the language you want to learn, but I would suggest giving it a try in what language the instructor is saying to get out the most from the course.

These resources are widely used and recommended, but you should pick the one that suits you best, based on your learning style, schedule, and budget.

Do Solve DSA's Problems on HackerRank/LeetCode. 

Let us know in comment section about your favorite course/book or even any question.