Sunday, May 10, 2015

Exponents In Python - Python Numbers - Python Tutorial

Exponents in PythonExponents In Python


In this Python Tutorial, we are going to look at exponents in Python. We first need to realise what an exponent is. An exponent is the power to which a number is raised. For example 2 * 2 * 2 is equivalent to 8. With an exponent, we can cut down on writing the whole equation out and use **(exponent in Python) by using this operator we are doing the equation in shorthand. Example of exponent 2 ** 3 is equal to 8.  Less typing the same outcome.


Examples Exponents In Python


We will perform all these examples in our Python interpreter. Make sure you follow a long and do some of your own. The best way to learn Python is by practicing.


#Examples of Exponents In Python

>>> 2 ** 4
16

>>> 78 ** 97
3413315720704337419097101027000771686980504078677575730926905758203118108388932801027381449501146100963728165489443041132814728861531870470672271776414594578193877674244432184882495488

>>> 5 ** 5
3125

>>> 4 ** 2
16

>>> 9 ** 7
4782969

>>> 7.5 ** 4
3164.0625

As you can see there is not much to exponents in Python. Since exponents are pretty simple to work with there is not much to this Python tutorial. Just remember that exponents are available to you and the syntax and you will be all ready to go.



Saturday, May 9, 2015

Modulo Operator to Get Remainder In Python - Python Numbers - Python Tutorial

Modulo Operator to Get Remainder In PythonModulo Operator to Get Remainder In Python


In this Python tutorial we are going to focus on Modulo Operator to Get Remainder In Python.  We could use modulo to get remainder after division is performed.  This can be a little confusing at times so make sure you check work closely to make sure your program is performing as expected.


How Modulo Operator Works


To implement the modulo operator we need to use the percent sign(%) in our equations. When use the percent sign in an equation we will get the remainder after division is performed. Sometimes this will return a number that does not make much sense so we will look at that as well.


Modulo Examples


In this tutorial all the examples will be carried out in the Python interpreter. We suggest you open your Python interpreter and follow a long. Practicing modulos will help you figure out how it actually works.


#Modulo Examples

#No remainder when you divide 8 by 2
>>> 8 % 2
0

#10 divided by 3 is 9 with 1 left over
>>> 10 % 3
1

#12.5 divided by 5 is 2.5
>>> 12.5 % 5
2.5


Now the Confusing Modulo Equations


This may or may not be confusing to you, but I remember when I first learned how to use modulo this was a bit confusing to me a times until I figured out what was going on.


#Now the Confusing Modulo Equations

#What is going on here?
#When you use modulo here where numbers go in evenly it will return the exact number of the modulo number
>>> 25 % 50
25


#This is also the proper remainder
>>> 30 % 70
30

In the above example what is going on? Well we can not divide a smaller number by a larger number.  Think of this way if you have a job to go out and get 70 beers and not to return till you got these 70 beers going to a store with only 30 is not going to cut it so when you leave that store the store still has 30 beers. This is the best way I can explain it.

In this tutorial, we looked at Modulo Operator to Get Remainder In Python which is a useful tool to find the remaining numbers. If you have any questions please leave a comment below.



Fahrenheit To Celsius Conversion Program in Python - Python Tutorial

Fahrenheit To Celsius Conversion Program in PythonFahrenheit To Celsius Conversion Program


In this tutorial, we are going to build a simple Fahrenheit to Celsius Conversion Program in Python. This program is similar to our previous program with some slight changes to perform fahrenheit to celsius conversion.


Functions and Methods Used 


  • printt()

  • float()

  • input()

  • round()

  • .format()

Step 1 – Open Text Editor and Save Program


In the first step, we need to open your preferred text editor we use Sublime Text and save your program to your desktop as ftoc.py.


Step 2 – Write a welcome to our users


In this step, we are going to write a simple welcome statement to our users using a print statement.


#Write a welcome to our users

print("Welcome to Fahrenheit to Celsius Program")

Step 3 – User’s Input


First we need to create a variable in this step we created a variable called “fah” for fahrenheit.  Then we need to convert the user’s input to a floating point number using float().  Then we prompt the user for a number using the input() function.


#User's Input

print("Welcome to Fahrenheit to Celsius Program")
fah = float(input("Please give us a temperature in Fahrenheit. "))

Step 4 – The Conversion


In this step, we have to create a variable to point to the value we get from our conversion. We create a variable called “cel” for celsius. We will need to round this equation so we put the equation in the round() function with one number after the decimal. Then we have to put the first part of the equation in parentheses so this part is performed before any other part of the equation (fah – 32). Then we need in order to finish off the equation by multiplying by 5 and then divide by 9.


#The Equation

print("Welcome to Fahrenheit to Celsius Program")
fah = float(input("Please give us a temperature in Fahrenheit. "))
cel = round((fah - 32) * 5 / 9, 1)

Step 5 – Return the conversion to the user


We return to the conversion to the user using a print() statement which we .format() to include our variable “cel” which will be replaced by the value of our conversion.


#Return Conversion To User

print("Welcome to Fahrenheit to Celsius Program")
fah = float(input("Please give us a temperature in Fahrenheit. "))
cel = round((fah - 32) * 5 / 9, 1)
print("The temperature in Celsius is ".format(cel))

Step 6 – Run the Program


To run the program open your terminal or command prompt.  Cd into your desktop directory and enter python3 ftoc.py


#Run The Program

Thomass-MBP:desktop Tommy$ python3 ftoc.py
Welcome to Fahrenheit to Celsius Program
Please give us a temperature in Fahrenheit. 78
The temperature in Celsius is 25.6

In this Python Programming Tutorial, we created a Fahrenheit to Celsius Conversion Program in Python. If you have any questions please leave a comment below and we will help you out.


 



Friday, May 8, 2015

Division In Python - Python Numbers - Python Tutorial

Division in PythonDivision In Python


We have seen division way back in one of our earlier tutorials.  In Python 3, there are two different types of division we can do first one being true division and the second being floor division. Understanding what value you need to return to you is key when working with division in Python.


What Is True Division In Python


True division is represented by a single backslash(/) and we get our results as a floating point number which will return the remainder in the position after the decimal.


True Division Examples


#True Division Examples

>>> 3 /7
0.42857142857142855

>>> 8 / 9
0.8888888888888888

>>> 90 / 3
30.0

>>> 8 / 5
1.6

>>> 45890 / 78
588.3333333333334

>>> 89 / 12
7.416666666666667

>>> 90.0 / 3
30.0

Floating Point Accuracy


We have talked about the floating point accuracy a lot in our previous tutorials. When using division in Python we see a lot of numbers returned inaccurate do the fraction and floating point conversion.  To fix these issues, we need to use the built-in function round() or the decimal module that is available to us. We will not cover them in this tutorial so if you have not seen those tutorial I suggest your visit the round() tutorial or decimal module tutorial.


What is Floor Division


Floor division is represented by a double backslash(//). Floor division will return with a rounded down floating point number or integer and this return type is based off which type we use in the equation. Which means that we will not get a remainder when we use the floor division operator.


Floor division Example


#Floor division Example

#integers
>>> 89 // 9
9

>>> 124345 // 67
1855

>>> 345 // 8
43

>>> 89 // -6
-15

>>> 787643 // 2345
335

#floats
>>> 689.90 // 987
0.0

>>> 8976.98 // 76
118.0

>>> 865 // 8
108

>>> 78.6768789789 // 2
39.0

>>> 765734.1 // 67.3
11377.0

Conclusion


We have just seen how to perform division in Python. When writing your program, you need to figure out how precisely you would like your numbers. If you need a precise number, then I would use true division if you need a number that was not as precise the floor division may be a better decision.

If you have any questions about our Python tutorial on division in Python leave, a comment below and we will help you out.


 



Convert Celsius to Fahrenheit Python Program tutorial - Python Tutorial

Convert Celsius to Fahrenheit Python Program tutorialConvert Celsius to Fahrenheit Python Program tutorial


In this Python tutorial, we are going to build a Python program that will convert celsius to fahrenheit. This is another very simple program that takes the users input and converts celsius temperature to a fahrenheit temperature and prints the temperature back to the user.


Functions and Methods Used in This Tutorial


  • print()

  • float()

  • .format()

Build The Python Program


In this program, we will be using Python 3, a text editor of your choice we use Sublime Text and the Python interpreter.


Step 1 – Open Text Editor and Save Program


Open your text editor and save your file to your desktop as ctof.py(Celsius to Fahrenheit). The dot py is a Python extension.


Step 2 – Welcome Message to Users


We want to just add a very simple welcome message to the users.  To this we use the print() function.


#Welcome Message to Users

#Spelled celsius and Fahrenheit wrong sorry!
print("Convert Celcius to Fahreheit")

Step 3 – Retrieve the users input


To retrieve the user’s input we first need to assign a variable to that object the user will create. In this step, we create a variable called “cel” for celsius. We then need in order to convert the number to a floating point number using the float() function in Python. Then we use the input() function to get the users temperature in celsius.  We also prompt the user for the input() function by adding a string “Input temperature in celsius”.


#Retrieve the users input

print("Convert Celcius to Fahreheit")
cel = float(input("Input tempature in celcius. "))

Step 4 – The conversion


In this step, we need to assign a variable to the value of our equation. We create a variable called “fah” and assign the returned value from our conversion. To convert celsius to fahrenheit we first need to input the user’s value by using the variable called “cel” then multiply that number by 9 then divide by 5 and then add 32 to get the temperature in fahrenheit.


#The conversion

print("Convert Celcius to Fahreheit")
cel = float(input("Input tempature in celcius. "))
fah = cel * 9 / 5 + 32

Step 5 – Print conversion to user


In this step, we need to return the conversion to the user. We do this using a print() statement and use .format() method to convert the string.


#Print Conversion To User

print("Convert Celcius to Fahreheit")
cel = float(input("Input tempature in celcius. "))
fah = cel * 9 / 5 + 32
print("The tempature in Fahreheit is ".format(fah))

Step 6 – Save and Run the program


Save your program and we need to run the program.  To run the program we will go into our terminal or command prompt and cd into our desktop and type python3 ctof.py and press return.


#Save and Run the program

Thomass-MBP:desktop Tommy$ python3 ctof.py
Convert Celcius to Fahreheit
Input tempature in celcius. 22
The tempature in Fahreheit is 71.6

Conclusion 


In this Python tutorial, we built a very simple program that will convert celsius to fahrenheit and return the value to the user. If you have any questions please leave a comment below and we will help you out.


 


Source Code

Thursday, May 7, 2015

Multiplication In Python - Python Numbers - Python Tutorial

Multiplication in PythonMultiplication In Python


In this Python tutorial, we will be focusing on multiplication in Python. Multiplication is another simple concept in the Python programming language. We use the asterisk(*) to indicate multiplication.


Examples of Multiplication In Python


#Examples of Multiplication In Python

#Integers
>>> 7 * 8
56
>>> 33 * 98
3234
>>> 44 * 7
308
>>> 23 * -4
-92

#floats
>>> 7.8 * 9.5
74.1
>>> 8.65 * 7.34
63.491
>>> 67.23 * 56.8
3818.664

Multiplication is so straightforward in Python that there is not much to it.  I suggest you play around with multiplication if you run in to float that not returning like they should then go ahead and try to fix them using round() or decimal module.


If you have any questions about multiplication in Python leave a question in our Q & A section of our website.



Wednesday, May 6, 2015

How do I switch into a directory that has a space - Python Tutorial

I am trying to switch into my “Python tutorials” but using cd will not allow me to switch.  Is there a way to do this?  Do I have to change the name?