Karim Aya

Karim Aya

1566746392

5 Python Tips for Beginners

In this article, we discuss six tips for beginners just getting started with Python, including list comprehension, *args and **kwargs, lambda …

Python is one of the easiest programming languages to learn. The syntax is close to English. Beginners generally encounter only a few surprises, such as forced indentation and the use of self in methods.

At some point, everyone starts reading, copying, and editing other people’s code. That’s where the confusion starts.

I’ll explain five Python concepts that will help beginners to modify code written by other authors. These tips were compiled by observing problems novice Python developers encountered in workshop-style training as well as analyzing online discussions that took place in a free and open development communityfocused on the usage of APIs, image processing, and metadata (text) processing for the RICOH THETA camera. The typical person has intermediate programming experience with Java, C, JavaScript, or bash, but is still a Python novice.

They can write their own Python code to get a job done, but have problems reading the code contributed by other people.

Here are five things that helped some of these people better understand Python.

1. *args and **kwargs Are Function Arguments

If you look at Python modules or even documentation for the module, you’re likely to see *args and **kwargs. They look and act vaguely like a pointer in C. They are not. *args is simply a list of arguments sent to a function.

**kwargs is a dictionary of keyword arguments.

<strong>*args</strong>** example:**

def add_it(*args):
for num in args:
print(num)
add_it(3, 4, 5, 6)

Output:

$ python dzone.py
3
4
5
6

<strong>**kwargs</strong>** example:**

def fish_counter(**kwargs):
print(kwargs)

fish_counter(salmon=10, trout=30, smelt=10, bluegill=52)

Output:

$ python dzone.py 
{'bluegill': 52, 'smelt': 10, 'salmon': 10, 'trout': 30}

2. List Comprehensions Are For Loop Shortcuts

list compressions are a short way to return a list. In the snippet, the for loop is for number in args.

The expression that is normally inside the loop is num **2 , which returns the square of the arguments.

def square_it(*args):
    return [num **2 for num in args]

print(square_it(3, 4, 5, 6))

Output:

$ python dzone.py 
[9, 16, 25, 36]

You can tack on a filter to the end of the list comprehension to filter out inputs. For example, to only square the even numbers, filter it with this:

def square_it(*args):
    return [num **2 for num in args if num %2 == 0]

print(square_it(3, 4, 5, 6))

Output:

$ python dzone.py 
[16, 36]

The list comprehension does not add any special feature over the for loop. People use list comprehensions because they are shorter and can make code easier to read once you get accustomed to the syntax. Some people may overuse list comprehensions and make code more difficult to read. Be careful of this, as convoluted a list comprehension with multiple nesting is not best practice.

If you’re just getting started with Python and see a for loop all in one line, you can search on the Internet for list comprehension and review the syntax of the three components:

  1. Expression.
  2. For loop.
  3. Filter.

3. F Strings Can Replace .format()

Python is wonderful for string manipulation. You’ll probably see at least three techniques to insert variables into strings. Two are clunky. One is cool.

String Concatenation

Long ago, you may have inserted variables into a string with code similar to this:

animal = "dogs"
population = 3
city = "Palo Alto"
print("There are " + str(population) + " " + animal + " in " + city + ".")

Output:

$ python dzone.py 
There are 3 dogs in Palo Alto.

This is difficult to read and easy to make errors. Even with syntax highlighting, it’s easy to miss a space.

String Format

A better method is to use .format() and make strings like this:

animal = "dogs"
population = 3
city = "Palo Alto"

print("There are {} {} in {}".format(population, animal, city))

$ python dzone.py 
There are 3 dogs in Palo Alto

Although .format is a big improvement over string concatenation, it is still a bit clunky.

F String

First, upgrade to Python 3.6 or 3.7. Now, you can use f strings.

print(f"There are {population} {animal} in {city}")

Output:

$ python3 dzone.py 
There are 3 dogs in Palo Alto

4. Lambda Functions Are Anonymous

Python lambda functions are shortcuts. Although they can be assigned to a variable, similar to a normal function, they are commonly used as an anonymous function using the syntax below.

print((lambda num1, num2: num1 + num2)(4,6))


Output:

$ python3 dzone.py 
10


Like most of these Python shortcuts, the lambda function doesn’t usually add new functionality. Though, it can reduce the code complexity once you get used to the syntax.

5. Decorator Functions Extend Python Functions

You’ll likely see a decorator function used with an @decorator_name above a function.

@time_decorator
def cool_function:
  print("doing cool things")

The name of the decorator can be anything. For example, it would work with @panda . You don’t have to understand how to create your own decorator in order to use it. For example, let’s look at the Django documentation for http decorators.

from django.views.decorators.http import require_http_methods
@require_http_methods(["GET", "POST"])
def my_view(request):
    # I can assume now that only GET or POST requests make it this far
    # ...
    pass

The import line allows you to use pre-built decorators. In this case, you just need to understand that the @require_http_methods adds additional features to the function you created called, my_view() .

Additional Tips

As I primarily discuss Python programming with a group of people focused on a specific class of problems, I would love to get additional tips suitable for novice programmers that can help them interact with a wider developer community.

===================================================================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

Complete Python Bootcamp: Go from zero to hero in Python 3

Python and Django Full Stack Web Developer Bootcamp

Python for Time Series Data Analysis

Python Programming For Beginners From Scratch

Beginner’s guide on Python: Learn python from scratch! (New)

Python for Beginners: Complete Python Programming

Django 2.1 & Python | The Ultimate Web Development Bootcamp

Python eCommerce | Build a Django eCommerce Web Application

Python Django Dev To Deployment

#python #web-development

What is GEEK

Buddha Community

5 Python Tips for Beginners
Ray  Patel

Ray Patel

1619518440

top 30 Python Tips and Tricks for Beginners

Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.

1) swap two numbers.

2) Reversing a string in Python.

3) Create a single string from all the elements in list.

4) Chaining Of Comparison Operators.

5) Print The File Path Of Imported Modules.

6) Return Multiple Values From Functions.

7) Find The Most Frequent Value In A List.

8) Check The Memory Usage Of An Object.

#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners

Ray  Patel

Ray Patel

1619510796

Lambda, Map, Filter functions in python

Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.

Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is

Syntax: x = lambda arguments : expression

Now i will show you some python lambda function examples:

#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map

Sival Alethea

Sival Alethea

1624291780

Learn Python - Full Course for Beginners [Tutorial]

This course will give you a full introduction into all of the core concepts in python. Follow along with the videos and you’ll be a python programmer in no time!
⭐️ Contents ⭐
⌨️ (0:00) Introduction
⌨️ (1:45) Installing Python & PyCharm
⌨️ (6:40) Setup & Hello World
⌨️ (10:23) Drawing a Shape
⌨️ (15:06) Variables & Data Types
⌨️ (27:03) Working With Strings
⌨️ (38:18) Working With Numbers
⌨️ (48:26) Getting Input From Users
⌨️ (52:37) Building a Basic Calculator
⌨️ (58:27) Mad Libs Game
⌨️ (1:03:10) Lists
⌨️ (1:10:44) List Functions
⌨️ (1:18:57) Tuples
⌨️ (1:24:15) Functions
⌨️ (1:34:11) Return Statement
⌨️ (1:40:06) If Statements
⌨️ (1:54:07) If Statements & Comparisons
⌨️ (2:00:37) Building a better Calculator
⌨️ (2:07:17) Dictionaries
⌨️ (2:14:13) While Loop
⌨️ (2:20:21) Building a Guessing Game
⌨️ (2:32:44) For Loops
⌨️ (2:41:20) Exponent Function
⌨️ (2:47:13) 2D Lists & Nested Loops
⌨️ (2:52:41) Building a Translator
⌨️ (3:00:18) Comments
⌨️ (3:04:17) Try / Except
⌨️ (3:12:41) Reading Files
⌨️ (3:21:26) Writing to Files
⌨️ (3:28:13) Modules & Pip
⌨️ (3:43:56) Classes & Objects
⌨️ (3:57:37) Building a Multiple Choice Quiz
⌨️ (4:08:28) Object Functions
⌨️ (4:12:37) Inheritance
⌨️ (4:20:43) Python Interpreter
📺 The video in this post was made by freeCodeCamp.org
The origin of the article: https://www.youtube.com/watch?v=rfscVS0vtbw&list=PLWKjhJtqVAblfum5WiQblKPwIbqYXkDoC&index=3

🔥 If you’re a beginner. I believe the article below will be useful to you ☞ What You Should Know Before Investing in Cryptocurrency - For Beginner
⭐ ⭐ ⭐The project is of interest to the community. Join to Get free ‘GEEK coin’ (GEEKCASH coin)!
☞ **-----CLICK HERE-----**⭐ ⭐ ⭐
Thanks for visiting and watching! Please don’t forget to leave a like, comment and share!

#python #learn python #learn python for beginners #learn python - full course for beginners [tutorial] #python programmer #concepts in python

Art  Lind

Art Lind

1602968400

Python Tricks Every Developer Should Know

Python is awesome, it’s one of the easiest languages with simple and intuitive syntax but wait, have you ever thought that there might ways to write your python code simpler?

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

Let’s get started

Swapping value in Python

Instead of creating a temporary variable to hold the value of the one while swapping, you can do this instead

>>> FirstName = "kalebu"
>>> LastName = "Jordan"
>>> FirstName, LastName = LastName, FirstName 
>>> print(FirstName, LastName)
('Jordan', 'kalebu')

#python #python-programming #python3 #python-tutorials #learn-python #python-tips #python-skills #python-development

Art  Lind

Art Lind

1602666000

How to Remove all Duplicate Files on your Drive via Python

Today you’re going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates.

Intro

In many situations you may find yourself having duplicates files on your disk and but when it comes to tracking and checking them manually it can tedious.

Heres a solution

Instead of tracking throughout your disk to see if there is a duplicate, you can automate the process using coding, by writing a program to recursively track through the disk and remove all the found duplicates and that’s what this article is about.

But How do we do it?

If we were to read the whole file and then compare it to the rest of the files recursively through the given directory it will take a very long time, then how do we do it?

The answer is hashing, with hashing can generate a given string of letters and numbers which act as the identity of a given file and if we find any other file with the same identity we gonna delete it.

There’s a variety of hashing algorithms out there such as

  • md5
  • sha1
  • sha224, sha256, sha384 and sha512

#python-programming #python-tutorials #learn-python #python-project #python3 #python #python-skills #python-tips