0% found this document useful (0 votes)
7 views7 pages

Lecture 3-4

The document provides an overview of Python data types, including standard types such as Number, String, List, Tuple, and Dictionary. It explains the dynamic typing nature of Python, how to check data types using the type() function, and details about numeric types, string handling, and built-in functions for both. Additionally, it includes examples and descriptions of various operations that can be performed on these data types.

Uploaded by

jeenamukesh33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Lecture 3-4

The document provides an overview of Python data types, including standard types such as Number, String, List, Tuple, and Dictionary. It explains the dynamic typing nature of Python, how to check data types using the type() function, and details about numeric types, string handling, and built-in functions for both. Additionally, it includes examples and descriptions of various operations that can be performed on these data types.

Uploaded by

jeenamukesh33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Data Types

Python data types are the classification or categorization of data that a variable is
holding. It determines what operations can be performed on that data.

Since python is a dynamically typed language, we do not need to define the type of
the variable while declaring it. Interpreter implicitly binds the value with its respective
type.

Python also allows us to check the data type of a variable using type() function which
returns the data type of the variable passed.

Example:

a = 16
b = “GautamKumar”
c = 147.59
print(type(a)) #<type ‘int’>
print(type(b)) #<type ‘str’>
print(type(c)) #<type ‘float’>

Standard Data Types


Python provides various standard data types which are as follows:

1. Number
2. String
3. List
4. Tuple
5. Dictionary

Let’s have a brief introduction of these data types. We will learn these data types in
more details later.

Number Data Type


Number data type is used to store numeric values. Python creates Number objects
when we assign a numeric value to a variable.
Example:

num1 = 17
num2 = 121

We can delete a single or multiple objects by using the del statement.

Example:

del num1 #deletes single object


del num2, num3 #deletes multiple objects

In python, integers can be of any length but a floating point number is accurately
only up to 15 decimal places.

Python can also express Binary, Octal and Hexadecimal numbers.

Binary numbers have base 2 and are represented with 0b or 0B as prefix before the
number.

Octal numbers have base 8 and are represented with 0o or 0O as prefix before the
number.

Hexadecimal numbers have base 16 and are represented with 0x or 0X as prefix


before the number.

Python supports the following 4 types of numeric data,

1. int: used to store signed integers like 21, 452 etc.

2. long: used to store higher range of integer values like 123456789L, 987654321L
etc.

3. float: used to store floating point numbers like 15.2, 756.98 etc.

4. complex: used to store complex numbers like 5+2j, 21-13j etc.


We can use the type() function to know which class a variable or value belongs to
and isinstance() function to check if it belongs to a particular class.

We can use python built-in functions to convert between types explicitly. These
functions can even convert from strings.

1. int(x) is used to convert a number to a plain integer.


2. long(x) is used to convert a number to a long integer.
3. float(x) is used to convert a number to a floating point number.
4. complex(x) is used to convert a number to a complex number with real part x and
imaginary part 0.
5. complex(x,y) is used to convert a number to a complex number with real part x
and imaginary part y.
Python built-in number functions
Function Name Description
abs(x) Returns the absolute value of x.
ceil(x) Returns the ceil value of x.
floor(x) Returns the floor of x.
cmp(x,y) Returns -1 if x < y, 0 if x == y or 1 if x > y.
exp(x) abcReturns the exponent of x (ex).
log(x) Returns the natural logarithm of x.
max() Returns the maximum value.
min() Returns the minimum value.
pow(x,y) Returns the value of x**y.
sqrt(x) Returns the square root of x.
sin(x) Returns the sine of x radians.
cos(x) Returns the cosine of x radians.
tan(x) Returns the tangent of x radians.
degrees(x) Converts angle x from radians to degrees.
radians(x) Converts angle x from degrees to radians.
random() Returns the random float number greater than or equals to
0 and less than 1.
randrange([start,] stop Returns a randomly selected number from range (start,
[,step]) stop, step).
String Data Type
A string is an ordered set of characters represented either in single or double quotes.
A string can contain alphabets, digits and special characters.

Python doesn’t support character data type instead a single character is considered
as a string of length 1.

The indexing of a string with length n starts from 0 to n-1.

An escape or non-printable character is represented with backslash (\) in strings.

String handling in python is a simple and easy task as we have various built-in
functions and operators.

In python, the operator + (plus sign) is a concatenation operator and the operator *
(asterisk) is the repetition operator.

We can print the subset of a string by using the slice operator ( [ ] or [ : ] ) with index
starting at 0.

Example

str1 = “Gautam”
str2 = “Kumar”
print str1 # Prints complete string
print str1[5] # Prints 5th character of the string
print str1[2:4] # Prints characters starting from 2nd
to 5th
print str1[4: ] # Prints complete string starting from
4th character
print str2 * 3 # Prints string 2 times
print str1 + str2 # Prints concatenation of str1 and str2

Output:

Gautam
m
utam
am
KumarKumar
GautamKumar
We can update the whole string but can’t update a particular character within a
string.

Example:

myStr = “Gautam”
myStr[1] = “A” # An error will occur as a particular
character can’t be updated
myStr = “Kumar” # whole string can be updated
print(myStr) # prints Kumar

Python built-in string functions

Function name Description

capitalize() Returns a copy of the string only with its


first character capitalized.
count(string,begin,end) Returns the number of occurrences of a
substring in a string.
endswith(suffix, beg=0, end=len(string) Returns a boolean value if the string
terminates with given suffix between begin
and end.
expandtabs(tabsize=8) Returns a copy of the string in which tab
characters i.e. '\t' are expanded using
spaces, optionally using the given tabsize
(default 8).
find(substring, beginIndex, endIndex) Returns the index value of the string where
substring is found between begin index
and end index.
format(value) Returns a formatted version of S using the
passed value.
isalnum() Returns true if string has at least 1
character and all characters are
alphanumeric and false otherwise.
isalpha() Returns true if string has at least 1
character and all characters are alphabetic
and false otherwise.
isdigit() Returns true if string contains only digits
and false otherwise.
islower() Returns true if string has at least 1
character and all characters are in
lowercase and false otherwise.
isupper() Returns true if string has at least 1
character and all characters are in
uppercase and false otherwise.
isnumeric() Returns true if a string contains only
numeric characters and false otherwise.
isdecimal() Returns true if all the characters of the
string are decimals otherwise false.
isspace() Returns true if string contains only
whitespace characters and false otherwise.
istitle() Returns true if the string is titled properly
and false otherwise. A title string is the one
in which the first character is uppercase and
other characters are lowercase.
isidentifier() Returns true if the string is a valid identifier
otherwise false.
join(seq) Returns a string which is the concatenation
of the strings in the sequence seq.
len(string) Returns the length of the string.
lower() Returns a copy of the string in which all
characters have been lowercased.
upper() Returns a copy of the string in which all
characters have been uppercased.
lstrip() Returns a copy of the string in which all
chars have been stripped from the
beginning of the string.
replace(old, new [,count]) Returns a copy of the string in which the
occurrences of old have been replaced with
new, optionally restricting the number of
replacements to count.
rfind(str, beg=0,end=len(string)) Same as find(), but search backwards in
string.
rindex( str, beg=0, end=len(string)) Same as index(), but search backwards in
string.
rjust(width,[, fillchar]) Returns a space-padded string with the
original string right-justified to a total of
width columns.
rstrip() Returns a copy of the string in which all
chars have been stripped from the end of
the string (default whitespace characters).
split(str="", num=string.count(str)) Returns a list of all the words in the string,
using str as the separator (splits on all
whitespace if left unspecified), optionally
limiting the number of splits to num.
startswith(str, beg=0,end=len(string)) Returns true if string starts with str
otherwise false.
splitlines( num=string.count('\n')) Returns a list with all the lines in string,
optionally including the line breaks, if num
is supplied.
strip([chars]) Returns a copy of the string in which all
chars have been stripped from the
beginning and the end of the string.
swapcase() Returns a copy of the string in which all the
case-based characters have had their case
swapped.
title() Returns a copy of the string in which first
characters of all the words are capitalized.
translate(table, deletechars=””) Returns a copy of the string in which all
characters have been translated using table,
optionally deleting all characters found in
the string deletechars.
zfill(width) Returns original string leftpadded with
zeros to a total of width characters.

You might also like