Python Basics

2.1 Keywords and Identifiers

What are Keywords

Keywords are reserved words in Python that have special meanings and cannot be used as variable names.
Examples: if, else, while, for, True, False, None

List of Python Keywords

Python provides a predefined set of keywords. You can view them using:

import keyword
print(keyword.kwlist)

What are Identifiers

Identifiers are names used to identify variables, functions, classes, etc.
Example:

name = "Pooja"

Here, name is an identifier.

Rules for Naming Identifiers

  • Must start with a letter or underscore _
  • Cannot start with a number
  • Cannot use keywords
  • Can contain letters, numbers, and underscores

Best Practices

  • Use meaningful names (student_name instead of s)
  • Follow snake_case naming convention

2.2 Variables and Constants

What is a Variable

A variable is used to store data in memory.
Example:

age = 20

Declaring and Assigning Variables

Python does not require explicit declaration.

x = 10
y = "Hello"

Multiple Assignments

a, b, c = 1, 2, 3

Naming Conventions

  • Use lowercase letters
  • Separate words using underscores

What are Constants

Constants are values that do not change during execution.

Defining Constants

Python does not have built-in constants, but conventionally:

PI = 3.14

2.3 Data Types Overview

Introduction to Data Types

Data types define the type of data stored in a variable.

Numeric Types

a = 10        # int
b = 3.14 # float
c = 2 + 3j # complex

Sequence Types

list_data = [1, 2, 3]
tuple_data = (1, 2, 3)
string_data = "Hello"

Set Type

set_data = {1, 2, 3}

Mapping Type

dict_data = {"name": "Pooja", "age": 20}

Boolean Type

is_active = True

Checking Data Types

print(type(a))

2.4 Type Casting

What is Type Casting

Type casting means converting one data type into another.

Implicit Type Conversion

Python automatically converts types:

x = 10
y = 2.5
z = x + y # result is float

Explicit Type Conversion

Manual conversion:

x = int("10")
y = float(5)
z = str(100)

Common Functions

  • int()
  • float()
  • str()

Practical Example

num = int(input("Enter a number: "))
print(num + 5)

2.5 Input and Output Functions

Taking Input

name = input("Enter your name: ")

Displaying Output

print("Hello", name)

Formatting Output

age = 20
print(f"Age is {age}")

Multiple Inputs

a, b = input("Enter two numbers: ").split()

Escape Characters

print("Hello\nWorld")

2.6 Comments and Documentation

What are Comments

Comments are used to explain code.

Single-line Comments

# This is a comment

Multi-line Comments

"""
This is a
multi-line comment
"""

Writing Good Comments

  • Keep them clear and concise
  • Explain why, not what

Docstrings

Used to document functions:

def add(a, b):
"""This function returns sum of two numbers"""
return a + b

Importance of Documentation

  • Improves readability
  • Helps other developers understand code
  • Makes maintenance easier

Conclusion

Understanding Python basics is essential for any beginner starting their programming journey. Concepts like keywords, variables, data types, and input/output form the foundation of all Python programs. Mastering these topics will make it easier to learn advanced concepts such as loops, functions, and object-oriented programming.

Consistent practice with these basics will help learners gain confidence and improve problem-solving skills. This module acts as the first major step toward becoming proficient in Python programming.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *