1584998460
If you are just stumbling onto this series, be sure to check out part’s 1 and 2. For the rest of you, today we will be talking about operators and none
.
While we are still covering the basics, the good news is after today you will be able to write a program that will solve math problems, whether it’s from Kindergarten or advanced financial calculations using a formula.
You’ll need to understand the concept of order of operations (PEMDAS).
If math class was too long ago for you here’s the order:
That’s it!
As long as you made it to 3rd grade, you’ve already seen most operators in programming.
Let’s take a look at the ones you already know:
# Math Operators
# Addition
sum = 10 + 20 # 30
# Subtraction
difference = 100 - 75 # 25
# Multiplication
product = 20 * 5 # 100
# Division
quotient = 15 / 3 # 5
# Floor Division
quotient = 15 // 2 # 7, a single '/' results in 7.5
# Exponent
result = 3 ** 5 # 243
# Comparison Operators
# Greater than
result = 3 > 4 # False
# Less than
result = 3 < 4 # True
# Equal to
result = 3 == 4 # False
# Greater than or equal to
result = 3 >= 4 # False
# Less than or equal to
result = 3 <= 4 # True
# Not equal to
result = 3 != 4 # True
# Inverse (minus)
result = -(-100) # 100
result = -(100) # -100
# Logical Operators
# And - all elements must be True, not 0, or an empty string.
result = True and False # False
# Or - any element must be True, not 0, or an empty string.
result = True or False # True
# Not
result = not True # False
# Is - Identity (points to the same object)
result = 1 is True # True
# In - Membership (resides in range, list, or collection)
my_list = ['eggs', 'milk', 'cheese']
result = 'milk' in my_list # True
Easy stuff, right? Now let’s look at one that you probably aren’t familiar with:
# Remainder - also known as modulo
result = 15 % 10 # 5
O.K. so that last one was weird. It’s my favorite because it allows us to do all kinds of cool math that would normally cause problems. For example, if we wanted to assign a number to every letter of the alphabet and we wanted the 28th letter (wait, what?). Yes, the 28th letter. Well, the 28th letter would be “B” assuming we looped back around to the beginning. We can accomplish this with %
. We could write it out like value = 28 % 26
. value
would then be equal to 2, which the second letter of the alphabet is “B”. This is how the Caesar cipher was made; the Vigenère cipher was developed in the same way later on.
Operators are used frequently in programming. It’s how programs know how to do stuff. We can even use the addition operator for more than just math. For example, we can use it to concatenate strings together.
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name # John Doe
Notice that I used the variable name, while I could have just typed the following:
full_name = "John" + " " + "Doe"
It shows how we can use variables and constants to hold values, so we don’t have to keep typing them. I’m sure you noticed that both first_name
and last_name
are longer than the value they hold. I could change the example to have a last name of Supercalifragilisticexpialidocious, but let’s keep things simple. Both Visual Studio Code and PyCharm have built-in autocomplete that is absolutely a lifesaver, but beyond that, we don’t always know what the value of first_name
or last_name
will be. When we use the variable name in place, we can always be sure that we have the first name, followed by a space, followed by the last name.
A shorthand way of adding, subtracting, multiplying, and dividing is to append the operator to the assignment operator (you can still concatenate strings using this method using the addition operator):
first_number = 1
second_number = 2
first_number += second_number # 3
first_number *= second_number # 2
first_number -= second_number # -1
first_number /= second_number
first_number /= second_number # Guess the value
name = "John"
last_name = "Doe"
name += " " + last_name # John Doe
Hopefully, you’ve got that down by now and maybe have tried exploring on your own. Practicing is always good.
None
is special. It means doesn’t exist. If you are coming from other languages it is synonymous with null
or nil
.
None
doesn’t mean 0, it means without value. To illustrate this, let’s consider you have a notebook sitting on a desk. This notebook can have any number of pages in it. Let us assume it has 200 pages in it. As you make notes, you tear pages out of the notebook. When you run out of pages, the notebook is all that remains. Since we don’t have any pages in it, we throw the notebook away.
While the notebook could be representative of a page_count
variable, holding the number of pages that currently reside in the notebook. When the notebook hit 0 pages, it was no longer useful to us, so we removed the notebook, in Python, it looks like:
del page_count
All we have now is an empty desk which represents our program. We can ask the following to determine if the notebook (page_count
) still exists:
notebook_status = page_count is not None # False
We could have checked if page_count is None
, but that wouldn’t have made sense for a notebook_status
. By adding not
, we make the world right by asking if page_count
exists.
Now I want to add something important about None
using an example:
page_count = None
# is NOT the same as
del page_count
Why? None
is a type named NoneType
. It’s about like assigning null
or nil
to a variable, except we will still hold a reference to the value so garbage collection will not free up the memory. While this could be an improvement to keep your code fast by keeping the reference to memory around, if you want to allow garbage collection to free this memory, you must use del
as this method also removes the reference to the variable.
Whew, that’s a lot for today! Take a deep breath. You made it through another part. Hopefully, you learned a little about some of the operators Python has to offer.
We also learned about None type, why we might use this type and an alternative method when you want to free memory instead.
In the next post, we will cover decision making and loops. This is where things start coming together, and we get to have a little bit of fun. Don’t forget to practice in between the lessons. In programming, practicing goes a long way.
Please review the article
Thank you for reading and keep visitting!
#python #programming #tutorial
1619518440
Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.
…
#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
1574340419
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
#uide to Python #Guide to Python Programming #Guide to Python Programming Language #Python Programming #Python Programming Language
1619510796
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
1599813647
Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it? Well, you are in the right place to discover the answer! and I’ll also answer the why, what, and how regarding Python programming.
People prefer Python over French (What 😮)
According to a recent survey, in the UK, Python overtook French to be the most popular language taught in primary schools. (OMG!) 6 of 10 parents preferred their children to learn Python over French.
So hurry up🏃♂️🏃♀️(or these kids will for sure)! Get ready to learn it! Because there’s a possibility of you being hired in one of the companies mentioned below!!!
#python #python-programming #software-development #python3 #python-tips #learn-python #python-top-story #python-skills
1624930726
Alright is a python wrapper that helps you automate WhatsApp web using python, giving you the capability to send messages, images, video, and files to both saved and unsaved contacts without having to rescan the QR code every time.
I was looking for a way to control and automate WhatsApp web with Python; I came across some very nice libraries and wrappers implementations, including:
So I tried
pywhatkit, a well crafted to be used, but its implementations require you to open a new browser tab and scan QR code every time you send a message, no matter if it’s the same person, which was a deal-breaker for using it.
I then tried
pywhatsapp,which is based on
yowsupand require you to do some registration withyowsup
before using it of which after a bit of googling, I got scared of having my number blocked. So I went for the next option.
I then went for WebWhatsapp-Wrapper. It has some good documentation and recent commits so I had hoped it is going to work. But It didn’t for me, and after having a couple of errors, I abandoned it to look for the next alternative.
PyWhatsapp by shauryauppal, which was more of a CLI tool than a wrapper, surprisingly worked. Its approach allows you to dynamically send WhatsApp messages to unsaved contacts without rescanning QR-code every time.
So what I did is refactoring the implementation of that tool to be more of a wrapper to easily allow people to run different scripts on top of it. Instead of just using it as a tool, I then thought of sharing the codebase with people who might struggle to do this as I did.
#python #python-programming #python-tutorials #python-programming-lists #selenium #python-dev-tips #python-developers #programming #web-monetization