Skip to main content

Python = vs ==

Hello everyone, in this tutorial you’ll see what’s the difference between = and == in python. Most of new programmers get confused with them.

Python = vs ==

= (assignment operator)

Well, in simple words, ‘=’ is an assignment operator which is used to assign a value (on right side) to a variable (on left side).

Example:

var_name = 10

here var_name is a variable name and 10 is the value to be assigned, we’re using ‘=’ to assign it.

Example Programs:

#example 1
n = 10
print("n = ", n)

Output:

n = 10

#example  2
n1 = 10
n2 = 20
sum = n1 + n2
print("sum = ", sum)

Output:

sum = 30

As in above program, we can also write an expression on the right side of  ‘=’ . the result of that expression will be assigned to the variable on left side.

#example  3
n1,n2 = 10,20
print("n1 = " , n1)
print("n2 = " , n2)

Output:

n1 = 10

n2 = 20

In python, we can also assign more than one value at once using commas.

#example 4
n = input("please enter your name:")
print("name = " + n)

Output:

please enter  your name :  crazy programmer

name =  crazy programmer

The value received by input() function will be assigned to variable ‘n’.

==  (equal to operator)

“==” equal to operator is a relational operator just like “!=”, “<=”. It is used to compare the operands on both of its side. It returns a boolean value (true or false), when both operands are equal then it returns ‘true’ otherwise ‘false’. Mostly it is used in conditional statements.

Example Programs:

#example 1
n = 10
print(n == 12)

Output:

False

In above program, the operands on both side of ‘==’ will be compared with each other. If they are equal then program will print “True”, if not then it will print “False”. The output of above program will be ‘False’ because ‘n’ is not equal to ‘12’.

#example 2
name = input("enter your name:")
if(name == "xyz"):
  print("login successful")
else:
  print("login unsuccessful")

Output: 

enter your name: xyz

login successful

In this program first user will enter a name then interpreter will check the condition. Inside the if statement we wrote ‘name == “xyz”’. If the name entered by user is ‘xyz’ then it will return ‘True’ otherwise ‘False’. In output window we’re entering ‘xyz’ as name then the ‘if’ will be true and will print “login successful”.

I hope your doubt has been cleared. If you have any problem related with this article then please comment below, we’ll reply as soon as possible.

The post Python = vs == appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2018/02/python-equal-vs-double-equal.html

Comments

Popular posts from this blog

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan...

15 Web Design Trends to Watch in 2018

The modern world is full of extraordinary things that influence our imagination and mood. Our soul needs a perfect atmosphere and impressive spots. To apply such things in practice, we have submitted the list of the web trends that deserve your attention. Robert frost design analysis will meet all your wishes and expectations. Image Source Web Design Trends to Watch in 2018 1. More Organic Shapes Until this year, web design, as well as mobile design, were based on the right-angled and sharp-edged shapes. However, it seems that this year will bring some significant changes in the field of web design. The recent trends will offer the absolute rounded corners. In addition, the web design of 2018 will make the real things look like the cartoonish ones. 2.   Bold Minimalism Although some of you may think that this web design trend will not attract the Internet users. Indeed, the notion of minimalism is often associated with boredom and dullness. However, in this case, bold ...

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and...