Docsity

Python Programming – Complete Beginner-Friendly Study Notes (Exam-Focused), Study notes of Computer science

Toggle sidebar
bg1
Python Programming • Exam Notes Page 1
Python Programming Language
Complete Exam-Focused Study Notes
Beginner Level • Important Definitions • Syntax • Examples • Quick Revision
Fundamental Python concepts: features, data types, operators, control statements, loops, functions and commonly tested programs.
1. Introduction to Python
Definition: Python is a high-level, interpreted, general-purpose programming language known for simple, readable syntax.
• Developer: Guido van Rossum • First released: 1991 • Extension: .py
• Features: simple, interpreted, portable, open-source, object-oriented, dynamically typed, extensive libraries.
Important Definitions
Term Definition
Program Set of instructions to perform a task.
Algorithm Step-by-step procedure to solve a problem.
Flowchart Graphical representation of an algorithm.
Syntax Rules for writing valid code.
Variable A name that refers to a value.
Keyword Reserved word with a predefined meaning.
Identifier Name used for variables, functions, classes, etc.
Expression Values, variables and operators producing a result.
Statement An instruction executed by a program.
Comment Text ignored during execution; explains code.
2. Variables and Identifiers
A variable is a name that refers to a value. Python does not require explicit type declarations.
name = "Aman"
age = 17
x = 10
x = "Hello"
• Start identifiers with a letter or underscore (_), not a digit.
• May contain letters, digits and underscores; no spaces or special symbols.
• Cannot be a keyword; identifiers are case-sensitive (age ≠ Age).
• Valid: student_name, _total, marks1. Invalid: 1name, student name, class.
3. Data Types
Type Meaning Example
int Integer 10, -5
float Decimal 3.14
complex Complex number 2+3j
bool Boolean True
str Text 'Python'
list Ordered, mutable [1, 2]
tuple Ordered, immutable (1, 2)
set Unique elements {1, 2}
dict Key-value pairs {'a': 1}
NoneType No value None
Mutable: list, set, dictionary. Immutable: int, float, string, tuple, Boolean.
pf3
pf4
pf5

Partial preview of the text

Download Python Programming – Complete Beginner-Friendly Study Notes (Exam-Focused) and more Study notes Computer science in PDF only on Docsity!

Python Programming Language

Complete Exam-Focused Study Notes

Beginner Level • Important Definitions • Syntax • Examples • Quick Revision

Fundamental Python concepts: features, data types, operators, control statements, loops, functions and commonly tested programs.

1. Introduction to Python

Definition: Python is a high-level, interpreted, general-purpose programming language known for simple, readable syntax.

  • Developer: Guido van Rossum • First released: 1991 • Extension: .py
  • Features: simple, interpreted, portable, open-source, object-oriented, dynamically typed, extensive libraries.

Important Definitions

Term Definition

Program Set of instructions to perform a task.

Algorithm Step-by-step procedure to solve a problem.

Flowchart Graphical representation of an algorithm.

Syntax Rules for writing valid code.

Variable A name that refers to a value.

Keyword Reserved word with a predefined meaning.

Identifier Name used for variables, functions, classes, etc.

Expression Values, variables and operators producing a result.

Statement An instruction executed by a program.

Comment Text ignored during execution; explains code.

2. Variables and Identifiers

A variable is a name that refers to a value. Python does not require explicit type declarations.

name = "Aman" age = 17 x = 10 x = "Hello"

  • Start identifiers with a letter or underscore (_), not a digit.
  • May contain letters, digits and underscores; no spaces or special symbols.
  • Cannot be a keyword; identifiers are case-sensitive (age ≠ Age).
  • Valid: student_name, _total, marks1. Invalid: 1name, student name, class.

3. Data Types

Type Meaning Example

int Integer 10, -

float Decimal 3.

complex Complex number 2+3j

bool Boolean True

str Text 'Python'

list Ordered, mutable [1, 2]

tuple Ordered, immutable (1, 2)

set Unique elements {1, 2}

dict Key-value pairs {'a': 1}

NoneType No value None

Mutable: list, set, dictionary. Immutable: int, float, string, tuple, Boolean.

4. Input, Output & Type Conversion

Output — print()

print("Hello World") print(10 + 5) # 15

Input — input()

input() accepts user input and returns a string by default.

name = input("Enter name: ") age = int(input("Enter age: ")) marks = float(input("Enter marks: "))

Type conversion

int() → integer • float() → float • str() → string • bool() → Boolean • list() → list • tuple() → tuple.

x = "10" print(int(x) + 5) # 15

5. Operators

Category Operators / Meaning

Arithmetic + add; − subtract; * multiply; / true division; // floor division; % remainder; ** power

Comparison ==, !=, >, <, >=, <= (return True/False)

Logical and: both true; or: at least one true; not: reverses truth value

Assignment =, +=, -=, *=, /=, //=, %=, **=

Membership in, not in — membership test

Identity is, is not — same object reference

Examples: 5/2 = 2.5; 5//2 = 2; 5%2 = 1; 2**3 = 8.

6. Conditional Statements

Used to make decisions based on conditions.

if / if-else / if-elif-else

if n % 2 == 0: print("Even") else: print("Odd")

if marks >= 90: print("A") elif marks >= 60: print("B") else: print("C")

Rule: Use a colon (:) and indent the statements inside each block.

7. Loops

for loop and range()

for i in range(1, 6): print(i) # 1 to 5

range(stop) starts at 0; stop excluded. range(start, stop) excludes stop. range(start, stop, step) uses step. Example: range(1,

while loop

i = 1 while i <= 5: print(i) i += 1

Statement Purpose

break Terminates nearest enclosing loop.

continue Skips rest of current iteration.

pass Does nothing; placeholder.

12. Errors and Exception Handling

Error type Meaning Example

Syntax error Breaks Python syntax rules Missing colon

Runtime error Occurs during execution Division by zero

Logical error Runs but gives wrong result Wrong operator

Exception handling

try: print(10 / 0) except ZeroDivisionError: print("Cannot divide by zero")

**13. Important Exam Programs

  1. Add two numbers**

a = int(input("First: ")) b = int(input("Second: ")) print("Sum =", a + b)

2. Even or odd

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

3. Largest of two numbers

a = int(input("First: ")) b = int(input("Second: ")) if a > b: print(a) else: print(b)

4. Factorial

n = int(input("Number: ")) fact = 1 for i in range(1, n + 1): fact *= i print(fact)

For non-negative integers, factorial is the product of positive integers up to n; 0! = 1.

5. Multiplication table

n = int(input("Number: ")) for i in range(1, 11): print(n, "x", i, "=", n * i)

6. Sum of first N natural numbers

n = int(input("Number: ")) total = 0 for i in range(1, n + 1): total += i print(total)

14. Common Mistakes & Important Rules

  • = assigns; == compares.
  • input() returns a string by default; convert when needed.
  • Indentation defines blocks; wrong indentation may cause IndentationError.
  • Indexing starts at 0; range() excludes stop.
  • Python is case-sensitive (Name ≠ name).
  • Strings are immutable; lists are mutable.
  • Dictionary keys are unique and hashable.
  • / is true division; // is floor division.

15. Quick Revision Sheet

  • Creator: Guido van Rossum • Released: 1991 • Extension: .py
  • Output: print() • Input: input() • Length: len() • Type: type()
  • Decision: if/elif/else • Loops: for/while • Function: def
  • Loop controls: break, continue, pass.
  • List: mutable []; Tuple: immutable (); Set: unique values; Dictionary: key-value pairs.
  • Comment: # for a single-line comment. 16. Important Exam Questions

Very Short Answer

1. Who developed Python? 2. What is a variable? 3. Difference between = and ==? 4. Purpose of input()? 5. What is indentation?

6. What is a keyword? 7. Difference between break and continue?

Short Answer

1. Explain five features of Python. 2. Differentiate list and tuple. 3. Explain types of operators. 4. Differentiate for and while. 5.

Explain types of errors. 6. Explain functions and their advantages.

Programming Questions

Write programs to check even/odd, find factorial, print a multiplication table, find the largest of two numbers, and calculate the sum

of first N natural numbers.

Final Summary

Python is a high-level, interpreted language with variables, data types, operators, conditionals, loops, functions and collections.

Focus on definitions, syntax, data-type differences, indentation and basic programs.