This blog assumes that you have a Linux machine with Python already installed.
If you don't, then you can use an AWS cloud based image with Python. For guidance, refer to my blog.
I am using Python 2.7 for this tutorial.
USER INPUT
What if you want the user of the program to input data.
Use the following.
raw_input()
NOTE: Python 3.0 supports input() in place of raw_input()
NOTE: Python 3.0 supports input() in place of raw_input()
Example-Program2:
*************************************************************************************
#! /bin/python # This is where your python is. Change this if the path differs on your computer
input1 = raw_input("Enter input1:")
input2 = raw_input("Enter input2:")
print "\n Input1 = ", input1
print "\n Input2 = ", input2
print "\n Input1 + Input2 = ", input1+input2
********************************************************************
Enter two integers as inputs and run this program. What is the output of the last line?
Note: raw_input() always reads the input as a string.
If you need a specific data type, you need to type cast.
TYPE CASTING
Try the following program.
Example-Program3:
*********************************************************************
#! /bin/python # This is where your python is. Change this if the path differs on your computer
int1 = raw_input("Enter integer1:")
int1 = int (int1)
int2 = int(raw_input("Enter integer2:"))
print "\n int1 = ", int1
print "\n int2 = ", int2
print "\n int1 + int2 = ", int1+int2
**********************************************************************
EXERCISE-1
Write a program that
1. Reads two integers and two floats from the user.
2. Prints the difference between the two integers and floats.
3. Type casts integers to floats and prints the sum of those.
No comments:
Post a Comment