Data Types and Other Basic Elements in Python
Welcome to codeswithpankaj.com ! In this tutorial, we will explore Python's data types, how to classify data, and other fundamental elements of a Python program. By the end, you'll have a solid understanding of Python's building blocks.
Part 1: Comments in Python
What Are Comments?
Comments are used to explain code and make it easier to read. Python ignores comments during execution.
Types of Comments:
Single-line Comments Use the
#
symbol to write single-line comments.Multi-line Comments Use triple quotes (
'''
or"""
) for multi-line comments.
Part 2: Values and Data Types
What Are Data Types?
Data types define the kind of data a variable can hold. Python supports several built-in data types.
1. Numeric Data Types
Integer (int
)
Whole numbers (positive or negative).
Floating-point (float
)
Numbers with decimals.
Complex Numbers (complex
)
Numbers with real and imaginary parts.
2. Boolean Data Type
Represents
True
orFalse
.
3. Compound Data Types
List
Ordered and mutable collection.
Tuple
Ordered and immutable collection.
String
Sequence of characters.
4. Dictionary Data Type
Stores data in key-value pairs.
5. Set Data Type
Unordered collection of unique elements.
Part 3: Other Basic Elements of Python Program
1. Identifiers
Names used to identify variables, functions, or other elements.
Rules for identifiers:
Must start with a letter or underscore.
Can contain letters, digits, and underscores.
Case-sensitive.
2. Keywords
Reserved words in Python with predefined meanings.
Examples:
if
,else
,for
,while
,True
,False
, etc.
List of Python Keywords
Python has a specific set of keywords that are reserved and have predefined meanings. They should not be used as variable names. Here's a basic list of Python keywords:
False
None
True
and
as
assert
async
await
break
class
continue
def
del
elif
else
except
finally
for
from
global
if
import
in
is
lambda
nonlocal
not
or
pass
raise
return
try
while
with
yield
To obtain the most up-to-date list of Python keywords, you can use the keyword
module:
3. Variables
Containers for storing data values.
Python is dynamically typed, so you don’t need to declare the type explicitly.
Variable Declaration
Multiple Assignments
Reassigning Variables
Type Checking
Use the type()
function to check the type of a variable.
Examples Summary
Concept
Example Code
Output
Integer Data Type
x = 5; print(type(x))
<class 'int'>
List Data Type
lst = [1, 2, 3]; print(type(lst))
<class 'list'>
Boolean Data Type
flag = True; print(type(flag))
<class 'bool'>
Dictionary Data Type
d = {"key": "value"}; print(type(d))
<class 'dict'>
Variable Declaration
name = "Pankaj"; print(name)
Pankaj
Checking Keywords
import keyword; print(keyword.kwlist)
List of Python keywords
Conclusion
Python's data types and basic elements form the foundation for programming. Understanding these concepts is essential for building complex programs. Keep practicing, and stay tuned to codeswithpankaj.com for more tutorials!
Happy Coding! 🚀
Last updated