SlideShare a Scribd company logo
Akhil Kaushik
Asstt. Prof., CE Deptt.,
TIT Bhiwani
Data Types in Python
Python Introduction
• Python is a dynamic-typed language (uses interpreter).
• Many other languages are static typed, such as C/C++
and Java (use compiler).
• A static typed language requires the programmer to
explicitly tell the computer what type of “thing” each
data value is.
Python Introduction
• In Python, you simply give your variables names
and assign values to them.
• The interpreter takes care of keeping track of what
kinds of objects your program is using.
• This also means that you can change the size of
the values as you develop the program.
Names and Tokens
• Identifiers can be of unlimited length.
• Names and identifiers are case sensitive.
• Allowed characters: a-z, A-Z, 0-9 & underscore.
• It must begin with a letter or underscore.
• Names/variables in Python do not have a type.
Values have types.
Names and Tokens
• Naming conventions Not rigid, but:
–Modules and packages - all lower case.
–Global and constants – Upper case.
–Classes - caps with initial upper.
–Methods and functions – All lower case with words
separated by underscores.
–Local variables - Lowercase (with underscore
between words) or bumpy caps with initial lower or
your choice.
Blocks and Indentation
• Python represents block structure
and nested block structure with
indentation, not with begin and
end brackets.
• Indentation is 4 spaces and no
hard tabs.
• Reduces clutter. Eliminates all the
curly brackets.
Modules & Packages
• Modules correspond to files with a "*.py"
extension.
• Packages correspond to a directory (or folder) in
the file system; a package contains a file named
"__init__.py". Both modules and packages can be
imported.
• Packages - A directory containing a file named
"__init__.py".
Operators
• Python defines the following operators:
• The comparison operators <> and != are alternate
spellings of the same operator.
• Logical Operators: and, or, not, is, in
Operators’ Precedence
• Table summarizes the
operator precedence in
Python, from lowest
precedence to highest
precedence.
• Operators on the same line
have the same precedence.
• At end, +,-,*, / come.
Data Types
• Numeric Types
• Strings
• Lists
• Tuples
• Dictionaries
• Sets
• Files, Boolean, etc.
Data Types - Numeric
• Python supports 2 types of numbers:
– Integer
Ex: myint = 7
print(myint)
– Float
Ex: myfnt = 7.0
print(myfnt)
myff = float(28)
print(myff)
– complex nos: 3.14j
Data Types - Strings
• Strings in Python are identified as a contiguous set
of characters represented in the quotation marks.
• Python allows either pair of single or double quotes.
• Advantage of using double quotes is that we don’t
need to worry about apostrophes.
• More than 1 variable can be assigned values
simultaneously.
Data Types - Strings
Ex: mys = ‘Hello’
print(mys)
myss = “Mate”
print(myss)
print(mys + “ ”+myss)
Data Types - Strings
• 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 to the end.
• The plus (+) sign is the string concatenation
operator and the asterisk (*) is the repetition
operator.
Data Types - Strings
Data Types - Strings
Data Types – Formatted Strings
f-strings are string
literals that have an ‘f’
at the beginning and
curly braces containing
expressions that will be
replaced with their
values.
Data Types - Lists
• It is like a structure that
may contain different types
of data elements.
• Ex: list1 = [2233, 88.76,
45+6j, “wfh”, 56]
• Ex: list1[2] = “new”
Data Types - Lists
The concept of
slicing can also
be applied here:
Data Types - Lists
Data Types - Tuples
• It is similar to lists, but
once created it cannot
be edited.
• Tuple uses parenthesis
() instead of square
brackets[].
Data Types - Dictionary
• It is an alternate to the switch-case construct and is
used as a key-value pair.
• Each key is separated from its value by a colon (:),
the items are separated by commas, and the whole
thing is enclosed in curly braces.
• An empty dictionary without any items is written
with just two curly braces, like this: {}.
Data Types - Dictionary
Data Types – Boolean
• Boolean type provides two built-in values:
– True and False.
• It denotes by the class bool.
• True can be represented by any non-zero value or
'T' whereas false can be represented by the 0 or 'F'.
• Ex: print(type(True))
• Ex: print(type(False))
Data Types - Sets
• Python Set is the unordered collection of the data
type.
• It is iteratable, mutable(can modify after creation),
and has unique elements.
• In set, the order of the elements is undefined.
• The set is created by using a built-in
function set(), or a sequence of elements is passed
in the curly braces and separated by the comma.
Data Types - Sets
Ex: a = {5,2,3,1,4}
# printing set variable
print("a = ", a)
# data type of variable a
print(type(a))
Data Types - Sets
Ex: set1 = set()
set2 = {'James', 2, 3,'Pyt'}
print(set2)
set2.add(10)
print(set2)
set2.remove(2)
print(set2)
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
CONTACT ME AT:
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
THANK YOU !!!

More Related Content

Python Data-Types

  • 1. Akhil Kaushik Asstt. Prof., CE Deptt., TIT Bhiwani Data Types in Python
  • 2. Python Introduction • Python is a dynamic-typed language (uses interpreter). • Many other languages are static typed, such as C/C++ and Java (use compiler). • A static typed language requires the programmer to explicitly tell the computer what type of “thing” each data value is.
  • 3. Python Introduction • In Python, you simply give your variables names and assign values to them. • The interpreter takes care of keeping track of what kinds of objects your program is using. • This also means that you can change the size of the values as you develop the program.
  • 4. Names and Tokens • Identifiers can be of unlimited length. • Names and identifiers are case sensitive. • Allowed characters: a-z, A-Z, 0-9 & underscore. • It must begin with a letter or underscore. • Names/variables in Python do not have a type. Values have types.
  • 5. Names and Tokens • Naming conventions Not rigid, but: –Modules and packages - all lower case. –Global and constants – Upper case. –Classes - caps with initial upper. –Methods and functions – All lower case with words separated by underscores. –Local variables - Lowercase (with underscore between words) or bumpy caps with initial lower or your choice.
  • 6. Blocks and Indentation • Python represents block structure and nested block structure with indentation, not with begin and end brackets. • Indentation is 4 spaces and no hard tabs. • Reduces clutter. Eliminates all the curly brackets.
  • 7. Modules & Packages • Modules correspond to files with a "*.py" extension. • Packages correspond to a directory (or folder) in the file system; a package contains a file named "__init__.py". Both modules and packages can be imported. • Packages - A directory containing a file named "__init__.py".
  • 8. Operators • Python defines the following operators: • The comparison operators <> and != are alternate spellings of the same operator. • Logical Operators: and, or, not, is, in
  • 9. Operators’ Precedence • Table summarizes the operator precedence in Python, from lowest precedence to highest precedence. • Operators on the same line have the same precedence. • At end, +,-,*, / come.
  • 10. Data Types • Numeric Types • Strings • Lists • Tuples • Dictionaries • Sets • Files, Boolean, etc.
  • 11. Data Types - Numeric • Python supports 2 types of numbers: – Integer Ex: myint = 7 print(myint) – Float Ex: myfnt = 7.0 print(myfnt) myff = float(28) print(myff) – complex nos: 3.14j
  • 12. Data Types - Strings • Strings in Python are identified as a contiguous set of characters represented in the quotation marks. • Python allows either pair of single or double quotes. • Advantage of using double quotes is that we don’t need to worry about apostrophes. • More than 1 variable can be assigned values simultaneously.
  • 13. Data Types - Strings Ex: mys = ‘Hello’ print(mys) myss = “Mate” print(myss) print(mys + “ ”+myss)
  • 14. Data Types - Strings • 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 to the end. • The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator.
  • 15. Data Types - Strings
  • 16. Data Types - Strings
  • 17. Data Types – Formatted Strings f-strings are string literals that have an ‘f’ at the beginning and curly braces containing expressions that will be replaced with their values.
  • 18. Data Types - Lists • It is like a structure that may contain different types of data elements. • Ex: list1 = [2233, 88.76, 45+6j, “wfh”, 56] • Ex: list1[2] = “new”
  • 19. Data Types - Lists The concept of slicing can also be applied here:
  • 20. Data Types - Lists
  • 21. Data Types - Tuples • It is similar to lists, but once created it cannot be edited. • Tuple uses parenthesis () instead of square brackets[].
  • 22. Data Types - Dictionary • It is an alternate to the switch-case construct and is used as a key-value pair. • Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. • An empty dictionary without any items is written with just two curly braces, like this: {}.
  • 23. Data Types - Dictionary
  • 24. Data Types – Boolean • Boolean type provides two built-in values: – True and False. • It denotes by the class bool. • True can be represented by any non-zero value or 'T' whereas false can be represented by the 0 or 'F'. • Ex: print(type(True)) • Ex: print(type(False))
  • 25. Data Types - Sets • Python Set is the unordered collection of the data type. • It is iteratable, mutable(can modify after creation), and has unique elements. • In set, the order of the elements is undefined. • The set is created by using a built-in function set(), or a sequence of elements is passed in the curly braces and separated by the comma.
  • 26. Data Types - Sets Ex: a = {5,2,3,1,4} # printing set variable print("a = ", a) # data type of variable a print(type(a))
  • 27. Data Types - Sets Ex: set1 = set() set2 = {'James', 2, 3,'Pyt'} print(set2) set2.add(10) print(set2) set2.remove(2) print(set2)
  • 28. Akhil Kaushik akhilkaushik05@gmail.com 9416910303 CONTACT ME AT: Akhil Kaushik akhilkaushik05@gmail.com 9416910303 THANK YOU !!!