5 common mistakes made by beginner Python programmers

5 common mistakes made by beginner Python programmers

Originally published at pythoncircle.com

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 is operator.

- 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]

Bonus: Don't mix spaces and tabs. Just don't. You will cry.

More from pythoncircle.com:
-- Adding Robots.Txt File To Django Application
-- How To Host Django App On Pythonanywhere For Free
-- How To Send Email From Python And Django Using Office 365

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Description
Learn Hands-On Python Programming By Creating Projects, GUIs and Graphics

Python is a dynamic modern object -oriented programming language
It is easy to learn and can be used to do a lot of things both big and small
Python is what is referred to as a high level language
Python is used in the industry for things like embedded software, web development, desktop applications, and even mobile apps!
SQL-Lite allows your applications to become even more powerful by storing, retrieving, and filtering through large data sets easily
If you want to learn to code, Python GUIs are the best way to start!

I designed this programming course to be easily understood by absolute beginners and young people. We start with basic Python programming concepts. Reinforce the same by developing Project and GUIs.

Why Python?

The Python coding language integrates well with other platforms – and runs on virtually all modern devices. If you’re new to coding, you can easily learn the basics in this fast and powerful coding environment. If you have experience with other computer languages, you’ll find Python simple and straightforward. This OSI-approved open-source language allows free use and distribution – even commercial distribution.

When and how do I start a career as a Python programmer?

In an independent third party survey, it has been revealed that the Python programming language is currently the most popular language for data scientists worldwide. This claim is substantiated by the Institute of Electrical and Electronic Engineers, which tracks programming languages by popularity. According to them, Python is the second most popular programming language this year for development on the web after Java.

Python Job Profiles
Software Engineer
Research Analyst
Data Analyst
Data Scientist
Software Developer
Python Salary

The median total pay for Python jobs in California, United States is $74,410, for a professional with one year of experience
Below are graphs depicting average Python salary by city
The first chart depicts average salary for a Python professional with one year of experience and the second chart depicts the average salaries by years of experience
Who Uses Python?

This course gives you a solid set of skills in one of today’s top programming languages. Today’s biggest companies (and smartest startups) use Python, including Google, Facebook, Instagram, Amazon, IBM, and NASA. Python is increasingly being used for scientific computations and data analysis
Take this course today and learn the skills you need to rub shoulders with today’s tech industry giants. Have fun, create and control intriguing and interactive Python GUIs, and enjoy a bright future! Best of Luck
Who is the target audience?

Anyone who wants to learn to code
For Complete Programming Beginners
For People New to Python
This course was designed for students with little to no programming experience
People interested in building Projects
Anyone looking to start with Python GUI development
Basic knowledge
Access to a computer
Download Python (FREE)
Should have an interest in programming
Interest in learning Python programming
Install Python 3.6 on your computer
What will you learn
Build Python Graphical User Interfaces(GUI) with Tkinter
Be able to use the in-built Python modules for their own projects
Use programming fundamentals to build a calculator
Use advanced Python concepts to code
Build Your GUI in Python programming
Use programming fundamentals to build a Project
Signup Login & Registration Programs
Quizzes
Assignments
Job Interview Preparation Questions
& Much More

Guide to Python Programming Language

Guide to Python Programming Language

Guide to Python Programming Language

Description
The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.

The course is begin continuously developing by adding lectures regularly.

Please see the Promo and free sample video to get to know more.

Hope you will enjoy it.

Basic knowledge
An Enthusiast Mind
A Computer
Basic Knowledge To Use Computer
Internet Connection
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Description
Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.

As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about

programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.

In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With

Lots of exercises and more fun stuff, let's get started.

Download free Exercise files.

Dropbox: https://bit.ly/2AW7FYF

Who is the target audience?

First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Basic knowledge
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future

To know more: