5 common mistakes made by beginner Python programmers

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

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]

Mobile App Development Company India | Ecommerce Web Development Company India

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

We are custom eCommerce Development Company working with all types of industry verticals and providing them end-to-end solutions for their eCommerce store development.

Know more about Top E-Commerce Web Development Company

Python Tutorial - Learn Python for Machine Learning and Web Development

Python Tutorial - Learn Python for Machine Learning and Web Development

Python tutorial for beginners - Learn Python for Machine Learning and Web Development. Can Python be used for machine learning? Python is widely considered as the preferred language for teaching and learning ML (Machine Learning). Can I use Python for web development? Python can be used to build server-side web applications. Why Python is suitable for machine learning? How Python is used in AI? What language is best for machine learning?

Python tutorial for beginners - Learn Python for Machine Learning and Web Development

TABLE OF CONTENT

  • 00:00:00 Introduction
  • 00:01:49 Installing Python 3
  • 00:06:10 Your First Python Program
  • 00:08:11 How Python Code Gets Executed
  • 00:11:24 How Long It Takes To Learn Python
  • 00:13:03 Variables
  • 00:18:21 Receiving Input
  • 00:22:16 Python Cheat Sheet
  • 00:22:46 Type Conversion
  • 00:29:31 Strings
  • 00:37:36 Formatted Strings
  • 00:40:50 String Methods
  • 00:48:33 Arithmetic Operations
  • 00:51:33 Operator Precedence
  • 00:55:04 Math Functions
  • 00:58:17 If Statements
  • 01:06:32 Logical Operators
  • 01:11:25 Comparison Operators
  • 01:16:17 Weight Converter Program
  • 01:20:43 While Loops
  • 01:24:07 Building a Guessing Game
  • 01:30:51 Building the Car Game
  • 01:41:48 For Loops
  • 01:47:46 Nested Loops
  • 01:55:50 Lists
  • 02:01:45 2D Lists
  • 02:05:11 My Complete Python Course
  • 02:06:00 List Methods
  • 02:13:25 Tuples
  • 02:15:34 Unpacking
  • 02:18:21 Dictionaries
  • 02:26:21 Emoji Converter
  • 02:30:31 Functions
  • 02:35:21 Parameters
  • 02:39:24 Keyword Arguments
  • 02:44:45 Return Statement
  • 02:48:55 Creating a Reusable Function
  • 02:53:42 Exceptions
  • 02:59:14 Comments
  • 03:01:46 Classes
  • 03:07:46 Constructors
  • 03:14:41 Inheritance
  • 03:19:33 Modules
  • 03:30:12 Packages
  • 03:36:22 Generating Random Values
  • 03:44:37 Working with Directories
  • 03:50:47 Pypi and Pip
  • 03:55:34 Project 1: Automation with Python
  • 04:10:22 Project 2: Machine Learning with Python
  • 04:58:37 Project 3: Building a Website with Django

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading

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

Machine Learning A-Z™: Hands-On Python & R In Data Science

Python and Django Full Stack Web Developer Bootcamp

Complete Python Masterclass

Python Programming Tutorial | Full Python Course for Beginners 2019 👍

Top 10 Python Frameworks for Web Development In 2019

Python for Financial Analysis and Algorithmic Trading

Building A Concurrent Web Scraper With Python and Selenium

Hire PHP Developer and Web Developer for your Online Business

Hire PHP Developer and Web Developer for your Online Business

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application...

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application development. To hire PHP developer and web developer at affordable prices contact Mobiweb Technology via [email protected]