Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Day 5 Python 2 & Python 3 (User Input)

The document discusses the differences between input handling in Python 2.x vs Python 3.x. In Python 2.x, input() takes any data type while raw_input() only takes strings. In Python 3.x, input() always returns a string, so type casting is needed. The document also provides examples of getting integer and float user input in both versions followed by code snippets demonstrating simple programs like calculating area of shapes, finding maximum of two numbers, and determining if a number is even or odd.

Uploaded by

Kumar Sujit
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Day 5 Python 2 & Python 3 (User Input)

The document discusses the differences between input handling in Python 2.x vs Python 3.x. In Python 2.x, input() takes any data type while raw_input() only takes strings. In Python 3.x, input() always returns a string, so type casting is needed. The document also provides examples of getting integer and float user input in both versions followed by code snippets demonstrating simple programs like calculating area of shapes, finding maximum of two numbers, and determining if a number is even or odd.

Uploaded by

Kumar Sujit
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

There are two major versions

2.x & 3.x available.


Python 2x (upto 2.7) & Python3x (after 2.7)
Python2 :
---------
user input (Python 2x):
-------------------------
input() ----> It take all number types of data
raw_input() ----> It take string type of data

Ex:
regno = input("Enter reg no:")
name = raw_input("Enter name:")
math = input("Enter math marks")

print "regno:",regno
print "name:",name
print "math mark:",math

print type (regno)


print type (name)
print type (math)

Python3:
--------

input() ---> Bydefault it take string class

Ex:
regno = int(input("Enter reg no:"))
name = input("Enter name:")
math = float(input("Enter math marks"))

print ("regno:",regno)
print ("name:",name)
print ("math mark:",math)

print (type (regno))


print (type (name))
print (type (math))

Lab1
1. Write a program to add two number
Ans
no1 = int (input ("Enter 1st number"))
no2 = int (input ("Enter 2nd number"))
res = no1 + no2
print ("sum of two number =", res )

2. Write a program to find area of circle


Ans
r = float (input ("Enter radious of circle"))
pi = 3.14
area = pi * r* r
printf ("area of circle =", area)

Lab 2 :
1. Write a pogram to find simple interest where principal amount , rate of
interest and time are given by user
Ans
principalamount = float (input ("Enter principal amount "))
rate = float (input ("enter rate of interest "))
time = float (input ("enter time period"))
i = (principalamount * time * rate)/ 100
totalamount = i + principalamount
print (" interest=", i)
print ("total amount =", totalamount)

2. Write a program to find area of triangle where three sides are given by user .
Ans
from math import sqrt
s1= float (input ("Enter side 1 value "))
s2= float (input ("Enter side 2 value "))
s3= float (input ("Enter side 3 value "))
s=(s1+s2+s3)/ 2;
area = sqrt (s * (s- s1) * (s-s2) * (s-s3))
print ("area of circle =", area)

3. Write a program to find max value from two number where two number are given by
user .
Ans
no1 = int (input ("Enter 1st number"))
no2 = int (input ("Enter 2nd number"))
if no1 > no2 :
max = no1
else :
max = no2
print ("max value =", max )

4. Write a program to chcek a number is even or odd.


Ans
no = int (input ("Enter any number"))
if no % 2 == 0 :
print (no, "is even")
else :
print (no, "is odd")

You might also like