Friday, March 27, 2015

Python Tutorial: Python Comparison Operators - Python Numbers #37

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.



Thursday, March 26, 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.



Python Tutorial: Booleans in Python - Python Numbers #36

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

 



Booleans In Python - Python Numbers - Python Tutorial

booleans in pythonBooleans In Python


In this Python tutorial, we are going to look at booleans in Python. Booleans are sometimes called truth statements in programming. Booleans are returned only a True or False.  You may be asking why we are covering booleans in the numbers section our Python tutorial. Booleans are indeed part of the integer family. A True boolean is equal to 1 and a False boolean is equal to 0.


Boolean Examples in Python


We will be checking out examples of booleans. Some of the methods, operators or functions may have not been covered yet in our tutorial series.  Just follow along we are going to get to all these very shortly. All these examples will be performed in our Python interpreter.


#boolean Examples in Python

#Boolean Operations
>>> False or True
True
>>> True or False
True
>>> False or False
False
>>> True or True
True
>>> True and False
False
>>> True and True
True
>>> False and False
False
>>> not False
True

#Comparison Operators
>>> 7 < 9
True
>>> 8 > 8
False
>>> 8 != 9
True
>>> 8 == 8
True
>>> 8 <= 9
True


Proof That Booleans are Integers


#Proof That Booleans are Integers

>>> True + False
1
>>> True * False
0
>>> True ** 8
1
>>> type(True)
<class 'bool'>
>>> a = True + False
>>> type(a)
<class 'int'>

We will do a lot of work with Python booleans over the next couple of Python tutorials. If you have any questions about booleans in Python leave, a comment below so we can help you.


 



str() - Python Tutorial

str() built-in function



 


str() Built-in Function


str() is a built-in function in Python that will convert an other type like a integer, float, list, tuple and so on to a string.


Str() Syntax


syntax(argument)


The argument can be a integer, floating point number, list, tuple, dictionary, file, and so on.  The argument will be converted to a string.


str() Built-in Function Examples


#Str() Built-in Function

>>> str(6)
'6'
>>> str(7.6)
'7.6'
>>> str((6,75,9))
'(6, 75, 9)'
>>> str('cat': 'mary', 'dog': 'maggie')
"'cat': 'mary', 'dog': 'maggie'"

If you have any questions about the str() built-in function leave a comment below and we will do our best to assist you.