Unit 1:
Python Syntax and Basic Data Types
1. Introduction to Python Syntax
1.1 General Characteristics
• Python emphasizes readability with a clean and straightforward
syntax.
• Indentation is critical in Python and is used to define code blocks.
1.2 Writing and Running Python Code
• Python scripts have a .py file extension.
• Code can be run:
o Directly in the Python REPL.
o As a script from the command line using python script_name.py.
o Inside an IDE or text editor with a built-in Python interpreter.
1.3 Basic Syntax Rules
• Case Sensitivity: Python is case-sensitive, e.g., Variable and variable
are different.
• Comments: Use # for single-line comments and triple quotes (''' or """)
for multi-line comments.
# This is a single-line comment
"""
This is a
multi-line comment.
"""
• Statements: Typically, one statement per line. Use a semicolon ; to
separate multiple statements on the same line.
2. Basic Data Types
2.1 Overview of Data Types
• Python has several built-in data types, categorized as:
o Numeric types: int, float, complex
o Text type: str
o Sequence types: list, tuple, range
o Set types: set, frozenset
o Mapping type: dict
o Boolean type: bool
o None type: NoneType
2.2 Numeric Data Types
• Integer (int): Whole numbers.
x=5
print(type(x)) # Output: <class 'int' >
• Float (float): Numbers with decimal points.
y = 5.3
print(type(y)) # Output: <class 'float'>
• Complex (complex): Numbers with a real and imaginary part.
z = 2 + 3j
print(type(z)) # Output: <class 'complex'>
2.3 Text Data Type
• String (str): Sequence of characters enclosed in single, double, or
triple quotes.
s = "Hello, World!"
print(type(s)) # Output: <class 'str'>
o Strings are immutable.
o Access individual characters using indexing (e.g., s[0] for the
first character).
2.4 Boolean Data Type
• Boolean (bool): Represents True or False values.
a = True
b = False
print(type(a)) # Output: <class 'bool'>
o Often used in conditional statements.
2.5 None Type
• None (NoneType): Represents the absence of a value or a null value.
n = None
print(type(n)) # Output: <class 'NoneType'>
3. Type Conversion
3.1 Implicit Type Conversion
• Python automatically converts one data type to another in some
operations.
x = 5 # int
y = 2.5 # float
result = x + y
print(type(result)) # Output: <class 'float'>
3.2 Explicit Type Conversion
• Use built-in functions to convert data types explicitly.
a = "10"
b = int(a) # Convert string to int
c = float(b) # Convert int to float
print(type(b), type(c)) # Output: <class 'int'> <class 'float'>
4. Common Operations on Data Types
4.1 Strings
• Concatenation: "Hello" + " World"
• Repetition: "Hi" * 3
• Methods: .upper(), .lower(), .strip(), .replace(), .split()
s = " hello ".strip().upper()
print(s) # Output: HELLO
4.2 Numbers
• Basic arithmetic: +, -, *, /
• Modulus: %
• Exponentiation: **
• Floor division: //
4.3 Booleans
• Logical operators: and, or, not
print(True and False) # Output: False
print(not True) # Output: False
5. Exercises
1. Write a Python program to declare variables of different types and print
their types.
2. Convert a string representation of a number to an integer and perform
arithmetic operations on it.
3. Experiment with string methods to manipulate a given text.