Docsity

C++ & Python Programming Notes | Beginner Coding Guide, Syntax, Examples & Practice, Study notes of Computer science

Toggle sidebar
bg1
C / C++ & Python Programming Notes • Page 1
C / C++ & PYTHON
PROGRAMMING NOTES
Beginner → Intermediate Quick Reference
A practical study guide covering core programming concepts, syntax, problem-solving, and common
patterns.
How to use: Read the concept, type the example yourself, then change it and see what happens. Programming is
learned by writing and debugging—not by memorizing code.
PART I — PROGRAMMING FOUNDATIONS
1. What Is Programming?
Programming is the process of giving a computer precise instructions. A program takes input, processes it using logic,
and produces output.
Algorithm: a step-by-step method for solving a problem. Variable: a named place used to store a value. Function: a
reusable block of code.
Problem-solving pattern: Understand the problem → identify inputs/outputs → design an algorithm → write code
→ test → debug → improve.
2. Variables & Data Types
A variable stores data. Common types include integers, decimal numbers, characters/text, and Boolean values.
C/C++:
int age = 17;
double price = 12.50;
char grade = 'A';
bool passed = true;
Python:
age = 17
price = 12.50
grade = "A"
passed = True
C/C++ generally requires you to declare a variable's type. Python determines the type at runtime.
3. Input & Output
C++:
pf3
pf4
pf5
pf8

Partial preview of the text

Download C++ & Python Programming Notes | Beginner Coding Guide, Syntax, Examples & Practice and more Study notes Computer science in PDF only on Docsity!

C / C++ & PYTHON

PROGRAMMING NOTES

Beginner → Intermediate Quick Reference

A practical study guide covering core programming concepts, syntax, problem-solving, and common

patterns.

How to use: Read the concept, type the example yourself, then change it and see what happens. Programming is

learned by writing and debugging—not by memorizing code.

PART I — PROGRAMMING FOUNDATIONS

1. What Is Programming?

Programming is the process of giving a computer precise instructions. A program takes input , processes it using logic,

and produces output.

Algorithm: a step-by-step method for solving a problem. Variable: a named place used to store a value. Function: a

reusable block of code.

Problem-solving pattern: Understand the problem → identify inputs/outputs → design an algorithm → write code

→ test → debug → improve.

2. Variables & Data Types

A variable stores data. Common types include integers, decimal numbers, characters/text, and Boolean values.

C/C++:

int age = 17; double price = 12.50; char grade = 'A'; bool passed = true;

Python:

age = 17 price = 12. grade = "A" passed = True

C/C++ generally requires you to declare a variable's type. Python determines the type at runtime.

3. Input & Output

C++:

#include using namespace std;

int main() { int age; cin >> age; cout << "Age: " << age; return 0; }

Python:

age = int(input("Age: ")) print("Age:", age)

Important: Python's input() returns text. Convert it with int(), float(), etc., when you need a number.

4. Operators

Category C/C++ / Python examples Purpose

Arithmetic + - * / Basic calculations

Remainder % Gives the remainder

Power C++: pow(a,b) / Python: a ** b Exponentiation

Comparison == != > < >= <= Produces true/false

Logical && / and; || / or;! / not Combines conditions

Assignment = += -= *= /= Stores/updates values

5. Conditional Statements

Use conditions to make decisions.

C++:

if (score >= 50) { cout << "Pass"; } else { cout << "Try again"; }

Python:

if score >= 50: print("Pass") else: print("Try again")

Python uses indentation to define blocks. C/C++ commonly uses braces { }.

6. Loops

Loops repeat code.

C++ for loop:

10. C++ Basics

#include

int main() { std::cout << "Hello, world!\n"; return 0; }

C++ uses streams such as std::cout and std::cin. The std:: prefix identifies names in the standard namespace.

11. Arrays

An array stores multiple values of the same type in contiguous positions. Indexing normally starts at 0.

int numbers[5] = {10, 20, 30, 40, 50}; cout << numbers[0]; // 10

Be careful with bounds: an array of size 5 has valid indexes 0 through 4.

12. Strings

C-style strings are character arrays ending with a null character. C++ also provides std::string, which is usually easier to

use.

#include std::string name = "Alex"; std::cout << name.length();

13. Pointers

A pointer stores a memory address. The & operator can obtain an address, while * can dereference a pointer.

int x = 10; int* p = &x;

std::cout << *p; // 10

Safety rule: Never dereference an uninitialized, null, or invalid pointer. Pointers are powerful but can cause crashes

and memory bugs.

14. References in C++

A reference provides another name for an existing object.

void addOne(int& x) { x++; }

Unlike a pointer, a reference is normally used as an alias rather than as a separate nullable address value.

15. Classes & Objects

class Student { public: std::string name;

void introduce() { std::cout << "Hi, I'm " << name; } };

Student s; s.name = "Alex"; s.introduce();

Class: blueprint/type. Object: an instance of a class. Encapsulation: keeping data and operations organized behind an

interface.

16. C++ STL Essentials

The Standard Template Library provides common containers and algorithms.

  • vector — dynamic array-like container.
  • string — text.
  • map — key/value association.
  • set — collection of unique values.
  • sort() — sorting algorithm.

#include #include

std::vector a = {4, 1, 3, 2}; std::sort(a.begin(), a.end());

PART III — PYTHON

17. Python Syntax

Python emphasizes readable syntax. Blocks are defined by indentation.

name = "Destiny" if name: print("Hello,", name)

Comments begin with #.

18. Lists, Tuples, Sets & Dictionaries

  • List: ordered and mutable — [1, 2, 3].
  • Tuple: ordered and immutable — (1, 2, 3).
  • Set: collection of unique values — {1, 2, 3}.
  • Dictionary: key/value pairs — {'name': 'Alex'}.
  • Runtime error: program starts but fails during execution.
  • Logic error: program runs but produces the wrong result.
  • Type error: an operation is applied to an incompatible type.

Debugging method: Reproduce the problem → read the error message → locate the failing line → inspect inputs

and assumptions → make one change → test again.

24. Time Complexity Basics

Big-O notation describes how an algorithm's work grows as input size increases.

Complexity Example idea Typical growth

O(1) Access one array element Constant

O(log n) Binary search Slow growth

O(n) Scan every item Linear

O(n log n) Efficient comparison sorting Near-linearithmic

O(n²) Nested loops over the same data Quadratic

25. Quick Comparison

Feature C C++ Python

Typing Static Static Dynamic

Memory control Manual Manual + abstractions Mostly automatic

Main style Procedural Multi-paradigm Multi-paradigm

Typical syntax Compact, low-level C-like High-level/readable

Common use Systems/embedded Systems, games, apps Automation, data, web, scripting

26. Mini Practice

1. Which Python expression gives the remainder of 17 divided by 5?

A) 17 / 5 B) 17 % 5 C) 17 // 5 D) 17 ** 5

2. What is the first valid index of a C++ array?

A) 0 B) 1 C) -1 D) It depends on the compiler

3. Which keyword defines a function in Python?

A) func B) function C) def D) define

4. What does a C++ pointer store?

A) Only integers B) A memory address C) A function name only D) A loop

5. What is the likely result of an infinite while loop?

A) It stops immediately B) It repeatedly executes until stopped/broken C) It becomes a function D) It compiles into Python

Answer Key

1. B — % gives the remainder.

2. A — C/C++ arrays use zero-based indexing.

3. C — def begins a Python function definition.

4. B — a pointer holds a memory address.

5. B — unless its condition changes or the loop exits, it keeps running.

27. Final Cheat Sheet

  • C/C++: remember types, semicolons, braces, arrays, pointers, references, and memory safety.
  • Python: remember indentation, dynamic typing, lists/dictionaries, functions, exceptions, and imports.
  • Both: master variables, conditions, loops, functions, data structures, debugging, and algorithms.
  • Before coding: write the steps in plain language first.
  • When stuck: make the problem smaller, test one assumption at a time, and inspect actual values.

Practice challenge: Build the same small program in both Python and C++. For example: ask for five numbers,

calculate the average, and print the largest number. Comparing the two implementations is an excellent way to

understand the languages.

C / C++ & Python Programming Notes • Educational study guide. Language syntax and library features can vary by version; consult the

official documentation for version-specific details.