Unit 1: Python fundamentals
Python Programming notes · PTU syllabus (UGSEC2507)
On this page
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
- 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
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.
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 type | Example |
|---|---|
| int | 25 |
| float | 3.14 |
| complex | 2 + 3j |
| str | "SBS" |
| bool | True |
| 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) andprint("Hello", name, sep=" ", end="\n").
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 formattingTopic 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 looppassis 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
- Q1.List four features of Python.
- Q2.Differentiate between / and // operators.
- Q3.What does input() return?
- Q4.What is string slicing? Give an example.
- Q5.Differentiate between break, continue and pass.
Long-answer questions
- Q1.Explain the data types and operators of Python with examples.
- Q2.Explain string creation, slicing, formatting and built-in string methods.
- 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.
