Basic Data Types input and formatted output
Nihar Ranjan Roy
[email protected], [email protected] Department of Computer Science and Engineering
GD Goenka University, Gurugram
Outline
1 Core data types
2 int type
3 float type
4 complex type
5 boolean type
6 string type
7 print() function
8 input() function
Core data types
Python Core Data Type
Core data types supported by Python are:
1 integer
2 float
3 string
4 complex
int type
Integer number
Combination of positive and negative numbers including zero(0) Example: 10, -20.
Integer literals can be octal or hexadecimal in format.
Octal numbers start with0o i.e. a zero followed by lower or upper
case letter ‘O’ followed by a sequence of digits from 0 to 7
Hexadecimal numbers start with0x i.e. a zero followed by lower or
upper case letter ‘X’ followed by a sequence of digits.
int function- converts a string or a number into a whole number to integer. It removes anything after the decimal point
Example:
float type
Floating point number
It consists of whole number, decimal point and fractional part. Example 3.141
A floating point number can be written in decimal notation or scientific notation Example:
3.73e1 is equivalent to 37.0
float function: Converts a string into floating point number Example: float(’12.12’)
complex type
Complex numbers
A complex number is a number that can be expressed in the form ‘a+bj’, where a and b are real numbers and ‘j’ is an imaginary unit
Example: 2+4j
type(2+4j)
<class ‘complex’>
type(4j)
<class ‘complex’>
boolean type
Boolean Type
In Python Boolean data type is represented as bool.
It can have one of the two valuesTrue or False.
Generally boolean type is used to compare two variables using relational operators.
Example:>>>5 == 5 True
Example:>>>5>8 False
The bool type is a subclass of the int type and True and False are its only instances:
string type
String Type I
Strings in python can be enclosed in single quotes (’...’) or double quotes (”...”) with the same result.
name=’nihar’ name=“nihar”
name=“““The quick brown fox jumps over the little lazy dog.The quick brown fox jumps over the little lazy dog.”””
strfunction: it is used to convert a number into string
>>> str(12.5)
String concatenation operator (+)
string type
String Type II
In the interactive interpreter, the output string is enclosed in quotes and special characters are escaped with backslashes.
print() produces a more readable version of the string.
string type
String Type III
string type
String Type IV
Strings can be indexed (subscripted).
There is no separate character type
print() function
print() function
print()function is used to print/ display contents on the screen.
Syntax for print() function is print(arguments)
The argument of the print function can be a value of any type int,str,float etc.
print() function with an end argument
print("Nihar")
print("Ranjan")
print("Roy")
input() function
input function
The input() function is used to accept input string from the user. The input value can be assigned to a variable.
Syntax
variable-name=input()
variable-name=input(”Put-your-message-here”)
Example:
name=input("Please enter your name")
print("Hello ",name," Welcome to the world
of python programming")\\
Output:\\
input() function
input other than string
How to accept an integer number from the user?
No1=int(input("Please enter a number"))
print("Square of ",No1," is =", No1*No1)
Output:\\
input() function
Problem
input() function
Solution: Area of circle
#program for calculating area of circle
radius=int(input("Please enter radius of the circle: ")) PI=3.141
area=PI*radius*radius
print("Area of circle is ",area)
Output:
Please enter radius of the circle: 5 Area of circle is 78.525
Note:Modify this program such that we get only 2 digits after decimal
Format specifier
Format specifier I
format(item, format-specifier)
item is the number or string to be formatted
format-specifier is a string that specifies how the item is formatted
print(”Area of circle is ”,format(area,’.2f’) Output: Please enter radius of the circle: 5
Format specifier
Format specifier II
format(10.45678,v
>
10
.
2f
v)
Fieldnwidth
nPrecision
ConversionnCode
Fieldnwidth
nPrecision
ConversionnCode
Justificationn(right)
Some of the popular conversion codes are:
‘s’ is for string
‘d’ is for decimal numbers
Modules in Python
Modules in Python
A module allows you to logically organize your Python code.
A module is a file consisting of Python code,can define functions, classes and variables.
#demonstrate usage of module
# calculate factorial using inbuilt function
import math
no=input("Enter the no ")
print("Factorial is "+str(math.factorial(int(no))))
You can choose to import only parts from a module, by using the from keyword.
from math import pi
Math module
Some inbuilt functions from math module
In order to use these functions import math
math.ceil(10.45) gives 11
math.floor(10.45) gives 10
math.exp(1) gives 2.718281.... its value ofex
math.sin(x) returns sin of x where x is in radians math.degrees(x) converts x from radians to degrees
math.radians(x) converts x from degree to radians
Math module
Problem
Math module
Solution: Hypotenuse of a right angled triangle
#program for calculating Hypotenuse of a right angle triangle
import math # Import math module
base=int(input("Please enter the base: ")) height=int(input("Please enter the height: ")) hypo=math.sqrt(base*base+height*height)
print("Hypontenuse is=",hypo)
Output:
Exercise
Practice Exercises
1. What will be printed if we write print() statement as
PriNt(”Hello Mr. Python”)
a. Hello Mr. Python
b. Syntax Error
c. Name Error
d. Both 1 and 2
2. Which of the following is a valid input statement?
a. x=input(Enter number:)
b. X=Input(Enter number:)
c. X=input(’Enter number:’)
d. X=Input(’Enter number:’)