Lab 1

De la WikiLabs
Versiunea din 14 septembrie 2026 05:20, autor: Andrei.ulmamei (discuție | contribuții) (Pagină nouă: = Lab 1 — Python for C/C++ Programmers = == Objectives == The purpose of this laboratory is to introduce Python to students who already have experience with procedural programm...)
(dif) ← Versiunea anterioară | Versiunea curentă (dif) | Versiunea următoare → (dif)
Jump to navigationJump to search

Lab 1 — Python for C/C++ Programmers

Objectives

The purpose of this laboratory is to introduce Python to students who already have experience with procedural programming in C and object-oriented programming in C++.

The focus is therefore not on learning programming concepts from scratch, but on understanding how concepts already familiar from C/C++ are expressed in Python.

After completing this laboratory, you should be able to:

  • understand the basic syntax of Python;
  • recognize the main differences between Python and C/C++;
  • work with Python's fundamental data types;
  • use conditional statements and loops;
  • work with lists, dictionaries, sets and tuples;
  • define and call functions;
  • understand basic Python object/reference semantics;
  • recognize several common Python idioms;
  • create and execute a simple Python project.

Throughout this laboratory, equivalent or similar C/C++ and Python code will be presented side by side.

    • TOC**

1. Running a Python program

A C/C++ program normally has to be compiled before it is executed. Python programs are usually executed directly by the Python interpreter.

C/C++ Python
#include <iostream>

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

A Python source file normally uses the extension:

.py

For example:

hello.py

It can be executed using:

python3 hello.py

Unlike C/C++, Python does not require a special main() function.

However, larger Python programs commonly use:

C/C++ Python
#include <iostream>

int main()
{
std::cout << "Program started\n";

```
return 0;
```

}
def main():
print("Program started")

if **name** == "**main**":
main()

The Python version is not mandatory, but this pattern will become useful when programs are split into multiple modules.

2. Blocks and indentation

In C and C++, braces delimit blocks of code.

Python uses indentation instead.

C/C++
if (x > 10) {
    std::cout << "Large\n";
    std::cout << "Value: " << x << "\n";
}
if x > 10:
    print("Large")
    print("Value:", x)

Indentation in Python is part of the language syntax.

The standard convention is 4 spaces per indentation level.

For example, the following code is incorrect:

if x > 10:
print("Large")

Python will report an IndentationError.

3. Variables and types

C/C++ variables have a declared static type.

Python variables do not require explicit type declarations.

C/C++
int count = 10;
double temperature = 23.5;
bool enabled = true;
std::string name = "Alice";
count = 10
temperature = 23.5
enabled = True
name = "Alice"

Python is dynamically typed.

This means that a variable name can later refer to an object of another type.

C/C++
int value = 10;

// Not allowed:
// value = "hello";
value = 10
value = "hello"

This does not mean that Python has no types.

Objects still have types:

C/C++
int value = 10;

std::cout << typeid(value).name();
value = 10

print(type(value))

Python will produce:

<class 'int'>

Common basic types

C/C++ type Python equivalent

4. Constants

C++ can enforce that a value cannot be modified using const.

Python does not enforce constants at language level.

C/C++
const double PI = 3.1415926535;
const int MAX_USERS = 100;
PI = 3.1415926535
MAX_USERS = 100

By convention, Python names that should be treated as constants are written using uppercase letters.

Nothing prevents the programmer from changing them.

5. Arithmetic operators

Most arithmetic operators are identical.

C/C++
int a = 10;
int b = 3;

int sum  = a + b;
int diff = a - b;
int prod = a * b;
int div  = a / b;
int rem  = a % b;
a = 10
b = 3

sum_ = a + b
diff = a - b
prod = a * b
div = a // b
rem = a % b

There is an important difference between / and //.

C/C++
int a = 10;
int b = 3;

std::cout << a / b;   // 3
a = 10
b = 3

print(a / b)   # 3.3333333333333335
print(a // b)  # 3

Python also provides an exponentiation operator:

C/C++
#include <cmath>

double result = std::pow(2, 8);
result = 2 ** 8

6. Printing values

Output using std::cout is replaced by the built-in print() function.