Lab 1
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. ConstantsC++ can enforce that a value cannot be modified using Python does not enforce constants at language level.
By convention, Python names that should be treated as constants are written using uppercase letters. Nothing prevents the programmer from changing them. 5. Arithmetic operatorsMost arithmetic operators are identical.
There is an important difference between
Python also provides an exponentiation operator:
6. Printing valuesOutput using |
|---|