Data Types in Python:
Every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.
The
data
stored in memory can be of many types. For example, a person's age is stored as
a numeric value and his or her address is stored as alphanumeric characters.
Python has various standard data types
that are used to define the operations possible on them and the storage method
for each of them.
Python has five
standard data types −
•Python Numbers
•Python String
•Python List
•Python Tuple
•Python Dictionary
1. Python Numbers:
Integers, Floating point numbers and Complex numbers falls under Python Numbers Category.
They are defined as
int
, float
and complex
class in Python.
Number objects are created when you assign a value to them.
For example :
var1 = 1
var2 = 7
We can delete the reference to a number object by using the del statement.
The syntax of the del statement is :
del var1[,var2[,var3[....,varN]]]]
We can use the
type()
function to know which class a variable or a value belongs to and the isinstance()
function to check if an object belongs to a particular class.
Example Program:
1. a = 5
2. print(a, "is of type", type(a))
3.
4. a = 2.0
5. print(a, "is of type", type(a))
6.
7. a = 1+2j
8. print(a, "is complex number?", isinstance(1+2j,complex))
Output:
5, is of type , Integer
2.0, is of type , floating point
1+2j, is complex number?, true
Number Type Conversion:
Python converts numbers internally in an expression containing mixed types to a common type for evaluation.
1. Type int(x) to convert x to a plain integer.
2. Type long(x) to convert x to a long integer.
3. Type float(x) to convert x to a floating-point number.
4. Type complex(x) to convert x to a complex number with real part x and imaginary part zero.
5. Type complex(x, y) to convert x and y to a complex number with real part x and imaginary part y. x and y are numeric expressions.
2. Python Strings:
Strings
in Python are identified as a contiguous set of
characters represented in the
quotation marks.
Multi-line strings can be denoted using triple quotes '''or
"""
.
>>> x = "This is first string."
>>> s = ' ' ' a multiple line
Python allows for either pairs of single or double quotes.
Subsets of strings can be taken using the slice operator ([ ] and
[:] ) with
indexes starting at 0 in the beginning of the string
and working their way from
-1 at the end.
The
plus (+) sign is the string concatenation operator and the
asterisk (*) is the
repetition operator.
For example:
str =
'Hello World!'
1. print
(str) # Prints complete string
2. print
(str[0]) # Prints first character of the string
3. print
(str[2:5]) # Prints characters starting from 3rd to
5th
4. print
(str[2:]) # Prints string starting from 3rd
character
5. print
(str *
2) # Prints string two times
6. print
(str +
"TEST") # Prints concatenated string
This will produce the following result −
1. Hello
World!
2. H
3. llo
4. llo
World!
5. Hello
World!Hello
World!
6. Hello
World!TEST
Python String Methods |
---|
capitalize() - Returns the string with first letter capitalized and the rest lowercased. |
casefold() - Returns a lowercase string, generally used for caseless matching. This is more aggressive than the lower() method. |
center() - Center the string within the specified width with optional fill character. |
count() - Count the non-overlapping occurrence of supplied substring in the string. |
encode() - Return the encoded version of the string as a bytes object. |
endswith() - Returns ture if the string ends with the supplied substring. |
expandtabs() - Return a string where all the tab characters are replaced by the supplied number of spaces. |
find() - Return the index of the first occurrence of supplied substring in the string. Return -1 if not found. |
format() - Format the given string. |
format_map() - Format the given string. |
index() - Return the index of the first occurrence of supplied substring in the string. Raise ValueError if not found. |
isalnum() - Return true if the string is non-empty and all characters are alphanumeric. |
isalpha() - Return true if the string is non-empty and all characters are alphabetic. |
isdecimal() - Return true if the string is non-empty and all characters are decimal characters. |
isdigit() - Return true if the string is non-empty and all characters are digits. |
isidentifier() - Return true if the string is a valid identifier. |
islower() - Return true if the string has all lowercased characters and at least one is cased character. |
isnumeric() - Return true if the string is non-empty and all characters are numeric. |
isprintable() - Return true if the string is empty or all characters are printable. |
isspace() - Return true if the string is non-empty and all characters are whitespaces. |
istitle() - Return true if the string is non-empty and titlecased. |
isupper() - Return true if the string has all uppercased characters and at least one is cased character. |
join() - Concatenate strings in the provided iterable with separator between them being the string providing this method. |
ljust() - Left justify the string in the provided width with optional fill characters. |
lower() - Return a copy of all lowercased string. |
lstrip() - Return a string with provided leading characters removed. |
maketrans() - Return a translation table. |
partition() - Partition the string at first occurrence of substring (separator) and return a 3-tuple with part before separator, the separator and part after separator. |
replace() - Replace all old substrings with new substrings. |
rfind() - Return the index of the last occurrence of supplied substring in the string. Return -1 if not found. |
rindex() - Return the index of the last occurrence of supplied substring in the string. Raise ValueError if not found. |
rjust() - Right justify the string in the provided width with optional fill characters. |
rpartition() - Partition the string at last occurrence of substring (separator) and return a 3-tuple with part before separator, the separator and part after separator. |
rsplit() - Return a list of words delimited by the provided subtring. If maximum number of split is specified, it is done from the right. |
rstrip() - Return a string with provided trailing characters removed. |
split() - Return a list of words delimited by the provided subtring. If maximum number of split is specified, it is done from the left. |
splitlines() - Return a list of lines in the string. |
startswith() - Return true if the string starts with the provided substring. |
strip() - Return a string with provided leading and trailing characters removed. |
swapcase() - Return a string with lowercase characters converted to uppercase and vice versa. |
title() - Return a title (first character of each word capitalized, others lowercased) cased string. |
translate() - Return a copy of string that has been mapped according to the provided map. |
upper() - Return a copy of all uppercased string. |
zfill() - Return a numeric string left filled with zeros in the provided width. |
No comments:
Post a Comment