3 Common Mistakes that Python Newbies Make

This post is to talk about the three most common mistakes I saw over the last week and some possible alternatives that might be better! So let’s start the countdown!

This post is to talk about the three most common mistakes I saw over the last week and some possible alternatives that might be better! So let’s start the countdown!


1. Deep Nesting of If Statements or Loops
# Calculating whether or not 'year' is a leap year

if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False

A lot of times, I’ll pull a line from the Zen of Python to lead off my feedback to a “mentee” (not to be confused with a manitee). When I see this issue, I always lead with

Flat is better than nested.

If you look at your code with your eyes unfocused, looking at the shapes and not reading the words, and you see a bunch of arrows going out and back in again:






/
/
/




/
/
/
/
/
/

It’s not definitely a bad thing, but it is a “code smell,” or a Spidey Sense that something could possibly be refactored.

So, what can you do instead of nest? There are a couple things to try. The first is inverting your logic and using “early returns” to peel off small pieces of the solution space one at a time.

if year % 400 == 0:
return True
if year % 100 == 0:
return False
if year % 4 == 0:
return True
return False

If the number is divisible by 400, then we immediately return true. Otherwise, for the rest of our code, we can know that year is definitely not divisible by 400. So, at that point, any other year that’s divisible by 100 is not a leap year. So we peel off that layer of the onion by returning False.

After that, we can know that year is definitely not a multiple of 400 or 100, and the remainder of the code follows the same pattern.

The other way to avoid nesting is by using “boolean operators:” and, or, and not. We can combine if statements and thus, save ourselves a layer of nesting!

if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
return True
else:
return False

Of course, that leads us to our second item…


2. Returning Booleans from If Statements

We’ll start with our last example from above:

if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
return True
else:
return False

Anytime you find yourself writing:

if something:
return True
else:
return False

You should remember that the clause of an if statement is itself a boolean!

>>> year = 2000
>>> year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
True

So, why not type a little less and return the result of the boolean operation directly?

return (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0))

Granted, at this point, the line may be getting a little long, but the code is a little less redundant now!


3. Lists are Like Hammers – Not Everything is a Nail

Here are two possible ways that this could show up:

some_numbers = [1, 2, 5, 7, 8, ...]
other_numbers = [1, 3, 6, 7, 9, ...]

Let's try to combine these two without duplicates

for number in other_numbers:
if number not in some_numbers:
some_numbers.append(number)

Or:

data = [["apple", 4], ["banana", 2], ["grape", 14]]

What fruits do we have?

for item in data:
print(item[0])

=> "apple" "banana" "grape" How many grapes do we have?

for item in data:
if item[0] == "grape":
print(item[1])

=> 14

In the first case, you’re trying to keep track of some groups of items and you want to combine them without duplicates. This is an ideal candidate for a set. Sets inherently keep track of their items (although not the order, so don’t use a set if the order is important). You can declare them with the built-in set() function or with squiggle braces ({}).

some_numbers = {1, 2, 5, 7, 8}
other_numbers = {1, 3, 6, 7, 9}

Sets use the 'binary or' operator to do "unions" which is where they take all of the unique elements

some_numbers | other_numbers

=> {1, 2, 3, 5, 6, 7, 8, 9} You can even add single items in!

some_numbers.add(10)

=> {1, 2, 5, 7, 8, 10} But adding a duplicate doesn't change anything

some_numbers.add(1)

=> {1, 2, 5, 7, 8, 10}

In the second case, again, order probably isn’t critical. You want to keep track of some data by a “label” or something, but be able to keep them all together and list them out as necessary. This time, you’re probably looking for a dict. You can create those with either the dict() built-in function or, again, squiggle braces ({}). This time, however, you separate the labels (keys) and the values with a colon.

fruits = {
"apples": 4,
"bananas": 2,
"grapes": 14,
}

You can list out all of the keys (or values!).

list(fruits.keys())

=> ["apples", "bananas", "grapes"]

list(fruits.values())

=> [4, 2, 14] Or both!

list(fruits.items())

=> [("apples", 4), ("bananas", 2), ("grapes", 14)]

And you can ask it about (or give it a new value for) specific keys.

# How many grapes are there?
fruits["grapes"]

=> 14 Not anymore. I ate some.

fruits["grapes"] = 0

fruits["grapes"]

=> 0

Using a list, the your algorithm loops through every item to find the right one. dict's are built to have very fast lookups, so, even if your dict is a bazillion fruits long, finding the grapes is still super fast – and easy to type! No loops!

Thanks for reading ❤

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

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

Python and Django Full Stack Web Developer Bootcamp

An A-Z of useful Python tricks

Top 100 Python Interview Questions and Answers

A guide to Face Detection in Python

Python Web Scraping Tutorial

Top 5 Python Frameworks For Test Automation In 2019

Python Tutorial for Beginners (2019) - Learn Python for Machine Learning and Web Development

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: