Unit 1 of 3 · BCA Sem 3

Unit 1: Python fundamentals

Python Programming notes · PTU syllabus (UGSEC2507)

3 min read4 topics8 exam questions
On this page
  1. Unit summary
  2. Introduction to Python
  3. Identifiers, keywords, operators and data types
  4. Strings
  5. Control flow
  6. Key terms
  7. Quick revision
  8. Important questions

Unit summary

Python is a simple yet powerful programming language used in web development, data science, AI, automation and scripting. This unit covers Python's history and uses, program structure, identifiers, keywords, operators, data types, input/output, strings and the control-flow statements.

After this unit you can

  • Explain Python's features and application areas
  • Use identifiers, keywords, operators and data types with type conversion
  • Create, slice and format strings and use string methods
  • Write programs with if, loops, break, continue and pass

PTU syllabus topics

  • History and application areas
  • program structure
  • identifiers and keywords
  • operators and precedence
  • data types and type conversion
  • statements and expressions
  • I/O statements
  • string creation and built-in functions
  • string slicing and formatting
  • conditional and loop control flow
  • continue/pass/break/exit statements
Key termsPython string slicing (s = "PYTHON")
s[0]
'P', the first character
s[-1]
'N', the last character
s[1:4]
'YTH', from index 1 up to 4
s[::-1]
'NOHTYP', reversed
len(s)
6, the number of characters
1

Topic 1

Introduction to Python

Python was created by Guido van Rossum and released in 1991. It is interpreted, high-level, dynamically typed and object-oriented, with readable syntax that uses indentation instead of braces. Application areas: web development (Django, Flask), data science and machine learning (NumPy, pandas, scikit-learn), automation and scripting, desktop apps, game development and IoT.

2

Topic 2

Identifiers, keywords, operators and data types

  • Identifiers start with a letter or underscore and are case-sensitive; keywords (if, else, for, while, def, return, class, import, True, False, None …) are reserved.
Data typeExample
int25
float3.14
complex2 + 3j
str"SBS"
boolTrue
list / tuple / dict / set[1, 2] / (1, 2) / {"a": 1} / {1, 2}
  • Operators: arithmetic (+, −, ×, /, // floor division, % modulus, ** power), comparison, logical (and, or, not), membership (in, not in) and identity (is, is not).
  • Type conversion: int("5"), float(3), str(10).
  • Input/output: name = input("Enter name: ") (always returns a string) and print("Hello", name, sep=" ", end="\n").
3

Topic 3

Strings

Strings are immutable sequences of characters.

pythons = "Python Programming"
print(s[0], s[-1])        # P g
print(s[0:6])             # Python   (slicing: start included, end excluded)
print(s[::-1])            # reversed string
print(s.upper(), s.lower(), s.split(), s.replace("Python", "C"))
print(len(s), s.find("Pro"), s.count("m"))
name, marks = "Ana", 91.5
print(f"{name} scored {marks:.1f}")      # f-string formatting
4

Topic 4

Control flow

pythonn = int(input("Number: "))
if n % 2 == 0:
    print("Even")
else:
    print("Odd")

for i in range(1, 6):     # 1 to 5
    if i == 3:
        continue          # skip 3
    print(i)

while True:
    x = int(input())
    if x < 0:
        break             # exit the loop
  • pass is a placeholder that does nothing; exit() (sys.exit) ends the program.
  • range(start, stop, step) generates numbers up to but not including stop.

Exam tip

Python uses indentation to define blocks. Mixing tabs and spaces causes an IndentationError — always use 4 spaces.

Key terms

Interpreter
Runs Python code line by line
Dynamic typing
Variable types are decided at run time
Slicing
Extracting part of a sequence with [start:stop:step]
Immutable
Cannot be changed after creation
f-string
A formatted string literal starting with f

Quick revision

  • Python: interpreted, dynamically typed, uses indentation.
  • // floor division, ** power, % remainder.
  • input() always returns a string; convert with int().
  • s[::-1] reverses; strings are immutable.
  • break exits, continue skips, pass does nothing.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.List four features of Python.
  2. Q2.Differentiate between / and // operators.
  3. Q3.What does input() return?
  4. Q4.What is string slicing? Give an example.
  5. Q5.Differentiate between break, continue and pass.

Long-answer questions

  1. Q1.Explain the data types and operators of Python with examples.
  2. Q2.Explain string creation, slicing, formatting and built-in string methods.
  3. Q3.Explain the control-flow statements of Python with programs.

Stuck on this unit?

Message SBS on WhatsApp for help with Python Programming, or to ask about studying BCA at Synetic.

WhatsApp us