Friday, May 8, 2015

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?



Subtraction Program In Python - Python Programs - Python Tutorial

subtraction program in PythonSubtraction Program in Python


In this Python programming, tutorial we are going to create a simple Subtraction Program in Python. In this program, we are going to add the ability to use floats not integers.


Functions and Methods Used in This Program


  • input()

  • float()

  • print()

  • .format()

  • round()

Step 1 – Open Your Text Editor


Open your text editor in this tutorial we use Sublime Text 3


Step 2 – Save your file


Save your file by pressing command + s or control + s.  Name your file subtraction.py and save to your desktop.


Step 3 – Create Welcome Print Statement


We want to welcome our users to our program so we will add a simple print statement doing so.


#Create Welcome Print Statement

print("Welcome to our subtraction program")

Step 4 - Get the users first number


In this step, we will get the users number using input() and then convert that number to a floating point number using float(). Then we will assign the value to a variable called firstnum.


#Get the users first number

print("Welcome to our subtraction program")
firstnum = float(input("What is your first number. "))

Step 5 – Get the users second number


This step is similar to step 4 with the only difference being we are going to assign the value to the user gives to a new variable called secondnum.


#Get the users second number

print("Welcome to our subtraction program")
firstnum = float(input("What is your first number. "))
secondnum = float(input("What is your second number. "))

Step 6 – Get Users Difference


This step we will get the users difference between two numbers and round the number to two numbers after the decimal and save the difference to a variable called thesum.


#Get Users Difference

print("Welcome to our subtraction program")
firstnum = float(input("What is your first number. "))
secondnum = float(input("What is your second number. "))
thesum = round(firstnum - secondnum, 2)

Step 7 – Print The Difference Back to User


This is our final line of code in our little program here.  We want to print the difference back to the user so to do that we use a print() statement and format the string print backs the value within thesum.


#Print The Difference Back to User

print("Welcome to our subtraction program")
firstnum = float(input("What is your first number. "))
secondnum = float(input("What is your second number. "))
thesum = round(firstnum - secondnum, 2)
print("Your difference is ".format(thesum))

Step 8 – Run The Program


Open your terminal or command prompt.  Change into your desktop and run the program by typing python3 subtraction.py


#Run The Program

Thomass-MBP:~ Tommy$ cd desktop
Thomass-MBP:desktop Tommy$ python3 subtraction.py
Welcome to our subtraction program
What is your first number. 4
What is your second number. 5
Your difference is -1.0

Now that you have seen the way to build a simple subtraction program in Python with floating point numbers. Play with the program try to improve it and make your own modifications. If you can break let us know so we can do a tutorial how to improve the subtraction program.  If you have any questions leave a comment below and we will help you.



Tuesday, May 5, 2015

Subtraction in Python - Python Numbers - Python Tutorial

Subtraction in PythonSubtraction in Python


In the previous tutorial, we covered addition which was a very simple tutorial. In this tutorial, we will look at subtraction in Python which is another very simple subject. Subtraction works as expect in Python as simple as it was in grade school.

To perform subtraction, in Python we use the minus(-) symbol. In this tutorial, we will be doing all our code in the Python Interpreter. To follow along open your Python interpreter and try some of the examples below.


Integer Subtraction In Python


The following are just basic examples of subtracting Python integers.


#Integer Subtraction in Python

>>> 8 - 4
4
>>> 345 - 67
278

Float Subtraction in Python


The following are just basic examples of subtracting Python floats or floating point numbers.


#Float Subtraction in Python

#simple float subtraction
>>> 6.7 -98
-91.3
>>> 67.43 - 98.76
-31.33

#fixing float accuracy
>>> 8.8 - 1.5
7.300000000000001

#default round function no numbers after the decimal
>>> round(8.8 - 1.5)
7

#fix for one number after the decimal
>>> round(8.8 - 1.5, 1)
7.3

Now that you have seen how easy subtraction in Python you can now simply add and subtract numbers using your Python code.  If you have any questions about subtraction in Python leave a comment below and we will do our best to help you out.



Addition Program in Python - Python Tutorial

Addition Program in PythonAddition Program in Python


In this tutorial, we are about build a very simple addition program in Python. We will show how to build a simple program that will add two integer numbers that a user gives us via the terminal or command prompt and then we will return the sum of those two numbers to the user. The program will be accessible to download at the end of the tutorial. Just try to follow along the more you practice writing programs in Python the more you will learn.


Building the Addition Python Program


We will be using sublime text as our text editor to build this program. We will be writing our code using Python 3 syntax.  We will run the code via our terminal or command prompt.


Step 1 - Open Text Editor


Open your text editor so we can write the code for our program.


Step 2 - Save your program


Save your program to your desktop as addition.py


Step 3 - Write Welcome Message For Our Program


Here we will use a simple print statement to welcome our users to our program.


#Write Welcome Message For Our Program

print("Welcome to our addition program")

Step 4 - Get Users First Number


In this step, we will get the users first number using input() and then convert the number to an integer using int() and assign a variable to the object that holds the users first number.


#Get Users First Number

print("Welcome to our addition program")
firstnum = int(input("Give us your first number. "))

Step 5 – Get Users Second Number


In this step, we do the same for the previous step above but we give this user input a different variable.


#Get Users Second Number

print("Welcome to our addition program")
firstnum = int(input("Give us your first number. "))
secondnum = int(input("Give us your second number. "))

Step 6 – Perform The Equation


In this step, we will add the users two numbers together and then assign the sum a variable.


#Perform The Equation

print("Welcome to our addition program")
firstnum = int(input("Give us your first number. "))
secondnum = int(input("Give us your second number. "))
thesum = firstnum + secondnum

Step 7 – Return The Sum To The User


We will return the sum of the user. We will print a string to the user and format the string using a format() method to add our sum into the string.


#Return The Sum To The User

print("Welcome to our addition program")
firstnum = int(input("Give us your first number. "))
secondnum = int(input("Give us your second number. "))
thesum = firstnum + secondnum
print("Your sum is ".format(thesum))

Step 8 – Run The Program


Open your terminal or command prompt and change into your desktop. To run the program, simply type python3 addition.py and press return or enter. The program will ask you to input a number then press return. The program will again prompt you to add your second number again input your number then press return. Now the program will return your sum to you. This is a very simple program in Python.


If you have any questions about Addition Program in Python please leave a comment below and we will help you out.


Get Source Code From Git

 



Monday, May 4, 2015

Addition in Python - Python Numbers - Python Tutorial

Addition in PythonAddition in Python


We are getting back into the basics in this Python tutorial. In the first three tutorials in this chapter, on Python numbers we focussed on floating point numbers and decimal numbers. Our plan was to start out very basic with addition but we had some questions about the accuracy of floating point numbers which made us push back this tutorial on addition in Python.


Addition in Python is very simple and you should not  have any issues with addition in Python. To add numbers we use the plus(+) symbol to add the numbers together. The plus symbol is regarded as an arithmetic operator in the Python programming language.


Example of Addition in Python


In these examples, we will be using our Python interpreter. The interpreter is a very useful tool when it comes to working with math and numbers.


Adding Integers 


#Adding Integers in Python

>>> 2 + 2
4
>>> 33 + 22
55
>>> 67 + 32
99
>>> 55 + 22
77
>>> 19 + 8978
8997

#adding positive integer to negative integer
>>> 340000 + -40000
300000

Adding Floats


We will run into some issues adding floats, but we have learned in our previous tutorials how to fix these issues. When going through this these examples use the skills that you have learned in the previous Python tutorials and fix these numbers.


#Adding Floats

#Fix this one
>>> 5.6 + 87.3
92.89999999999999

#If you add an integer and a float together we will get a float back
>>> 76.5 + 65
141.5

#fix this one as well
>>> 678.34 + 34.21
712.5500000000001

#adding a positive float to a negative float

>>> 75.3232 + -2.3
73.0232

############################
Answers to above problems below
############################
>>> round(5.6 + 87.3, 1)
92.9
>>> round(678.34 + 34.21, 2)
712.55

This is a very short tutorial and the reason is that addition in Python is so simple that we really did not have much more to show you. Just remember with addition and floats you can run into some weird returns so either use the built-in function round() or the decimal module.


Build An Addition Program

 


If you have any questions please leave a comment below.