Python Basic Data Types
Python Basic Data Types
Integers(Signed)
Boolean
Integers
Output:
Enter the value of a: 45
Enter the value of b:
67
The sum of two
integers= 112
Floating Point Numbers
A number having fractional part is a floating point
number. It has a decimal point. It is written in two forms :
i) Fractional Form : Normal decimal notation e.g. 675.456
ii) Exponent Notation: It has mantissa and
exponent. e.g. 6.75456E2
Advantage of Floating point numbers:
They can represent values between the integers.
They can represent a much greater range of values.
Disadvantage of Floating point numbers:
Floating-point operations are usually slower than
integer operations.
Demonstration of Floating Point Data
Type
#Demonstration of Float Number- Calculate Simple Interest
princ=float(input("Enter the Principal Amount:"))
rate=float(input("Enter the Rate of interest:"))
time=float(input("Enter the Time period:"))
si=(princ*rate*time)/100
print("The Simple Interest=",si)
Output:
Enter the Principal
Amount:5000 Enter the Rate of
interest:8.5 Enter the Time
period:5.5 Simple Interest=
2337.5
Strings
A String is a group of valid characters enclosed in Single or
Double quotation marks. A string can group any type of
known characters i.e. letters ,numbers and special characters.
A Python string is a sequence of characters and each
character can be accessed by its index either by forward
indexing or by backward indexing.
e.g. subj=“Computer”
Forwar d indexing 0 1 2 3 4 5 6 7
Subj C o m p u t e r
-8 -7 -6 -5 -4 -3 -2 -1 B
ackward indexing
Demonstration of String Data Type
#Demonstration of String- To input string & print
it my_name=input("What is your Name? :")
print("Greetings!!!")
print("Hello!",my_name)
print("How do you
do?")
Output :
What is your Name? :Ananya
Greetings!!!
Hello! Ananya
How do you do?
Functions
Method Description
capitalize() Converts the first character to upper case
endswith() Returns true if the string ends with the specified value
find() Searches the string for a specified value and returns the
position of where it was found
Method Description
format() Formats specified values in a string
index() Searches the string for a specified value and returns the
position of where it was found
isupper() Returns True if all characters in the string are upper case
find() Searches the string for a specified value and returns the
position of where it was found
To accept a number and change it into
Upper case
txt = "Hello my friends"
x = txt.upper()
print(x)
Print EVEN length words of a string
# declare, assign string
str = "Python is a programming language"
# extract words in list
words = list(str.split(' '))
# print string
print("str: ", str)
# print list converted string i.e. list of words
print("list converted string: ", words)
# iterate words, get length
# if length is EVEN print word
print("EVEN length words:")
for w in words: str: Python is a programming language
if(len(w)%2==0 ): list converted string: ['Python', 'is', 'a', 'programming', 'language']
EVEN length words:
print(w)
Python
is
language
Print words with their length of a string
def splitString (str):
# split the string by spaces
str = str.split (' ')
# iterate words in string
for words in str:
print(words," (", len (words), ")")
# Main code
# declare string and assign value
Hello ( 5 )
str = "Hello World How are you?"
World ( 5 )
How ( 3 )
# call the function are ( 3 )
splitString(str) you? ( 4 )
Complex Number
Python represents complex numbers in the form a+bj.
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
Program to Join two lists
Output:
Enter Elements for List 1:[12,78,45,30]
Enter Elements for List 2:[80,50,56,77,95]
List 1 : [12, 78, 45, 30]
List 2 : [80, 50, 56, 77, 95]
Joined List : [12, 78, 45, 30, 80, 50, 56, 77, 95]
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(5,10))
[5, 6, 7, 8, 9]
>>> list(range(5, 10, 3))
[5, 8]
Python program to sum all the items in a list.
cars.sort()