Python Basics – Print, Data types and comments

Using Numbers, Integers and Floats in python

Doing calculations is very very important. I used very twice because its that important. Moving on.

Try this in python

print (2)
print(“2”)

both will return the value 2. Now try this

print(2 + “2”)

you were expecting 4 instead it give you an error. What gives right. Did I break python? No I didn’t I mean I don’t know if that’s even possible. However what really happened was python looked at both these numbers differently. It saw the first 2 as a number and the second as a text because we used quotations. You can’t do a calculation between a text and a number therefore its threw an error to your face.
Try this line now

print(2+2)

output will be 4. All is well in the world again, right.

Similar to what we learnt with string numbers have their own rules in programming. They don’t like being stuck in quotations so if you want to calculate something just use the plain old numbers.

There are many different arithmetic’s in python programming. For this tutorial we will use the basics hence the tutorial basic programming in python.

Add Subtract Multiply Divide
2+2 2-2 2*2 2/2

 

Its very simple. Now important thing to remember here python or any other programming language will first do the multiply and divide first after that it will do the add or subtract.

Lets test this theory out

print (2+2-3*5)

This will return -11 as a result if you were to do this on a calculator from the beginning it will give you 5. 2+2 = 4, 4-3 = 1, 1*5 = 5. Since python is multiplying first it’s doing 5*3 = 15, 4 – 15 = -11. Making sense now. If not read it again.

If we wanted to do the times last we will need use extra parenthesis to make sure we prioritize the calculations we want first.

Follow this one

print ((2+2-3)*5)

This will return the value 5. Hope that clears things up on this end.
Lets look up rest of the arithmetics in python

print(“Add 5 + 2”, 5+2) # mixing string a integers together
print(“Subtract 5 – 2”, 5-2) #subtracting numbers
print(“Multiply 5*2”, 5*2) #multiplying numbers
print(“Divide 5/2”, 5/2) # division

look up the results, do the maths yourself to make sure its accurate.

Using number variables in python

Important thing to remember while working with variables you can use lowercase or capital letters but they are not the name. The variable Name and name is not the same python is case sensitive and will treat both variables differently. There is no space allowed in the name of the variable. you can use underscore to show space however be careful. A good programmer names the variables appropriately.

numA = 5
numB = 6
print(numA + numB)

This will return 11. Python looked inside the variable for a value and found 5 and 6. As instructed it added both numbers together and returned a total of 11.

We can also do this

numA = 5
numB = 6
total = numA + numB
print(total)

in this example we have declared another variable called total and done the sum of numA and numB in there instead of print. Lastly we printed the value inside the total variable.
Now try the subtract, multiply and divide arithmetic’s in python.

Now so far we have been working with integers which represent whole numbers such as 1, 2, 4, 50 , 66 etc. To use decimal points its exactly the same as integers in python. we will traditionally use numA = 2.2 now this will present a float or a number with a decimal point for basics.

numA = 2.2
print(numA)

Returns 2.2 to the display. This is a float data type. it can be used for currency, height, weight, width or any other calculations that you need decimal points for.
The reason I am making the distinction of mentioning here other than I am very nice is when you move to more complicated programming you might want to declare which is an integer and which is a float its important to know the difference.




Comments are closed.

%d bloggers like this: