University of Delhi Syllabus ยท Unit 2

Creating Python Programs

Identifiers, operators, I/O, functions and control structures

Learning Outcomes
  • Use identifiers, keywords and literals correctly
  • Form expressions using Python operators
  • Read input and produce formatted output
  • Define functions and use control structures

Identifiers, Keywords and Literals

Identifiers name variables and functions and follow Python naming rules; keywords are reserved words. Literals include numbers and strings, and Python is dynamically typed so no explicit type declaration is needed.

age = 21 # integer literal pi = 3.14159 # float literal name = "Asha" # string literal

Operators and Expressions

Python provides arithmetic, relational, logical, assignment, membership and identity operators. Expressions combine operands with operators and are evaluated according to operator precedence.

a, b = 7, 3 print(a + b, a // b, a % b, a ** b)

Input and Output

The input function reads a string from the user and print displays output; f-strings format values cleanly within text.

n = int(input("Enter a number: ")) print(f"The square of {n} is {n*n}")

Functions and Control Structures

Functions are defined with def and may take default arguments and return values. Control flow uses if-elif-else and while or for loops with break, continue and pass; the exit function ends a program.

def is_even(n=0): return n % 2 == 0 for i in range(5): if is_even(i): print(i, "is even")

Summary

This unit covered identifiers and literals, operators and expressions, input and output, defining functions with default arguments, and the control structures of Python.

Exercises

  • Write a program to check whether a number is prime and to generate the first n prime numbers.
  • Write a program using a function to compute the factorial of a number.
  • Write a program to print a pyramid pattern of stars.