Karim Aya

Karim Aya

1562121176

5 common mistakes made by beginner Python programmers

Python is a relatively easy language to get started in where there’s … Here are 5 common Python programming mistakes most beginners find …

During the initial days as python programmer, all of us face some or other type of weird bug in our code which, after spending multiple painful hours on StackOverflow, turns out to be not a bug but python feature. That’s how things work in python. So below are the 5 most common mistakes most of the beginner python programmers make. Let’s know a bit about them so that we can save a few hours of asking questions on Facebook pages and groups.

Creating a copy of dictionary or lists.

Whenever you need to make a copy of a dictionary or list, do not simply use the assignment operator.

Wrong:

>>> dict_a = {"name": "John", "address":"221B Baker street"}
>>> dict_b = dict_a

Now if you edit/update the dict_b, dict_a will also be updated because by using assignment operator, you are trying to say that dict_b will point to the same object to which dict_a is pointing.

>>> dict_b["age"] = 26
>>> dict_b
{'address': '221B Baker street', 'name': 'John', 'age': 26}
>>> dict_a
{'address': '221B Baker street', 'name': 'John', 'age': 26}
>>> 

Correct:

Use the copy() or deepcopy() method.

>>> dict_c = dict_b.copy()
>>> dict_c["location"] = "somewhere"
>>> dict_c
{'address': '221B Baker street', 'name': 'John', 'age': 26, 'location': 'somewhere'}
>>> dict_b
{'address': '221B Baker street', 'name': 'John', 'age': 26}
>>> dict_a
{'address': '221B Baker street', 'name': 'John', 'age': 26}
>>> 

See the difference between copy and deepcopy.

Dictionary keys.

Let’s say we put the below values in a dictionary.

>>> dict_a = dict()
>>> dict_a
{}
>>> dict_a[1] = "apple"
>>> dict_a[True] = "mango"
>>> dict_a[2] = "melon"

If we try to print the dictionary, what will be the output. Let’s see.

>>> dict_a
{1: 'mango', 2: 'melon'}

What just happened? where is the key True?

Remember Boolean class is the subclass of Integer. Integer equivalent of True is 1 and that of False is 0. Hence the value of key 1 is overwritten.

>>> isinstance(True, int)
True
>>> isinstance(False, int)
True
>>> True == 1
True
>>> False == 0
True
>>> 

Updating lists or dictionaries.

Let’s say you want to append an item to the list.

>>> list_a = [1,2,3,4,5]
>>> list_a = list_a.append(6)
>>> list_a
>>> # prints nothing

Try to update a dictionary.

>>> dict_a = {"a" : "b"}
>>> dict_a = dict_a.update({"c" : "d"})
>>> dict_a
>>> # prints nothing

Ok, let’s try to sort a list.

>>> list_b = [2,5,3,1,7]
>>> list_b = list_b.sort()
>>> list_b
>>> # prints nothing

Why nothing is being printed? What are we doing wrong?

Most the sequence object methods like sort, update, append, add, etc works in place to increase performance by avoiding to create a separate copy un-necessarily.

Do not try to assign the output of such methods to the variable.

Right way:

>>> list_a = [1,2,3,4,5]
>>> list_a.append(6)
>>> dict_a = {"a" : "b"}
>>> dict_a.update({"c" : "d"})
>>> dict_a
{'c': 'd', 'a': 'b'}
>>> list_a.sort()
>>> list_a
[1, 2, 3, 4, 5, 6]

Interned Strings.

In some cases, python tries to reuse the existing immutable objects. String interning is one such case.

>>> a = "gmail"
>>> b = "gmail"
>>> a is b
True

Here we tried to create two different string objects. But when checked if both the objects are same, it returned True. This is because python didn’t create another object b but pointed the b to the first value “gmail”.

All strings of length 1 are interned. A string having anything except ASCII characters, digits and underscore in them are not interned.

Let’s see.

>>> a = "@gmail"
>>> b = "@gmail"
>>> a is b
False
>>> 

Also, remember == is different than is operator. == checks if values are equal or not while is checks if both the variable are pointing to the same object.

>>> a = "@gmail"
>>> b = "@gmail"
>>> a is b
False
>>> a == b
True
>>> 

So keep the above point in mind while using immutable strings or == or isoperator.

Default arguments are evaluated only once.

Consider the below example.

def func(a, lst=[]):
    lst.append(a)
    return lst

print(func(1))
print(func(2))

What do you think will be the output of the above two print statements?

Let’s try to run it.

>>> def func(a, lst=[]):
...     lst.append(a)
...     return lst
... 
>>> print(func(1))
[1]
>>> print(func(2))
[1, 2]
>>> 

Why the output is [1,2] in the second case. Shouldn’t it be just [2]?

So the catch here is, default arguments of a function are evaluated just once. On first call i.e func(1), list lst is evaluated and is found empty hence 1 is appended to it. But on the second call, the list is already having one element hence output is [1,2]

#python #web-development

What is GEEK

Buddha Community

5 common mistakes made by beginner Python programmers
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

Samanta  Moore

Samanta Moore

1623795660

Every Java Junior does it: Common Java mistakes made by newcomers

Everyone makes mistakes, not just beginners, but even professionals. This article goes over a dozen common mistakes that Java newbies and newcomers make and how to avoid them. Have you or your colleagues made any of these common Java mistakes early in your career?

Everyone makes mistakes, not only learners or beginners, but professionals. As a programming course, CodeGym team often collects mistakes of newbies to improve our auto validator. This time we decided to interview experienced programmers about mistakes in Java they made closer to their careers start or noticed them among their young colleagues.

We collected their answers and compiled this list of dozen popular mistakes Java beginners make. The order of errors is random and does not carry any special meaning.

#articles #java #everyone makes mistakes #beginners #common mistakes #common java mistakes made by newcomers

Ray  Patel

Ray Patel

1619636760

42 Exciting Python Project Ideas & Topics for Beginners [2021]

Python Project Ideas

Python is one of the most popular programming languages currently. It looks like this trend is about to continue in 2021 and beyond. So, if you are a Python beginner, the best thing you can do is work on some real-time Python project ideas.

We, here at upGrad, believe in a practical approach as theoretical knowledge alone won’t be of help in a real-time work environment. In this article, we will be exploring some interesting Python project ideas which beginners can work on to put their Python knowledge to test. In this article, you will find 42 top python project ideas for beginners to get hands-on experience on Python

Moreover, project-based learning helps improve student knowledge. That’s why all of the upGrad courses cover case studies and assignments based on real-life problems. This technique is ideally for, but not limited to, beginners in programming skills.

But first, let’s address the more pertinent question that must be lurking in your mind:

#data science #python project #python project ideas #python project ideas for beginners #python project topics #python projects #python projects for beginners