<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ro">
	<id>http://wiki.dcae.pub.ro/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Andrei.ulmamei</id>
	<title>WikiLabs - Contribuții utilizator [ro]</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.dcae.pub.ro/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Andrei.ulmamei"/>
	<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php/Special:Contribu%C8%9Bii/Andrei.ulmamei"/>
	<updated>2026-10-05T11:26:14Z</updated>
	<subtitle>Contribuții utilizator</subtitle>
	<generator>MediaWiki 1.35.14</generator>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=SDA_Lucrarea_1&amp;diff=8384</id>
		<title>SDA Lucrarea 1</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=SDA_Lucrarea_1&amp;diff=8384"/>
		<updated>2026-10-01T10:03:40Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Problema 3 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;După acest laborator veți putea folosi IDE-ul pentru a scrie și depana programe în C. Totodată veți relua și recapitula noțiunile legate de pointeri în C.&lt;br /&gt;
&lt;br /&gt;
= Utilizarea IDE-ului CLion =&lt;br /&gt;
&lt;br /&gt;
[https://www.jetbrains.com/clion CLion] este un mediu integrat de dezvoltare (IDE) care permite dezvoltarea de programe în limbajele C și C++. CLion nu instalează și compilator pentru C/C++, acesta trebuie instalat manual, în prealabil. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;lt;span style=&amp;quot;color: red; font-weight: bold&amp;quot;&amp;gt;Atenție:&amp;lt;/span&amp;gt; Imaginea de Linux folosită la Programarea Calculatoarelor are deja instalat compilatorul de C.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Instalarea GCC ==&lt;br /&gt;
&lt;br /&gt;
=== Instalarea GCC în Linux ===&lt;br /&gt;
&lt;br /&gt;
Pentru instalarea compilatorului de C (GCC) în distribuțiile de Linux provenite din Ubuntu (Ubuntu, Kubuntu, Xubuntu, Mint, LMDE, etc.) se poate folosi comanda:&lt;br /&gt;
&lt;br /&gt;
 apt-get install -y build-essential&lt;br /&gt;
&lt;br /&gt;
=== Instalarea GCC în Windows ===&lt;br /&gt;
&lt;br /&gt;
Pentru compilarea de programe cu GCC în Windows, aveți nevoie de instalarea acestui compilator care poate fi făcută prin instalarea unuia din următoarele două suite de programe:&lt;br /&gt;
* [https://cygwin.com Cygwin] - Tutorial de instalare [https://cygwin.com/install.html aici].&lt;br /&gt;
* [http://www.mingw.org/ MinGW] - Tutorial de instalare [http://www.mingw.org/wiki/InstallationHOWTOforMinGW aici].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;lt;span style=&amp;quot;color: red; font-weight: bold&amp;quot;&amp;gt;Atenție:&amp;lt;/span&amp;gt; Nu uitați ca la instalare să bifați în lista de pachete și compilatorul de C (&amp;lt;code&amp;gt;gcc&amp;lt;/code&amp;gt;), &amp;lt;code&amp;gt;make&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;git&amp;lt;/code&amp;gt;.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Instalarea CLion ==&lt;br /&gt;
&lt;br /&gt;
De la adresa https://www.jetbrains.com/clion/download/#section=linux puteți descărca kitul de instalare pentru sistemul vostru de operare.&lt;br /&gt;
După instalare, urmaţi următorii paşi de configurare, în funcţie de suita de programe aleasă:&lt;br /&gt;
* [https://www.jetbrains.com/help/clion/quick-tutorial-on-configuring-clion-on-windows.html#Cygwin Configurare CLion cu Cygwin]&lt;br /&gt;
* [https://www.jetbrains.com/help/clion/quick-tutorial-on-configuring-clion-on-windows.html#MinGW  Configurare CLion cu MinGW]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Realizarea unui proiect ==&lt;br /&gt;
&lt;br /&gt;
Odată pornit, CLion oferă posibilitatea de a deschide un proiect existent, sau de a crea unul nou. Vrem să realizăm un proiect nou, aşadar se va alege &amp;#039;&amp;#039;&amp;#039;New Project&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt; &amp;#039;&amp;#039;&amp;#039; Observație: &amp;#039;&amp;#039;&amp;#039; Se poate realiza un nou proiect atunci când altul este deja deschis folosind meniul &amp;lt;code&amp;gt;File - New Project &amp;lt;/code&amp;gt; .&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Welcome_to_clion.png | 600px | Imaginea 1]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt; Mai departe se va selecta categoria &amp;#039;&amp;#039;&amp;#039;C++ Executable&amp;#039;&amp;#039;&amp;#039; şi se va introduce locaţia proiectului.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;lt;span style=&amp;quot;color: red; font-weight: bold&amp;quot;&amp;gt;Atenție:&amp;lt;/span&amp;gt; În imagine se observă că locaţia introdusă este &amp;#039;&amp;#039;&amp;#039;/home/student/projects/NewProject&amp;#039;&amp;#039;&amp;#039;. Se recomandă ca directorul în care se realizează proiectul să aibă numele proiectului, sau un nume sugestiv (în imagine &amp;#039;&amp;#039;&amp;#039;NewProject&amp;#039;&amp;#039;&amp;#039;)&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Fișier:New_project_location_clion.png | 600px | Imaginea 2]]&lt;br /&gt;
&lt;br /&gt;
== Componentele IDE-ului CLion == &lt;br /&gt;
&lt;br /&gt;
[[Fișier:CLion_env.png | 1000px | Imaginea 3]]&lt;br /&gt;
&lt;br /&gt;
La crearea unui nou proiect, IDE-ul CLion generează automat fişierul &amp;#039;&amp;#039;&amp;#039;main.cpp&amp;#039;&amp;#039;&amp;#039;. Acesta conţine un exemplu program ce va fi rescris de către utilizator. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Secţiunile marcate în imagine reprezintă:&lt;br /&gt;
# Zona &amp;lt;span style=&amp;quot;color: green&amp;quot;&amp;gt;verde&amp;lt;/span&amp;gt; - &amp;#039;&amp;#039;&amp;#039;Project view&amp;#039;&amp;#039;&amp;#039; indică toate fişierele şi directoarele ce alcătuiesc proiectul.&lt;br /&gt;
# Zona &amp;lt;span style=&amp;quot;color: blue&amp;quot;&amp;gt;albastră&amp;lt;/span&amp;gt; - &amp;#039;&amp;#039;&amp;#039;Editor&amp;#039;&amp;#039;&amp;#039; este fereastra de vizualizare şi editare a textului &lt;br /&gt;
# Zona &amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;roşie&amp;lt;/span&amp;gt; - &amp;#039;&amp;#039;&amp;#039;Toolbar&amp;#039;&amp;#039;&amp;#039; oferă acces rapid pentru operaţiile uzuale. Dintre acestea, cel mai des vom folosi:&lt;br /&gt;
#*&amp;#039;&amp;#039;&amp;#039;Build&amp;#039;&amp;#039;&amp;#039; [[Fișier:CLion_build_button.png | 16px]]&lt;br /&gt;
#*&amp;#039;&amp;#039;&amp;#039;Run&amp;#039;&amp;#039;&amp;#039; [[Fișier:CLion_run_button.png | 16px]]&lt;br /&gt;
#*&amp;#039;&amp;#039;&amp;#039;Debug&amp;#039;&amp;#039;&amp;#039; [[Fișier:CLion_debug_button.png | 16px]]&lt;br /&gt;
# Zona &amp;lt;span style=&amp;quot;color: gold&amp;quot;&amp;gt;galbenă&amp;lt;/span&amp;gt; - &amp;#039;&amp;#039;&amp;#039;Run&amp;#039;&amp;#039;&amp;#039; este zona în care se introduc datele de intrare şi se în care vor fi afişate datele de ieşire.&lt;br /&gt;
&lt;br /&gt;
== Exemplu de program în modul &amp;#039;&amp;#039;debug&amp;#039;&amp;#039; ==&lt;br /&gt;
&lt;br /&gt;
Copiați codul de mai jos înlocuind programul deja existent în fișierul &amp;#039;&amp;#039;main.c&amp;#039;&amp;#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot; line&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main(){&lt;br /&gt;
    printf(&amp;quot;Debugging program...\n&amp;quot;);&lt;br /&gt;
    int value = 0;&lt;br /&gt;
    value = value + 1;&lt;br /&gt;
    value++;&lt;br /&gt;
    value = value * 2;&lt;br /&gt;
    value -= 1;&lt;br /&gt;
    printf(&amp;quot;Value is now %d!\n&amp;quot;, value);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pentru a executa programul în modul debug trebuie sa introducem cel puţin un &amp;#039;&amp;#039;&amp;#039;breakpoint&amp;#039;&amp;#039;&amp;#039;. Astfel indicăm programului unde se va opri pentru a ne acorda control asupra execuţiei, având posibilitatea de a continua execuţia pas cu pas. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Introducem un nou breakpoint dând click în dreptul liniei de la care vrem să obţinem controlul. În cazul nostru, vom alege să indroducem un breakpoint chiar la definirea variabilei &amp;lt;code&amp;gt;value&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier:CLion_breakpoint.png | 1000px | Imaginea 4]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;lt;span style=&amp;quot;color: red; font-weight: bold&amp;quot;&amp;gt;Atenție:&amp;lt;/span&amp;gt; Când alegem locaţia unui breakpoint trebuie să ţinem cont de următorul aspect: în modul &amp;#039;&amp;#039;&amp;#039;debug&amp;#039;&amp;#039;&amp;#039; programul se va opri &amp;#039;&amp;#039;&amp;#039;înainte&amp;#039;&amp;#039;&amp;#039; de a executa linia în dreptul căreia a fost introdus breakpoint-ul!&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
În continuare, trebuie se pornim execuţia programului in modul debug [[Fișier:CLion_debug_button.png | 16px]]. Observăm apariţia ferestrei Debug (chenarul albastru, Imaginea 5) ce conţine următoarele:&lt;br /&gt;
#Zona &amp;lt;span style=&amp;quot;color: gold&amp;quot;&amp;gt;galbenă&amp;lt;/span&amp;gt; - fereastra cu toate variabilele declarate, împreună cu valorile lor. Momentan nu există nicio variabilă declarată (deoarece linia asupra la care ne-am oprit nu a fost încă executată!)&lt;br /&gt;
#Zona &amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;roşie&amp;lt;/span&amp;gt; - Permite trecerea între fereastra pentru procesul de depanare (&amp;#039;&amp;#039;&amp;#039;Debugger&amp;#039;&amp;#039;&amp;#039;) şi fereastra în care se afişează/introduc datele (&amp;#039;&amp;#039;&amp;#039;Console&amp;#039;&amp;#039;&amp;#039;)&lt;br /&gt;
#Zona &amp;lt;span style=&amp;quot;color: green&amp;quot;&amp;gt;verde&amp;lt;/span&amp;gt; - Conţine comenzile pentru controlul execuţiei în modul debug (comenzi numite &amp;#039;&amp;#039;&amp;#039;stepping actions&amp;#039;&amp;#039;&amp;#039;):&lt;br /&gt;
#*&amp;#039;&amp;#039;&amp;#039;Step over&amp;#039;&amp;#039;&amp;#039; [[Fișier:CLion_step_over.png | 16px]] - Execută comenzile de pe linia curentă şi trece la următoarea linie&lt;br /&gt;
#*&amp;#039;&amp;#039;&amp;#039;Step into&amp;#039;&amp;#039;&amp;#039; [[Fișier:CLion_step_into.png | 16px]] - Programul va executa linia curentă iar dacă pe linia curentă există un apel de funcție, se va opri pe prima linie din funcția respectivă. Această comandă este utilă atunci cand pe linia curentă se află un apel de funcţie şi vrem sa studiem comportamentul codului din interiorul funcţiei.&lt;br /&gt;
#*&amp;#039;&amp;#039;&amp;#039;Step out&amp;#039;&amp;#039;&amp;#039; [[Fișier:CLion_step_out.png | 16px]] - Termină de executat funcţia în care se află linia curentă&lt;br /&gt;
#*&amp;#039;&amp;#039;&amp;#039;Run to cursor&amp;#039;&amp;#039;&amp;#039; [[Fișier:CLion_run_to_cursor.png | 16px]] - Execută toate instructiunile până se intâlneste cursorul.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt; &amp;#039;&amp;#039;&amp;#039; Observație: &amp;#039;&amp;#039;&amp;#039; În modul debug, linia curentă este marcată de către mediul de dezvoltare prin colorarea acesteia cu albastru.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alte comenzi utile:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Resume program&amp;#039;&amp;#039;&amp;#039; [[Fișier:CLion_resume_project.png | 16px]] - Continuă execuţia pâmă la următorul breakpoint (dacă nu există, se va executa până la final)&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Stop&amp;#039;&amp;#039;&amp;#039; [[Fișier:CLion_stop.png | 16px]] - Se opreşte definitv execuţia programului, împreună cu modul debug&lt;br /&gt;
&lt;br /&gt;
[[Fișier:CLion_debug_process.png | 1000px | Imaginea 5]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pentru a exersa utilizarea sistemului de debug, realizați următoarele operații:&lt;br /&gt;
# Parcurgeti tot programul folosind &amp;#039;&amp;#039;&amp;#039;Step Over&amp;#039;&amp;#039;&amp;#039; și vizualizând valoarea variabilei &amp;lt;code&amp;gt;value&amp;lt;/code&amp;gt; la fiecare pas, până la ultima linie din program, unde utilizați &amp;#039;&amp;#039;&amp;#039;Resume program&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
# Reporniți aplicația în modul de debug și puneți un al doilea &amp;#039;&amp;#039;breakpoint&amp;#039;&amp;#039; pe linia cu apelul funcției &amp;lt;code&amp;gt;printf&amp;lt;/code&amp;gt;. &lt;br /&gt;
# Folosiți &amp;#039;&amp;#039;&amp;#039;Resume program&amp;#039;&amp;#039;&amp;#039; pentru a ajunge la al doilea &amp;#039;&amp;#039;breakpoint&amp;#039;&amp;#039;.&lt;br /&gt;
# Odată ajunși acolo, folosiți &amp;#039;&amp;#039;&amp;#039;Step In&amp;#039;&amp;#039;&amp;#039; pentru a intra în funcția &amp;lt;code&amp;gt;printf&amp;lt;/code&amp;gt; (neavând codul sursă, veți vedea codul de asamblare obținut din dezasamblarea fișierului obiect de către GDB).&lt;br /&gt;
# Folosiți &amp;#039;&amp;#039;&amp;#039;Step Out&amp;#039;&amp;#039;&amp;#039; pentru a reveni în funcția &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; și apoi &amp;#039;&amp;#039;&amp;#039;Resume program&amp;#039;&amp;#039;&amp;#039; pentru a termina execuția.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Tips &amp;amp; Tricks ==&lt;br /&gt;
&lt;br /&gt;
=== Formatare automată a codului ===&lt;br /&gt;
&lt;br /&gt;
CLion oferă opțiunea de a formata automat codul prin selectarea &amp;#039;&amp;#039;&amp;#039;Code -&amp;gt; Reformat Code&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Ce probleme rezolvă această formatare automată:&lt;br /&gt;
* alinierea liniilor în funcție de blocurile de instrucțiuni și tipurile de &amp;#039;&amp;#039;statements&amp;#039;&amp;#039; de pe fiecare linie;&lt;br /&gt;
* plasarea sau eliminarea de spații acolo unde este necasar.&lt;br /&gt;
Ce probleme NU rezolvă această formatare automată;&lt;br /&gt;
* lipsa acoladelor de la blocurile &amp;#039;&amp;#039;&amp;#039;if&amp;#039;&amp;#039;&amp;#039; și &amp;#039;&amp;#039;&amp;#039;for&amp;#039;&amp;#039;&amp;#039;;&lt;br /&gt;
* numele insuficient de sugestive pentru numele de funcții, structuri sau variabile.&lt;br /&gt;
&lt;br /&gt;
Stilul conform căruia este modificat codul poate fi configurat folosind meniul &amp;lt;code&amp;gt;File|Settings|Editor|CodeStyle&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generare automată a codului ===&lt;br /&gt;
&lt;br /&gt;
CLion oferă posibilitatea de a genera fragmente de cod des înâlnite, precum constructori şi operatori în interiorul claselor. Această acţiune poate fi realizată folosind meniul &amp;lt;code&amp;gt; Code|Generate&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Altele ===&lt;br /&gt;
* [https://www.jetbrains.com/help/clion/using-todo.html TODO Comments]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt; Nu uitați de regulile următoare: [[Convenții de cod - C]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
= Exerciții =&lt;br /&gt;
&lt;br /&gt;
*Săptămâna 1&lt;br /&gt;
*# Realizați un proiect nou în Netbeans și scrieți un program care să citească de la tastatură un număr întreg fără semn &amp;lt;code&amp;gt;size&amp;lt;/code&amp;gt;. Se va aloca apoi dinamic în HEAP un vector de &amp;lt;code&amp;gt;size&amp;lt;/code&amp;gt; valori numerice întregi pe 16 biți. Scrieți o funcție care să citească &amp;lt;code&amp;gt;size&amp;lt;/code&amp;gt; valori de la tastatură și să le plaseze în vector. Scrieți apoi o funcție care să găsească valoarea maximă din vector. Apelați în &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; funcțiile de mai sus și afișați valoarea obținută pe ecran. Nu uitați să dezalocați memoria alocată. Pentru funcțiile de alocare și dezalocare de memorie, puteți recapitula [[PC Laborator 12]].&lt;br /&gt;
*# Modificați programul de mai sus astfel încât citirea variabilei &amp;lt;code&amp;gt;size&amp;lt;/code&amp;gt; cât și alocarea de memorie să fie făcută într-o funcție separată. Această funcție trebuie să întoarcă un pointer cu adresa de început a vectorului dar și dimensiunea acestuia.&lt;br /&gt;
*# Realizați un alt proiect în Netbeans și scrieți un program care să citească de la tastatură un șir de caractere de lungime maximă 255. Folosind aritmetica pointerilor și o singură buclă &amp;#039;&amp;#039;&amp;#039;for&amp;#039;&amp;#039;&amp;#039;, afișați pe ecran doar cifrele din șirul citit.&lt;br /&gt;
* Săptămâna 2&lt;br /&gt;
*# Realizați un proiect nou în Netbeans și scrieți un program care să citească de la tastatură un număr întreg fără semn &amp;lt;code&amp;gt;size&amp;lt;/code&amp;gt;. Scrieți apoi o funcție care să aloce memorie pentru un șir de caractere care să conțină de &amp;lt;code&amp;gt;size&amp;lt;/code&amp;gt; ori secvența &amp;quot;ha&amp;quot; (spre exemplu, pentru size == 3, string-ul va trebui să conțină &amp;quot;hahaha&amp;quot;), să umple șirul cu numărul cerut de &amp;quot;ha&amp;quot;-uri și să întoarcă adresa memoriei alocate. În funcția &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; afișați șirul de caractere (nu uitați la alocare de terminatorul de sfârșit de șir). Nu uitați să dezalocați memoria. Pentru funcțiile de alocare și dezalocare de memorie, puteți recapitula [[PC Laborator 12]].&lt;br /&gt;
*# Modificați programul de mai sus astfel încât în loc de secvența &amp;quot;ha&amp;quot;, secvența repetată să fie citită de la tastatură cu &amp;lt;code&amp;gt;scanf&amp;lt;/code&amp;gt; și trimisă ca argument funcției de alocare. &lt;br /&gt;
*# Realizați un alt proiect în Netbeans și scrieți un program care să citească de la tastatură un șir de caractere de lungime maximă 255. Scrieți o singură funcție care să numere și să întoarcă numărul de litere și numărul de cifre din șirul citit. Aceste valori se vor afișa pe ecran în funcția &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt;.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Laborator 2 (Saptamana 2) ==&lt;br /&gt;
&lt;br /&gt;
= Exemplu =&lt;br /&gt;
&lt;br /&gt;
Impreună cu echipa de la firmă ați inventat un nou algoritm de generare de numere pseudo-aleatoare. Pentru a valida că generatorul poate fi folosit în algoritmi criptografici (cryptographically secure) trebuie să implementați și să rulați o baterie de teste. Unul din aceste teste verifică numărul de apariții pentru fiecare secvență posibilă de doi biți: 00, 01, 10 și 11 cât și raportul între numărul de biți de 0 și de 1. Pentru ca secvența de biți să fie aleatoare, trebuie ca numărul de apariții pentru fiecare din cele patru perechi să fie aproximativ egale și în același timp numărul de biți de 0 să fie aproximativ egal cu cei de 1. Mai precis, trebuie ca raporturile R1 dintre numărul de apariții a perechii care apare de cele mai multe ori și numărul de apariții a perechii care apare de cele mai puține ori, cât și raportul R2 între numărul de apariții ale celui mai frecvent bit și numărul de apariții ale celui mai puțin frecvent bit să fie mai mici sau egale cu 110%.&lt;br /&gt;
&lt;br /&gt;
Cerință&lt;br /&gt;
&lt;br /&gt;
Dându-se un număr n reprezentând numărul de biți generat de RNG și secvența de n biți, să se calculeze raporturile R1 și R2 și să se decidă dacă generatorul este valid sau nu.&lt;br /&gt;
&lt;br /&gt;
Date de intrare&lt;br /&gt;
&lt;br /&gt;
Pe prima linie se află n, numărul de biți generați. Pe a doua linie se află o secvență continuă de n biți (valori de 0 sau 1), ne-separați prin spații.&lt;br /&gt;
&lt;br /&gt;
Date de ieșire&lt;br /&gt;
&lt;br /&gt;
Programul va afișa în consolă (pe stream-ul stdout) pe prima linie raporturile R1 și R2 calculate conform descrierii, valori fracționare cu două zecimale, separate prin spațiu, iar pe a doua linie valoarea 1 dacă generatorul este valid sau 0 dacă nu este.&lt;br /&gt;
&lt;br /&gt;
Restricții&lt;br /&gt;
&lt;br /&gt;
1. 2 ≤ n ≤ 10000, n este par&lt;br /&gt;
&lt;br /&gt;
Intrare:&lt;br /&gt;
&lt;br /&gt;
18&lt;br /&gt;
&lt;br /&gt;
101100110111100001&lt;br /&gt;
&lt;br /&gt;
Iesire:&lt;br /&gt;
&lt;br /&gt;
1.50 1.25&lt;br /&gt;
&lt;br /&gt;
0&lt;br /&gt;
&lt;br /&gt;
Secvența are 18 biți și extragând secvențele de câte doi biți se observă că 00 apare de două ori, 01 apare de două ori, 10 apare de două ori și 11 apare de trei ori. Calculând raportul dintre maxim și minim (3/2) obținem 150%. Numărul de biți 1 este 10 și numărul de biți 0 este 8. Raportul este 10/8 = 125%. Pentru că cel puțin unul din raporturi este mai mare strict decât 110%, se afișează pe a doua linie 0 (secvența nu este aleatoare).&lt;br /&gt;
&lt;br /&gt;
Input:&lt;br /&gt;
&lt;br /&gt;
24&lt;br /&gt;
&lt;br /&gt;
101100110111100001010010&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
1.00 1.00&lt;br /&gt;
&lt;br /&gt;
1&lt;br /&gt;
&lt;br /&gt;
În acest exemplu toate perechile apar de trei ori, deci raportul R1 este 100%. Numărul de biți 1 este 12 și este egal cu numărul de biți 0, deci și raportul R2 este 100%. Astfel, pentru că ambele raporturi sunt mai mici sau egale decât 110%, pe a doua linie se afișează 1 (secvența este aleatoare).&lt;br /&gt;
&lt;br /&gt;
=Problema 1 (saptamana 2)=&lt;br /&gt;
&lt;br /&gt;
Pentru a deveni competitivă în domeniul Blockchain și a monedelor virtuale, firma la care lucrați a dezvoltat un algoritm de hashing inovativ pentru valori numerice. Algoritmul funcționează în modul următor: asupra unui număr natural se face următoarea transformare: dacă numărul are cel puțin două cifre, se iau pe rând din număr câte două cifre vecine și se scade cea mai mică din cea mai mare. Cu cifrele astfel obținute se formează un nou număr. De exemplu, pentru numărul 5734, din cifrele 5 și 7 se obține 2, din 7 și 3 se obține 4 iar din 3 și 4 se obține 1. Formăm deci un nou număr 241, căruia i se poate aplica aceeași transformare, obținându-se 23. Din 23, prin același procedeu, obținem 1.  Dacă numărul este format dintr-o singură cifră, transformarea îl lasă nemodificat. Hash-ul se calculează realizând succesiv transformarea asupra numărului original și apoi asupra rezultatului obținut, de k ori, și sumând toate rezultatele parțiale și rezultatul final (excluzând valoarea de start).&lt;br /&gt;
&lt;br /&gt;
Cerință&lt;br /&gt;
&lt;br /&gt;
Se dau două numere naturale N şi k și apoi încă N numere naturale. Se cere să se determine valoarea maximă a hash-ului care se va obține aplicând algoritmul de hash celor N numere folosind k pentru numărul de iterații de transformare, conform descrierii de mai sus.&lt;br /&gt;
&lt;br /&gt;
Date de intrare&lt;br /&gt;
&lt;br /&gt;
La intrarea programului se prezintă pe prima linie două numere naturale N și k, separate prin spațiu. Pe a doua linie se află cele N numere, separate tot prin spații. Liniile de intrare se încheie cu caracterul newline (\n), obținut prin apăsarea tastei Enter.&lt;br /&gt;
&lt;br /&gt;
Date de ieșire&lt;br /&gt;
&lt;br /&gt;
Programul va afișa la consolă (pe stream-ul stdout) un singur număr, reprezentând valoarea maximă dintre toate hash-urile obținute conform procedeului descris anterior.&lt;br /&gt;
&lt;br /&gt;
Restricții&lt;br /&gt;
&lt;br /&gt;
1. 0 &amp;lt; N &amp;lt; 10000&lt;br /&gt;
2. 0 ≤ ni &amp;lt; 100000000 (valorile asupra cărora se va aplica hash-ul).&lt;br /&gt;
3. 0 &amp;lt; k &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
Exemple:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Intrare:&lt;br /&gt;
&lt;br /&gt;
2 2&lt;br /&gt;
5734 1234&lt;br /&gt;
&lt;br /&gt;
Ieșire:&lt;br /&gt;
&lt;br /&gt;
264&lt;br /&gt;
&lt;br /&gt;
Explicații:&lt;br /&gt;
Se aplică de 2 ori succesiunea de transformări pentru fiecare din cele două valori.&lt;br /&gt;
Astfel, din 5734 se obține 241, iar din 241 se obține 23. Adunând numerele 241 și 23 se obține rezultatul: 264.&lt;br /&gt;
Din 1234 se obține 111 iar din 111 se obține 0. Sumând cele două valori rezultatul este 111.&lt;br /&gt;
Valoarea maximă este 264.&lt;br /&gt;
&lt;br /&gt;
Intrare:&lt;br /&gt;
&lt;br /&gt;
2 3&lt;br /&gt;
2228 6&lt;br /&gt;
&lt;br /&gt;
Iesire:&lt;br /&gt;
&lt;br /&gt;
18&lt;br /&gt;
&lt;br /&gt;
Se aplică transformările de 3 ori asupra celor două numere.&lt;br /&gt;
Pentru 2228, în urma primei transformări se obțin cifrele 0, 0, 6, din care formăm secvența 006, adică numărul 6. La următoarele 2 transformări rezultă aceeași valoare, 6. Prin urmare suma obținută este: 6+6+6 = 18.&lt;br /&gt;
Pentru valoarea 6, cele 3 transformări vor produce tot 6 iar suma va fi tot 18.&lt;br /&gt;
Rezultatul este deci 18.&lt;br /&gt;
&lt;br /&gt;
=Problema 2(saptamana 2)=&lt;br /&gt;
&lt;br /&gt;
Doamna secretară trebuie să stabilească numărul de studenți ce vor lua bursă de merit în anul universitar următor și să identifice studentul care va lua bursa de performanță (există o singură bursă de performanță). Are la dispoziție lista tuturor studenților și notele obținute de aceștia la diversele discipline. Bursa de performanță se acordă studentului integralist cu media cea mai mare. Bursele de merit se acordă în ordinea descrescătoare a mediilor, în limita numărului maxim de burse, tuturor studenților integraliști care au media generală peste 8.00.&lt;br /&gt;
&lt;br /&gt;
Cerinţă&lt;br /&gt;
&lt;br /&gt;
Stabiliți ce student va lua bursă de performanță și câți studenți vor lua bursă de merit în anul universitar următor.&lt;br /&gt;
&lt;br /&gt;
Date de intrare&lt;br /&gt;
&lt;br /&gt;
Se vor citi de la tastatură (fluxul stdin) următoarele date:&lt;br /&gt;
&lt;br /&gt;
* Trei numere întregi pozitive m, n, p separate prin spaţiu, reprezentând&lt;br /&gt;
&lt;br /&gt;
*   m – numărul de studenţi,&lt;br /&gt;
&lt;br /&gt;
*  n – numărul de discipline,&lt;br /&gt;
&lt;br /&gt;
*   p – numărul de burse de merit disponibile;&lt;br /&gt;
&lt;br /&gt;
*        2*m linii de pe care se citesc, în ordine, în formatul:&lt;br /&gt;
&lt;br /&gt;
*   &amp;lt;NS&amp;gt;, un şir de caractere reprezentând numele studentului;&lt;br /&gt;
&lt;br /&gt;
*   &amp;lt;N1&amp;gt;  &amp;lt;N2&amp;gt;  ...  &amp;lt;Nn&amp;gt;, n numere întregi din intervalul 1 – 10 separate prin spaţiu reprezentând notele obţinute de respectivul student la cele n discipline;&lt;br /&gt;
&lt;br /&gt;
Toate liniile conţinând date de intrare sunt finalizate cu caracterul newline (tasta Enter).&lt;br /&gt;
&lt;br /&gt;
Date de ieşire&lt;br /&gt;
&lt;br /&gt;
Programul va afișa pe ecran (stream-ul standard de ieșire):&lt;br /&gt;
&lt;br /&gt;
Pe prima linie: numărul de studenți ce vor lua bursă de merit&lt;br /&gt;
Pe a doua linie: numele studentului care va lua bursă de performanță și media media lui (număr fracționar cu 2 zecimale) separate prin spațiu.&lt;br /&gt;
Restricţii şi precizări&lt;br /&gt;
&lt;br /&gt;
1. 0 &amp;lt; m, n, p &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
Garantăm că nu vor exista studenți cu medii egale.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Intrare:&lt;br /&gt;
&lt;br /&gt;
6 3 2&lt;br /&gt;
&lt;br /&gt;
George Popescu&lt;br /&gt;
&lt;br /&gt;
9 9 9&lt;br /&gt;
&lt;br /&gt;
Dan Pop&lt;br /&gt;
&lt;br /&gt;
10 4 10&lt;br /&gt;
&lt;br /&gt;
Ionela Cristescu&lt;br /&gt;
&lt;br /&gt;
6 8 8&lt;br /&gt;
&lt;br /&gt;
Ilie David&lt;br /&gt;
&lt;br /&gt;
10 10 10&lt;br /&gt;
&lt;br /&gt;
Georgiana Fus&lt;br /&gt;
&lt;br /&gt;
9 10 10&lt;br /&gt;
&lt;br /&gt;
Cristian Oprescu&lt;br /&gt;
&lt;br /&gt;
8 9 9&lt;br /&gt;
&lt;br /&gt;
Ieşire:&lt;br /&gt;
&lt;br /&gt;
2&lt;br /&gt;
&lt;br /&gt;
Ilie David 10.00&lt;br /&gt;
&lt;br /&gt;
Explicaţie:&lt;br /&gt;
&lt;br /&gt;
Ilie David este studentul integralist cu cea mai mare medie, deci va primi bursa de performanță.&lt;br /&gt;
&lt;br /&gt;
Dan Pop nu poate primi bursă pentru că nu este integralist.&lt;br /&gt;
&lt;br /&gt;
Ionela Cristescu nu poate primi bursă pentru că are media sub 8.&lt;br /&gt;
&lt;br /&gt;
Georgiana Fus, George Popescu și Cristian Oprescu îndeplinesc condițiile de a primi bursă, însă doar doi dintre ei vor fi bursieri pentru că numărul maxim de burse de merit este 2.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Intrare:&lt;br /&gt;
&lt;br /&gt;
6 3 5&lt;br /&gt;
&lt;br /&gt;
George Popescu&lt;br /&gt;
&lt;br /&gt;
9 9 9&lt;br /&gt;
&lt;br /&gt;
Dan Pop&lt;br /&gt;
&lt;br /&gt;
10 4 10&lt;br /&gt;
&lt;br /&gt;
Ionela Cristescu&lt;br /&gt;
&lt;br /&gt;
6 8 8&lt;br /&gt;
&lt;br /&gt;
Ilie David&lt;br /&gt;
&lt;br /&gt;
10 10 10&lt;br /&gt;
&lt;br /&gt;
Georgiana Fus&lt;br /&gt;
&lt;br /&gt;
9 10 10&lt;br /&gt;
&lt;br /&gt;
Cristian Oprescu&lt;br /&gt;
&lt;br /&gt;
8 9 9&lt;br /&gt;
&lt;br /&gt;
Ieşire:&lt;br /&gt;
&lt;br /&gt;
3&lt;br /&gt;
&lt;br /&gt;
Ilie David 10.00&lt;br /&gt;
&lt;br /&gt;
Explicaţie:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ilie David este studentul integralist cu cea mai mare medie, deci va primi bursa de performanță.&lt;br /&gt;
&lt;br /&gt;
Dan Pop nu poate primi bursă pentru că nu este integralist.&lt;br /&gt;
&lt;br /&gt;
Ionela Cristescu nu poate primi bursă pentru că are media sub 8.&lt;br /&gt;
&lt;br /&gt;
Georgiana Fus, George Popescu și Cristian Oprescu vor primi cu toții bursă de merit pentru că îndeplinesc condițiile, iar numărul maxim de burse de merit este 5.&lt;br /&gt;
&lt;br /&gt;
= Probleme propuse =&lt;br /&gt;
&lt;br /&gt;
Problemele de mai jos au anumite erori care trebuie gasite si raparate folosind debugger-ul.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Problema 1 ==&lt;br /&gt;
Programul de mai jos ar trebui să citească de la tastatură un șir de maxim 1024 caractere și să afișeze câte litere mici, câte litere mari și câte cifre conține acest șir. Totuși acest program are o eroare. Folosiți debugger-ul pentru a o identifica și repara.&lt;br /&gt;
&lt;br /&gt;
Exemplu de intrare:&lt;br /&gt;
&lt;br /&gt;
Ana a plecat la piata si a cumparat 10 saci de cartofi.&lt;br /&gt;
&lt;br /&gt;
Ieșirea pentru intrarea de mai sus:&lt;br /&gt;
&lt;br /&gt;
40 1 2&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C++&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    char c;&lt;br /&gt;
    unsigned uppercaseChars = 0;&lt;br /&gt;
    unsigned lowecaseChars = 0;    &lt;br /&gt;
    unsigned digits = 0;&lt;br /&gt;
    &lt;br /&gt;
    while(scanf(&amp;quot;%c&amp;quot;, c) != EOF) {&lt;br /&gt;
        if(c &amp;gt; &amp;#039;a&amp;#039; || c &amp;lt; &amp;#039;z&amp;#039;) {&lt;br /&gt;
            lowecaseChars++;&lt;br /&gt;
        } else if(c &amp;gt; &amp;#039;A&amp;#039; &amp;amp;&amp;amp; c &amp;lt; &amp;#039;Z&amp;#039;) {&lt;br /&gt;
            uppercaseChars++;&lt;br /&gt;
        } else {&lt;br /&gt;
            digits++;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    printf(&amp;quot;%u %u %u&amp;quot;, lowecaseChars, uppercaseChars, digits);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Problema 2 ==&lt;br /&gt;
Un tehnician a măsurat un număr mare n de rezistori şi a obţinut n valori de rezistenţe. Tehnicianul ştie că rezistorii sunt de acelaşi fel, dar pentru că sunt vechi şi codul culorilor nu mai este vizibil, doreşte să calculeze valoarea nominală a rezistenţei (R) şi dispersia valorilor, (S). Apoi, având aceste valori, el vrea să determine câte din rezistenţele testate (procentual) se încadrează în intervalul [R – S; R + S]. Rezistenţa nominală se calculează ca media aritmetică a valorilor rezistenţelor, iar formula dispersiei este dată mai jos.&lt;br /&gt;
&lt;br /&gt;
Cerinţă&lt;br /&gt;
Dându-se un număr n de rezistori şi valorile rezistenţelor acestora Ri (i = 1, ..., n), să se determine procentul rezistoarelor care au rezistenţa în intervalul [R– S; R + S].&lt;br /&gt;
&lt;br /&gt;
Date de intrare&lt;br /&gt;
Pe prima linie se află numărul întreg n. Pe următoarea linie, separate printr-un spaţiu, sunt n valori fracţionare de rezistenţe (în ohmi).&lt;br /&gt;
&lt;br /&gt;
Date de ieşire&lt;br /&gt;
Se va afişa o singură valoare fracţionară, cu exact două zecimale reprezentând procentul de rezistori cu rezistenţa în intervalul [R – S; R + S].&lt;br /&gt;
&lt;br /&gt;
Restricţii şi precizări:&lt;br /&gt;
&lt;br /&gt;
1 &amp;lt; n &amp;lt; 1000&lt;br /&gt;
&lt;br /&gt;
1.0 &amp;lt;= Ri &amp;lt;= 10000.0 &lt;br /&gt;
&lt;br /&gt;
S=sqrt( pow(sum(R[i]−R),2) / n )&lt;br /&gt;
&lt;br /&gt;
Exemplu:&lt;br /&gt;
&lt;br /&gt;
Input =&lt;br /&gt;
10&lt;br /&gt;
&lt;br /&gt;
76 1 20 37 19 92 61 96 37 77&lt;br /&gt;
&lt;br /&gt;
Output = &lt;br /&gt;
50.00&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C++&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;unordered_map&amp;gt;&lt;br /&gt;
#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;
#include &amp;lt;cstdio&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    int n;&lt;br /&gt;
    double values[n];&lt;br /&gt;
    scanf(&amp;quot;%d&amp;quot;, &amp;amp;n);&lt;br /&gt;
    int sum = 0;&lt;br /&gt;
    for(int i = 0; i &amp;lt; n; i++){&lt;br /&gt;
        scanf(&amp;quot;%lf&amp;quot;, &amp;amp;values[i]);&lt;br /&gt;
        sum += values[i];&lt;br /&gt;
    }&lt;br /&gt;
    double average = sum / n;&lt;br /&gt;
    double dispersion = 0;&lt;br /&gt;
    for(int i = 0; i &amp;lt; n; i++){&lt;br /&gt;
        dispersion += (values[i] - average) * (values[i] - average);&lt;br /&gt;
    }&lt;br /&gt;
    dispersion = sqrt(dispersion / n);&lt;br /&gt;
    int larger = 0;&lt;br /&gt;
    for(int i = 0; i &amp;lt; n; i++) {&lt;br /&gt;
        if(values[i] &amp;gt;= average - dispersion &lt;br /&gt;
                &amp;amp;&amp;amp; values[i] &amp;lt;= average + dispersion) larger++;&lt;br /&gt;
    }&lt;br /&gt;
    printf(&amp;quot;%.2lf&amp;quot;, 100 * larger / n);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Problema 3 ==&lt;br /&gt;
Sunteți angajați de ministerul învățamântului să scrieți un program care să listeze toți elevii care au promovat examenul de bacalaureat. De la tastatură se introduce un număr n reprezentând numărul de candidați, apoi pe următoarele n linii, un nume de elev (format din exact două cuvinte: nume și prenume) și notele la cele patru materii, format fracționar, valori între 1 și 10. Să se afișeze toți studenții care au promovat examenul de bacalaureat (media mai mare ca 6, fiecare notă mai mare ca 5) în ordinea inversă a introducerii lor, împreună cu media de promovare, afișată cu fix două zecimale.&lt;br /&gt;
&lt;br /&gt;
Exemplu de date de intrare:&lt;br /&gt;
&lt;br /&gt;
4&lt;br /&gt;
&lt;br /&gt;
Gheorghe Ghita 8 7 5.5 10&lt;br /&gt;
&lt;br /&gt;
Vuia Vasile 4 10 10 10&lt;br /&gt;
&lt;br /&gt;
Andreescu Andra 9 10 9 10&lt;br /&gt;
&lt;br /&gt;
Elenescu Elena 5 5 5 5&lt;br /&gt;
&lt;br /&gt;
Ieșirea pentru intrarea de mai sus:&lt;br /&gt;
&lt;br /&gt;
Andreescu Andra 9.50&lt;br /&gt;
&lt;br /&gt;
Gheorghe Ghita 7.63&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C++&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
struct studenti{&lt;br /&gt;
    char n[128];&lt;br /&gt;
    char m[128];&lt;br /&gt;
    float a,b,c,d;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
int main(){&lt;br /&gt;
    int n,i;&lt;br /&gt;
    struct studenti s[n];&lt;br /&gt;
    scanf(&amp;quot;%d&amp;quot;, &amp;amp;n);&lt;br /&gt;
    for(i=0;i&amp;lt;n;i++)&lt;br /&gt;
        scanf(&amp;quot;%s%s%f%f%f%f&amp;quot;,&amp;amp;s[i].n,&amp;amp;s[i].m,&amp;amp;s[i].a,&amp;amp;s[i].b,&amp;amp;s[i].c,&amp;amp;s[i].d);&lt;br /&gt;
&lt;br /&gt;
    for(i=n;i&amp;gt;0;i--){&lt;br /&gt;
        float m=(s[i].a+s[i].b+s[i].c+s[i].d)/4;&lt;br /&gt;
        if(s[i].a&amp;gt;5&amp;amp;&amp;amp;s[i].b&amp;gt;5&amp;amp;&amp;amp;s[i].c&amp;gt;5&amp;amp;&amp;amp;s[i].d&amp;gt;5||m&amp;gt;=6){&lt;br /&gt;
            printf(&amp;quot;%s %s %.2f\n&amp;quot;,s[i].n,s[i].m,m);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_7&amp;diff=8376</id>
		<title>Lab 7</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_7&amp;diff=8376"/>
		<updated>2026-09-26T10:40:37Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: Pagină nouă: = Lab 7 — Final Integration, Demonstration and Code Review =  == Objectives ==  This laboratory is the final project session.  The objective is to demonstrate that the semester p...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 7 — Final Integration, Demonstration and Code Review =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
This laboratory is the final project session.&lt;br /&gt;
&lt;br /&gt;
The objective is to demonstrate that the semester project is complete, reproducible, technically understood by the team and ready for evaluation.&lt;br /&gt;
&lt;br /&gt;
Unlike the previous laboratories, this session is centered on:&lt;br /&gt;
&lt;br /&gt;
* final integration;&lt;br /&gt;
* project demonstration;&lt;br /&gt;
* architecture explanation;&lt;br /&gt;
* code walkthrough;&lt;br /&gt;
* technical discussion;&lt;br /&gt;
* verification of tests and documentation;&lt;br /&gt;
* evaluation of project quality;&lt;br /&gt;
* final submission.&lt;br /&gt;
&lt;br /&gt;
By the end of this laboratory, each team should be able to:&lt;br /&gt;
&lt;br /&gt;
* run the project from a clean and documented setup;&lt;br /&gt;
* demonstrate the primary application workflow;&lt;br /&gt;
* explain the architecture of the project;&lt;br /&gt;
* explain the responsibility of the main modules;&lt;br /&gt;
* justify important design decisions;&lt;br /&gt;
* explain how errors are handled;&lt;br /&gt;
* explain how the project is tested;&lt;br /&gt;
* discuss one important technical problem encountered during development;&lt;br /&gt;
* identify known limitations;&lt;br /&gt;
* show that the repository is complete and clean;&lt;br /&gt;
* answer technical questions about its own implementation.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== 1. Final laboratory structure ==&lt;br /&gt;
&lt;br /&gt;
A possible structure for the two-hour session is:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Time&lt;br /&gt;
! Activity&lt;br /&gt;
|-&lt;br /&gt;
| 0–10 min&lt;br /&gt;
| Final setup and repository verification&lt;br /&gt;
|-&lt;br /&gt;
| 10–80 min&lt;br /&gt;
| Project demonstrations&lt;br /&gt;
|-&lt;br /&gt;
| 80–105 min&lt;br /&gt;
| Code walkthrough and technical discussion&lt;br /&gt;
|-&lt;br /&gt;
| 105–115 min&lt;br /&gt;
| Final repository and documentation checks&lt;br /&gt;
|-&lt;br /&gt;
| 115–120 min&lt;br /&gt;
| Submission confirmation&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The exact timing depends on the number of teams.&lt;br /&gt;
&lt;br /&gt;
== 2. What should be ready before the session ==&lt;br /&gt;
&lt;br /&gt;
Before the final laboratory begins:&lt;br /&gt;
&lt;br /&gt;
* the core feature set should be frozen;&lt;br /&gt;
* no P0 defects should remain;&lt;br /&gt;
* the main workflow should work end-to-end;&lt;br /&gt;
* tests should pass;&lt;br /&gt;
* installation instructions should be complete;&lt;br /&gt;
* the README should be up to date;&lt;br /&gt;
* known limitations should be documented;&lt;br /&gt;
* the repository should not contain secrets or unnecessary generated files;&lt;br /&gt;
* the team should have a stable demonstration scenario.&lt;br /&gt;
&lt;br /&gt;
Lab 7 should not be used for major architecture changes.&lt;br /&gt;
&lt;br /&gt;
== 3. Final project state ==&lt;br /&gt;
&lt;br /&gt;
The expected state is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
implemented&lt;br /&gt;
    ↓&lt;br /&gt;
tested&lt;br /&gt;
    ↓&lt;br /&gt;
documented&lt;br /&gt;
    ↓&lt;br /&gt;
reproducible&lt;br /&gt;
    ↓&lt;br /&gt;
demonstrable&lt;br /&gt;
    ↓&lt;br /&gt;
explainable&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A project that only runs on the original developer&amp;#039;s machine is not yet ready for final evaluation.&lt;br /&gt;
&lt;br /&gt;
== 4. Demonstration first, slides second ==&lt;br /&gt;
&lt;br /&gt;
The primary evidence of a working project is the software itself.&lt;br /&gt;
&lt;br /&gt;
The demonstration should focus on:&lt;br /&gt;
&lt;br /&gt;
# starting the application;&lt;br /&gt;
# providing representative input;&lt;br /&gt;
# executing the main workflow;&lt;br /&gt;
# showing the output;&lt;br /&gt;
# showing one relevant error condition;&lt;br /&gt;
# explaining the result.&lt;br /&gt;
&lt;br /&gt;
Slides may be used for context, but they should not replace a live demonstration.&lt;br /&gt;
&lt;br /&gt;
== 5. Recommended demonstration structure ==&lt;br /&gt;
&lt;br /&gt;
A concise demonstration can follow:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Problem&lt;br /&gt;
  ↓&lt;br /&gt;
Input&lt;br /&gt;
  ↓&lt;br /&gt;
Execution&lt;br /&gt;
  ↓&lt;br /&gt;
Result&lt;br /&gt;
  ↓&lt;br /&gt;
Failure case&lt;br /&gt;
  ↓&lt;br /&gt;
Short technical explanation&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The demonstration should show the central objective of the project clearly.&lt;br /&gt;
&lt;br /&gt;
== 6. Demonstration time ==&lt;br /&gt;
&lt;br /&gt;
The demonstration should be concise.&lt;br /&gt;
&lt;br /&gt;
A reasonable target is approximately:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
5–8 minutes&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
depending on project complexity.&lt;br /&gt;
&lt;br /&gt;
The objective is not to show every implemented feature.&lt;br /&gt;
&lt;br /&gt;
Show the most important workflow reliably.&lt;br /&gt;
&lt;br /&gt;
== 7. Demonstration reliability ==&lt;br /&gt;
&lt;br /&gt;
Avoid a demonstration that depends on:&lt;br /&gt;
&lt;br /&gt;
* unstable public services;&lt;br /&gt;
* unavailable hardware;&lt;br /&gt;
* unknown network connectivity;&lt;br /&gt;
* private local files;&lt;br /&gt;
* credentials not available during evaluation;&lt;br /&gt;
* large downloads;&lt;br /&gt;
* manually prepared state that cannot be reproduced.&lt;br /&gt;
&lt;br /&gt;
If an external dependency is required, prepare a safe fallback demonstration strategy where possible.&lt;br /&gt;
&lt;br /&gt;
== 8. Representative input ==&lt;br /&gt;
&lt;br /&gt;
The demonstration should use input representative of the actual project.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Project type&lt;br /&gt;
! Representative input&lt;br /&gt;
|-&lt;br /&gt;
| image processing&lt;br /&gt;
| real or realistic image&lt;br /&gt;
|-&lt;br /&gt;
| data analysis&lt;br /&gt;
| meaningful dataset&lt;br /&gt;
|-&lt;br /&gt;
| networking&lt;br /&gt;
| valid application message or packet&lt;br /&gt;
|-&lt;br /&gt;
| hardware&lt;br /&gt;
| real device or recorded representative data&lt;br /&gt;
|-&lt;br /&gt;
| monitoring&lt;br /&gt;
| realistic metric stream&lt;br /&gt;
|-&lt;br /&gt;
| file processing&lt;br /&gt;
| correctly structured input file&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Avoid demonstrating only trivial toy input when the project is designed for a more realistic workload.&lt;br /&gt;
&lt;br /&gt;
== 9. Show one error condition ==&lt;br /&gt;
&lt;br /&gt;
A useful final demonstration includes one expected failure.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&lt;br /&gt;
* invalid input;&lt;br /&gt;
* missing file;&lt;br /&gt;
* malformed configuration;&lt;br /&gt;
* unavailable device;&lt;br /&gt;
* invalid packet;&lt;br /&gt;
* unsupported image format;&lt;br /&gt;
* timeout.&lt;br /&gt;
&lt;br /&gt;
The goal is to show that the project handles predictable failure conditions deliberately.&lt;br /&gt;
&lt;br /&gt;
== 10. Architecture explanation ==&lt;br /&gt;
&lt;br /&gt;
Each team should be able to explain the architecture in approximately one or two minutes.&lt;br /&gt;
&lt;br /&gt;
A good explanation identifies:&lt;br /&gt;
&lt;br /&gt;
* inputs;&lt;br /&gt;
* main components;&lt;br /&gt;
* data flow;&lt;br /&gt;
* external dependencies;&lt;br /&gt;
* outputs.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
CLI&lt;br /&gt;
 │&lt;br /&gt;
 ▼&lt;br /&gt;
Application service&lt;br /&gt;
 │&lt;br /&gt;
 ├── parser&lt;br /&gt;
 ├── processing pipeline&lt;br /&gt;
 └── storage&lt;br /&gt;
        │&lt;br /&gt;
        ▼&lt;br /&gt;
     output file&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 11. Architecture diagram ==&lt;br /&gt;
&lt;br /&gt;
A simple architecture diagram is sufficient.&lt;br /&gt;
&lt;br /&gt;
Avoid diagrams that show every function or variable.&lt;br /&gt;
&lt;br /&gt;
Focus on major components.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
main&lt;br /&gt;
 ├── config&lt;br /&gt;
 ├── service&lt;br /&gt;
 │    ├── model&lt;br /&gt;
 │    └── processor&lt;br /&gt;
 └── storage&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 12. Explain responsibilities ==&lt;br /&gt;
&lt;br /&gt;
For each main module, the team should be able to complete:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
This module is responsible for ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Module&lt;br /&gt;
! Responsibility&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;models.py&amp;lt;/code&amp;gt;&lt;br /&gt;
| application data structures&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;services.py&amp;lt;/code&amp;gt;&lt;br /&gt;
| central application logic&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;storage.py&amp;lt;/code&amp;gt;&lt;br /&gt;
| persistent data access&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;protocol.py&amp;lt;/code&amp;gt;&lt;br /&gt;
| message encoding and decoding&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;main.py&amp;lt;/code&amp;gt;&lt;br /&gt;
| application startup and orchestration&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 13. Code walkthrough ==&lt;br /&gt;
&lt;br /&gt;
The code walkthrough should not attempt to read the entire repository.&lt;br /&gt;
&lt;br /&gt;
Choose the most important implementation path.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
main()&lt;br /&gt;
  ↓&lt;br /&gt;
load configuration&lt;br /&gt;
  ↓&lt;br /&gt;
create service&lt;br /&gt;
  ↓&lt;br /&gt;
process input&lt;br /&gt;
  ↓&lt;br /&gt;
produce result&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Explain how the main workflow maps to the source code.&lt;br /&gt;
&lt;br /&gt;
== 14. What to show during code review ==&lt;br /&gt;
&lt;br /&gt;
Useful areas include:&lt;br /&gt;
&lt;br /&gt;
* application entry point;&lt;br /&gt;
* core processing logic;&lt;br /&gt;
* one important model or class;&lt;br /&gt;
* one error-handling path;&lt;br /&gt;
* one representative test;&lt;br /&gt;
* one external integration boundary.&lt;br /&gt;
&lt;br /&gt;
Do not spend presentation time showing trivial boilerplate.&lt;br /&gt;
&lt;br /&gt;
== 15. Explain one design decision ==&lt;br /&gt;
&lt;br /&gt;
Each team should prepare one important design decision.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Why was composition used instead of inheritance?&lt;br /&gt;
&lt;br /&gt;
Why was JSON selected for persistence?&lt;br /&gt;
&lt;br /&gt;
Why was a pipeline separated into stages?&lt;br /&gt;
&lt;br /&gt;
Why was dependency injection used?&lt;br /&gt;
&lt;br /&gt;
Why was the protocol parser separated from network transport?&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The answer should include the reasoning and trade-offs.&lt;br /&gt;
&lt;br /&gt;
== 16. Design trade-offs ==&lt;br /&gt;
&lt;br /&gt;
Most design decisions have advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
A useful explanation is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
We selected approach A because ...&lt;br /&gt;
The main advantage is ...&lt;br /&gt;
The main disadvantage is ...&lt;br /&gt;
Approach B was considered, but ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Avoid claiming that a design has no disadvantages.&lt;br /&gt;
&lt;br /&gt;
== 17. Explain one technical difficulty ==&lt;br /&gt;
&lt;br /&gt;
Each team should identify one meaningful technical difficulty encountered during the project.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&lt;br /&gt;
* parsing an undocumented format;&lt;br /&gt;
* synchronizing hardware communication;&lt;br /&gt;
* handling malformed packets;&lt;br /&gt;
* managing asynchronous operations;&lt;br /&gt;
* correcting image-processing artifacts;&lt;br /&gt;
* making a component testable;&lt;br /&gt;
* debugging a dependency issue;&lt;br /&gt;
* designing data serialization.&lt;br /&gt;
&lt;br /&gt;
The explanation should focus on the technical reasoning used to solve the problem.&lt;br /&gt;
&lt;br /&gt;
== 18. Explain the debugging process ==&lt;br /&gt;
&lt;br /&gt;
A useful structure is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Observed behavior&lt;br /&gt;
      ↓&lt;br /&gt;
Hypothesis&lt;br /&gt;
      ↓&lt;br /&gt;
Diagnostic method&lt;br /&gt;
      ↓&lt;br /&gt;
Root cause&lt;br /&gt;
      ↓&lt;br /&gt;
Fix&lt;br /&gt;
      ↓&lt;br /&gt;
Regression test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This demonstrates engineering process, not only the final result.&lt;br /&gt;
&lt;br /&gt;
== 19. Testing explanation ==&lt;br /&gt;
&lt;br /&gt;
The team should be able to explain:&lt;br /&gt;
&lt;br /&gt;
* what is tested;&lt;br /&gt;
* why those areas are important;&lt;br /&gt;
* what is not tested;&lt;br /&gt;
* which tests are unit tests;&lt;br /&gt;
* which tests are integration tests;&lt;br /&gt;
* how external dependencies are handled.&lt;br /&gt;
&lt;br /&gt;
== 20. Show one representative test ==&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_average_rejects_empty_list():&lt;br /&gt;
    with pytest.raises(ValueError):&lt;br /&gt;
        average([])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_round_trip(tmp_path):&lt;br /&gt;
    path = tmp_path / &amp;quot;data.json&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    save_data(path, original)&lt;br /&gt;
&lt;br /&gt;
    restored = load_data(path)&lt;br /&gt;
&lt;br /&gt;
    assert restored == original&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The test should demonstrate meaningful project behavior.&lt;br /&gt;
&lt;br /&gt;
== 21. Tests should pass ==&lt;br /&gt;
&lt;br /&gt;
Before the demonstration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pytest&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
should complete successfully.&lt;br /&gt;
&lt;br /&gt;
If some tests are intentionally skipped, the reason should be understood and documented.&lt;br /&gt;
&lt;br /&gt;
== 22. Quality checks ==&lt;br /&gt;
&lt;br /&gt;
Run the project&amp;#039;s normal quality checks.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ruff check .&lt;br /&gt;
pytest&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If static type checking is part of the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mypy src&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If formatting is enforced:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ruff format --check .&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 23. Final clean installation test ==&lt;br /&gt;
&lt;br /&gt;
The project should have been tested in a clean environment before Lab 7.&lt;br /&gt;
&lt;br /&gt;
A typical procedure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m venv clean-env&lt;br /&gt;
source clean-env/bin/activate&lt;br /&gt;
&lt;br /&gt;
python -m pip install -e .&lt;br /&gt;
&lt;br /&gt;
pytest&lt;br /&gt;
python -m project_name&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If these steps fail, the project is not reproducibly packaged.&lt;br /&gt;
&lt;br /&gt;
== 24. Windows clean setup ==&lt;br /&gt;
&lt;br /&gt;
For Windows PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
python -m venv clean-env&lt;br /&gt;
clean-env\Scripts\Activate.ps1&lt;br /&gt;
&lt;br /&gt;
python -m pip install -e .&lt;br /&gt;
&lt;br /&gt;
pytest&lt;br /&gt;
python -m project_name&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Document platform-specific requirements where necessary.&lt;br /&gt;
&lt;br /&gt;
== 25. README final review ==&lt;br /&gt;
&lt;br /&gt;
The README should contain at least:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Project name&lt;br /&gt;
Description&lt;br /&gt;
Main features&lt;br /&gt;
Requirements&lt;br /&gt;
Installation&lt;br /&gt;
Configuration&lt;br /&gt;
Running&lt;br /&gt;
Example usage&lt;br /&gt;
Testing&lt;br /&gt;
Architecture overview&lt;br /&gt;
Known limitations&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another developer should be able to start the project using only this information.&lt;br /&gt;
&lt;br /&gt;
== 26. Repository structure ==&lt;br /&gt;
&lt;br /&gt;
A final project may resemble:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
project/&lt;br /&gt;
├── README.md&lt;br /&gt;
├── pyproject.toml&lt;br /&gt;
├── .gitignore&lt;br /&gt;
├── src/&lt;br /&gt;
│   └── project_name/&lt;br /&gt;
│       ├── __init__.py&lt;br /&gt;
│       ├── main.py&lt;br /&gt;
│       └── ...&lt;br /&gt;
├── tests/&lt;br /&gt;
│   └── ...&lt;br /&gt;
└── examples/&lt;br /&gt;
    └── ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Not every project requires the same modules.&lt;br /&gt;
&lt;br /&gt;
== 27. Repository cleanliness ==&lt;br /&gt;
&lt;br /&gt;
The repository should generally not contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.venv/&lt;br /&gt;
__pycache__/&lt;br /&gt;
*.pyc&lt;br /&gt;
.pytest_cache/&lt;br /&gt;
.mypy_cache/&lt;br /&gt;
.ruff_cache/&lt;br /&gt;
.coverage&lt;br /&gt;
temporary output&lt;br /&gt;
real secrets&lt;br /&gt;
large accidental binaries&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
unless a specific generated artifact is deliberately required.&lt;br /&gt;
&lt;br /&gt;
== 28. Git status ==&lt;br /&gt;
&lt;br /&gt;
Before submission:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git status&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
should not show forgotten modifications or accidentally generated files.&lt;br /&gt;
&lt;br /&gt;
== 29. Git history ==&lt;br /&gt;
&lt;br /&gt;
Inspect:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git log --oneline --graph --decorate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The history should show meaningful development rather than one final upload.&lt;br /&gt;
&lt;br /&gt;
Useful examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Add packet decoder&lt;br /&gt;
Implement device retry handling&lt;br /&gt;
Add regression tests for invalid frames&lt;br /&gt;
Refactor configuration loading&lt;br /&gt;
Document installation procedure&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 30. Final commit ==&lt;br /&gt;
&lt;br /&gt;
A final commit may be something like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Prepare final project release&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
but the repository should already contain meaningful previous commits.&lt;br /&gt;
&lt;br /&gt;
== 31. Tagging a release ==&lt;br /&gt;
&lt;br /&gt;
Optionally, create a final release tag.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git tag v1.0.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If tags are used:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git push --tags&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is optional unless required by the course.&lt;br /&gt;
&lt;br /&gt;
== 32. Version consistency ==&lt;br /&gt;
&lt;br /&gt;
If the project uses a version:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[project]&lt;br /&gt;
version = &amp;quot;1.0.0&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
ensure documentation and release tags do not contradict it.&lt;br /&gt;
&lt;br /&gt;
== 33. Known limitations ==&lt;br /&gt;
&lt;br /&gt;
Known limitations should remain visible in the final documentation.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
## Known limitations&lt;br /&gt;
&lt;br /&gt;
- Only PNG input is supported.&lt;br /&gt;
- Device reconnect requires application restart.&lt;br /&gt;
- The project has only been tested on Linux.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not remove limitations merely to make the project appear more complete.&lt;br /&gt;
&lt;br /&gt;
== 34. Final acceptance test ==&lt;br /&gt;
&lt;br /&gt;
Before presentation, execute one complete acceptance test.&lt;br /&gt;
&lt;br /&gt;
Record:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Input:&lt;br /&gt;
Command:&lt;br /&gt;
Expected output:&lt;br /&gt;
Actual output:&lt;br /&gt;
Result:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main workflow should pass.&lt;br /&gt;
&lt;br /&gt;
== 35. Demonstration backup ==&lt;br /&gt;
&lt;br /&gt;
For projects with fragile external dependencies, prepare a backup.&lt;br /&gt;
&lt;br /&gt;
Possible backups include:&lt;br /&gt;
&lt;br /&gt;
* representative recorded data;&lt;br /&gt;
* stored sample responses;&lt;br /&gt;
* local test server;&lt;br /&gt;
* simulated hardware interface;&lt;br /&gt;
* known-good sample images.&lt;br /&gt;
&lt;br /&gt;
A backup should still demonstrate the project&amp;#039;s own logic.&lt;br /&gt;
&lt;br /&gt;
== 36. Do not fake the primary functionality ==&lt;br /&gt;
&lt;br /&gt;
A backup may replace an unavailable external dependency.&lt;br /&gt;
&lt;br /&gt;
It should not replace the actual project implementation.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
acceptable:&lt;br /&gt;
recorded sensor data → real processing pipeline&lt;br /&gt;
&lt;br /&gt;
not acceptable:&lt;br /&gt;
precomputed final output shown instead of processing&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 37. Technical questions ==&lt;br /&gt;
&lt;br /&gt;
Students should be prepared for questions such as:&lt;br /&gt;
&lt;br /&gt;
* Why is this component a class?&lt;br /&gt;
* Why is this function separate?&lt;br /&gt;
* Why does this module depend on that one?&lt;br /&gt;
* What happens if this file is missing?&lt;br /&gt;
* What happens if this network request times out?&lt;br /&gt;
* How is this behavior tested?&lt;br /&gt;
* What would need to change to support another input format?&lt;br /&gt;
* Which part was most difficult?&lt;br /&gt;
* Where is configuration stored?&lt;br /&gt;
* Why was this library selected?&lt;br /&gt;
* What is one limitation of the current implementation?&lt;br /&gt;
&lt;br /&gt;
== 38. Questions about Python ==&lt;br /&gt;
&lt;br /&gt;
The project discussion may also include concepts from Labs 1–4.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
* difference between &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;is&amp;lt;/code&amp;gt;;&lt;br /&gt;
* list/reference semantics;&lt;br /&gt;
* properties;&lt;br /&gt;
* dataclasses;&lt;br /&gt;
* inheritance versus composition;&lt;br /&gt;
* modules and packages;&lt;br /&gt;
* exceptions;&lt;br /&gt;
* context managers;&lt;br /&gt;
* type hints;&lt;br /&gt;
* pytest fixtures;&lt;br /&gt;
* parametrized tests;&lt;br /&gt;
* dependency injection.&lt;br /&gt;
&lt;br /&gt;
The objective is to connect the project implementation to the Python concepts studied during the course.&lt;br /&gt;
&lt;br /&gt;
== 39. Explain code ownership ==&lt;br /&gt;
&lt;br /&gt;
Every team member should understand the main project architecture.&lt;br /&gt;
&lt;br /&gt;
A student should not answer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;quot;I don&amp;#039;t know; another team member wrote that file.&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
for central project functionality.&lt;br /&gt;
&lt;br /&gt;
Specialization is reasonable, but the team should maintain shared understanding of the main system.&lt;br /&gt;
&lt;br /&gt;
== 40. Team presentation ==&lt;br /&gt;
&lt;br /&gt;
A possible team presentation structure is:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Part&lt;br /&gt;
! Suggested content&lt;br /&gt;
|-&lt;br /&gt;
| 1&lt;br /&gt;
| problem and objective&lt;br /&gt;
|-&lt;br /&gt;
| 2&lt;br /&gt;
| architecture&lt;br /&gt;
|-&lt;br /&gt;
| 3&lt;br /&gt;
| live demonstration&lt;br /&gt;
|-&lt;br /&gt;
| 4&lt;br /&gt;
| important implementation detail&lt;br /&gt;
|-&lt;br /&gt;
| 5&lt;br /&gt;
| tests and reliability&lt;br /&gt;
|-&lt;br /&gt;
| 6&lt;br /&gt;
| limitation and future improvement&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Keep the presentation technical and concise.&lt;br /&gt;
&lt;br /&gt;
== 41. Project objective ==&lt;br /&gt;
&lt;br /&gt;
The first explanation should answer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
What problem does this project solve?&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A good answer should fit in one or two sentences.&lt;br /&gt;
&lt;br /&gt;
Avoid beginning with a long list of technologies.&lt;br /&gt;
&lt;br /&gt;
== 42. Technology is not the objective ==&lt;br /&gt;
&lt;br /&gt;
Poor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
We used Python, NumPy, OpenCV,&lt;br /&gt;
JSON, Git and pytest.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Better:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
The application processes thermal frames&lt;br /&gt;
and detects defective pixels automatically.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Technologies support the project objective; they are not the objective themselves.&lt;br /&gt;
&lt;br /&gt;
== 43. Explain the data flow ==&lt;br /&gt;
&lt;br /&gt;
A useful technical explanation follows the data.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
camera frame&lt;br /&gt;
     ↓&lt;br /&gt;
decoder&lt;br /&gt;
     ↓&lt;br /&gt;
preprocessing&lt;br /&gt;
     ↓&lt;br /&gt;
analysis&lt;br /&gt;
     ↓&lt;br /&gt;
result&lt;br /&gt;
     ↓&lt;br /&gt;
storage/display&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This often makes the architecture easier to understand than describing files in arbitrary order.&lt;br /&gt;
&lt;br /&gt;
== 44. Explain external dependencies ==&lt;br /&gt;
&lt;br /&gt;
Identify external boundaries explicitly.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
filesystem&lt;br /&gt;
network API&lt;br /&gt;
camera&lt;br /&gt;
serial device&lt;br /&gt;
database&lt;br /&gt;
third-party library&lt;br /&gt;
operating system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Explain how failure at these boundaries is handled.&lt;br /&gt;
&lt;br /&gt;
== 45. Explain one invariant ==&lt;br /&gt;
&lt;br /&gt;
An invariant is a condition expected to remain true.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
grade is always between 1 and 10&lt;br /&gt;
packet length matches header length&lt;br /&gt;
image width is always positive&lt;br /&gt;
device ID is unique&lt;br /&gt;
configuration is validated before startup&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Explain where the invariant is enforced.&lt;br /&gt;
&lt;br /&gt;
== 46. Explain one exception path ==&lt;br /&gt;
&lt;br /&gt;
Students should be able to trace one error path.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
missing file&lt;br /&gt;
    ↓&lt;br /&gt;
FileNotFoundError&lt;br /&gt;
    ↓&lt;br /&gt;
application boundary catches error&lt;br /&gt;
    ↓&lt;br /&gt;
user-friendly message&lt;br /&gt;
    ↓&lt;br /&gt;
application exits with failure&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 47. Explain one testability decision ==&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
network client passed as dependency&lt;br /&gt;
file path passed as parameter&lt;br /&gt;
processing separated from input()&lt;br /&gt;
hardware access isolated in device.py&lt;br /&gt;
time source injected into function&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Explain how the decision makes automated testing easier.&lt;br /&gt;
&lt;br /&gt;
== 48. Explain one refactoring ==&lt;br /&gt;
&lt;br /&gt;
The team should identify one area improved after the MVP.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Before:&lt;br /&gt;
main.py contained parsing,&lt;br /&gt;
processing and output.&lt;br /&gt;
&lt;br /&gt;
After:&lt;br /&gt;
parser.py&lt;br /&gt;
processor.py&lt;br /&gt;
storage.py&lt;br /&gt;
main.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Explain why the new structure is better.&lt;br /&gt;
&lt;br /&gt;
== 49. Explain one regression test ==&lt;br /&gt;
&lt;br /&gt;
A useful answer is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
We discovered that empty input caused&lt;br /&gt;
ZeroDivisionError.&lt;br /&gt;
&lt;br /&gt;
We added a test for empty input,&lt;br /&gt;
then changed the function to raise&lt;br /&gt;
a meaningful ValueError.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This demonstrates a complete defect-fixing workflow.&lt;br /&gt;
&lt;br /&gt;
== 50. Evaluation areas ==&lt;br /&gt;
&lt;br /&gt;
A project may be evaluated across several dimensions.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Area&lt;br /&gt;
! What is considered&lt;br /&gt;
|-&lt;br /&gt;
| functionality&lt;br /&gt;
| Does the central application work?&lt;br /&gt;
|-&lt;br /&gt;
| correctness&lt;br /&gt;
| Are results and behaviors correct?&lt;br /&gt;
|-&lt;br /&gt;
| architecture&lt;br /&gt;
| Is the project structured coherently?&lt;br /&gt;
|-&lt;br /&gt;
| Python usage&lt;br /&gt;
| Does the implementation use appropriate Python constructs?&lt;br /&gt;
|-&lt;br /&gt;
| robustness&lt;br /&gt;
| Are expected failures handled?&lt;br /&gt;
|-&lt;br /&gt;
| testing&lt;br /&gt;
| Are important behaviors tested?&lt;br /&gt;
|-&lt;br /&gt;
| code quality&lt;br /&gt;
| Is the code readable and maintainable?&lt;br /&gt;
|-&lt;br /&gt;
| documentation&lt;br /&gt;
| Can the project be installed and used?&lt;br /&gt;
|-&lt;br /&gt;
| Git workflow&lt;br /&gt;
| Does the repository show meaningful development?&lt;br /&gt;
|-&lt;br /&gt;
| technical understanding&lt;br /&gt;
| Can the team explain its implementation and decisions?&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 51. Functionality is necessary but not sufficient ==&lt;br /&gt;
&lt;br /&gt;
A project that produces the correct output but contains:&lt;br /&gt;
&lt;br /&gt;
* one giant file;&lt;br /&gt;
* no tests;&lt;br /&gt;
* hard-coded machine paths;&lt;br /&gt;
* undocumented dependencies;&lt;br /&gt;
* hidden credentials;&lt;br /&gt;
* unhandled errors;&lt;br /&gt;
&lt;br /&gt;
is technically weaker than a project that also demonstrates software-engineering discipline.&lt;br /&gt;
&lt;br /&gt;
== 52. Complexity is not automatically quality ==&lt;br /&gt;
&lt;br /&gt;
Do not add complexity to appear advanced.&lt;br /&gt;
&lt;br /&gt;
A smaller clear implementation is often better than:&lt;br /&gt;
&lt;br /&gt;
* unnecessary inheritance;&lt;br /&gt;
* excessive abstraction layers;&lt;br /&gt;
* many external dependencies;&lt;br /&gt;
* unnecessary asynchronous code;&lt;br /&gt;
* complicated design patterns without a need.&lt;br /&gt;
&lt;br /&gt;
Quality is not measured by the number of classes or libraries.&lt;br /&gt;
&lt;br /&gt;
== 53. Pythonic implementation ==&lt;br /&gt;
&lt;br /&gt;
The final implementation should reflect concepts from the course.&lt;br /&gt;
&lt;br /&gt;
Examples include:&lt;br /&gt;
&lt;br /&gt;
* direct iteration instead of unnecessary index loops;&lt;br /&gt;
* dataclasses for simple data models;&lt;br /&gt;
* context managers for resources;&lt;br /&gt;
* &amp;lt;code&amp;gt;pathlib&amp;lt;/code&amp;gt; for paths;&lt;br /&gt;
* exceptions for meaningful failure conditions;&lt;br /&gt;
* modules with clear responsibilities;&lt;br /&gt;
* type hints where they improve interfaces;&lt;br /&gt;
* pytest for automated tests.&lt;br /&gt;
&lt;br /&gt;
== 54. C++ translation should no longer dominate ==&lt;br /&gt;
&lt;br /&gt;
By the final project, Python code should not look like a mechanical translation of C++.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Less idiomatic&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | More idiomatic&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
    print(values[i])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for value in values:&lt;br /&gt;
    print(value)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The same principle applies at architecture level.&lt;br /&gt;
&lt;br /&gt;
== 55. Final code review checklist ==&lt;br /&gt;
&lt;br /&gt;
Before submission, review:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Area&lt;br /&gt;
! Check&lt;br /&gt;
|-&lt;br /&gt;
| naming&lt;br /&gt;
| names communicate intent&lt;br /&gt;
|-&lt;br /&gt;
| functions&lt;br /&gt;
| no obvious excessively large functions remain&lt;br /&gt;
|-&lt;br /&gt;
| classes&lt;br /&gt;
| responsibilities are coherent&lt;br /&gt;
|-&lt;br /&gt;
| modules&lt;br /&gt;
| responsibilities are clear&lt;br /&gt;
|-&lt;br /&gt;
| globals&lt;br /&gt;
| unnecessary mutable globals removed&lt;br /&gt;
|-&lt;br /&gt;
| errors&lt;br /&gt;
| expected failures handled&lt;br /&gt;
|-&lt;br /&gt;
| logging&lt;br /&gt;
| temporary debug output removed&lt;br /&gt;
|-&lt;br /&gt;
| paths&lt;br /&gt;
| machine-specific paths removed&lt;br /&gt;
|-&lt;br /&gt;
| secrets&lt;br /&gt;
| none committed&lt;br /&gt;
|-&lt;br /&gt;
| tests&lt;br /&gt;
| suite passes&lt;br /&gt;
|-&lt;br /&gt;
| documentation&lt;br /&gt;
| current and reproducible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 56. Final testing checklist ==&lt;br /&gt;
&lt;br /&gt;
Run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pytest&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, where configured:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ruff check .&lt;br /&gt;
ruff format --check .&lt;br /&gt;
mypy src&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally run the main application manually.&lt;br /&gt;
&lt;br /&gt;
== 57. Final demonstration checklist ==&lt;br /&gt;
&lt;br /&gt;
Before presenting:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[ ] demo input available&lt;br /&gt;
[ ] application starts&lt;br /&gt;
[ ] dependencies installed&lt;br /&gt;
[ ] required hardware available&lt;br /&gt;
[ ] credentials/configuration available&lt;br /&gt;
[ ] output location writable&lt;br /&gt;
[ ] main workflow tested&lt;br /&gt;
[ ] failure example prepared&lt;br /&gt;
[ ] backup data available if needed&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 58. Final documentation checklist ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[ ] README complete&lt;br /&gt;
[ ] installation tested&lt;br /&gt;
[ ] run command documented&lt;br /&gt;
[ ] configuration documented&lt;br /&gt;
[ ] test command documented&lt;br /&gt;
[ ] architecture explained&lt;br /&gt;
[ ] known limitations listed&lt;br /&gt;
[ ] sample input included where useful&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 59. Final repository checklist ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[ ] git status clean&lt;br /&gt;
[ ] no .venv&lt;br /&gt;
[ ] no __pycache__&lt;br /&gt;
[ ] no secrets&lt;br /&gt;
[ ] no accidental large files&lt;br /&gt;
[ ] pyproject.toml correct&lt;br /&gt;
[ ] .gitignore correct&lt;br /&gt;
[ ] tests committed&lt;br /&gt;
[ ] documentation committed&lt;br /&gt;
[ ] final version committed&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 60. Final submission ==&lt;br /&gt;
&lt;br /&gt;
The final submission should identify the repository and the final revision clearly.&lt;br /&gt;
&lt;br /&gt;
A submission should not depend on an uncommitted local working tree.&lt;br /&gt;
&lt;br /&gt;
The evaluated state should correspond to a specific commit.&lt;br /&gt;
&lt;br /&gt;
Optional:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git rev-parse HEAD&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
can be used to record the exact commit identifier.&lt;br /&gt;
&lt;br /&gt;
== 61. Submission commit ==&lt;br /&gt;
&lt;br /&gt;
A possible final workflow is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git status&lt;br /&gt;
&lt;br /&gt;
pytest&lt;br /&gt;
ruff check .&lt;br /&gt;
&lt;br /&gt;
git add .&lt;br /&gt;
git commit -m &amp;quot;Prepare final release&amp;quot;&lt;br /&gt;
&lt;br /&gt;
git push&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Only commit if there are actual final changes.&lt;br /&gt;
&lt;br /&gt;
== 62. Avoid last-minute uncontrolled changes ==&lt;br /&gt;
&lt;br /&gt;
Immediately before the final demonstration, avoid:&lt;br /&gt;
&lt;br /&gt;
* changing architecture;&lt;br /&gt;
* upgrading dependencies without need;&lt;br /&gt;
* replacing working libraries;&lt;br /&gt;
* rewriting central functionality;&lt;br /&gt;
* merging experimental branches.&lt;br /&gt;
&lt;br /&gt;
Last-minute changes introduce unnecessary risk.&lt;br /&gt;
&lt;br /&gt;
== 63. If a defect appears during the final session ==&lt;br /&gt;
&lt;br /&gt;
If a non-critical problem appears:&lt;br /&gt;
&lt;br /&gt;
# identify the scope;&lt;br /&gt;
# avoid destabilizing unrelated code;&lt;br /&gt;
# apply a minimal fix if safe;&lt;br /&gt;
# rerun relevant tests;&lt;br /&gt;
# document the issue if it cannot safely be fixed.&lt;br /&gt;
&lt;br /&gt;
Do not perform a large redesign under presentation pressure.&lt;br /&gt;
&lt;br /&gt;
== 64. If the demo fails ==&lt;br /&gt;
&lt;br /&gt;
A failed demo should be approached technically.&lt;br /&gt;
&lt;br /&gt;
Check:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
environment&lt;br /&gt;
configuration&lt;br /&gt;
dependencies&lt;br /&gt;
input&lt;br /&gt;
external service&lt;br /&gt;
hardware&lt;br /&gt;
recent code changes&lt;br /&gt;
logs&lt;br /&gt;
traceback&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If a prepared representative backup exists, it may be used to continue the technical discussion.&lt;br /&gt;
&lt;br /&gt;
== 65. Future work ==&lt;br /&gt;
&lt;br /&gt;
The final presentation may include a short future-work section.&lt;br /&gt;
&lt;br /&gt;
Good examples:&lt;br /&gt;
&lt;br /&gt;
* additional input formats;&lt;br /&gt;
* improved performance;&lt;br /&gt;
* GUI;&lt;br /&gt;
* hardware support;&lt;br /&gt;
* better deployment;&lt;br /&gt;
* more complete test coverage;&lt;br /&gt;
* additional algorithms.&lt;br /&gt;
&lt;br /&gt;
Future work should not be used to disguise missing required functionality.&lt;br /&gt;
&lt;br /&gt;
== 66. Lessons learned ==&lt;br /&gt;
&lt;br /&gt;
Each team should be able to identify at least one useful technical lesson.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Separating I/O from processing made testing easier.&lt;br /&gt;
&lt;br /&gt;
Hard-coded paths created portability problems.&lt;br /&gt;
&lt;br /&gt;
Regression tests prevented a previously fixed bug from returning.&lt;br /&gt;
&lt;br /&gt;
A smaller dependency set simplified installation.&lt;br /&gt;
&lt;br /&gt;
Composition made the hardware interface easier to replace in tests.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 67. Individual understanding ==&lt;br /&gt;
&lt;br /&gt;
Each student should be able to explain:&lt;br /&gt;
&lt;br /&gt;
* the project objective;&lt;br /&gt;
* the architecture;&lt;br /&gt;
* the main workflow;&lt;br /&gt;
* at least one module in detail;&lt;br /&gt;
* at least one test;&lt;br /&gt;
* at least one error path.&lt;br /&gt;
&lt;br /&gt;
The project should represent team work rather than isolated contributions that nobody else understands.&lt;br /&gt;
&lt;br /&gt;
== 68. Suggested technical discussion ==&lt;br /&gt;
&lt;br /&gt;
A final discussion may cover:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Why this architecture?&lt;br /&gt;
&lt;br /&gt;
What alternative was considered?&lt;br /&gt;
&lt;br /&gt;
What was the hardest technical issue?&lt;br /&gt;
&lt;br /&gt;
What would fail first at larger scale?&lt;br /&gt;
&lt;br /&gt;
Which dependency is most critical?&lt;br /&gt;
&lt;br /&gt;
Which component is easiest to replace?&lt;br /&gt;
&lt;br /&gt;
Which part has the strongest tests?&lt;br /&gt;
&lt;br /&gt;
Which part still has the largest limitation?&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 69. Example final explanation ==&lt;br /&gt;
&lt;br /&gt;
A concise technical explanation could be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
The application reads thermal frames through&lt;br /&gt;
the acquisition module.&lt;br /&gt;
&lt;br /&gt;
Frames are passed to the processing pipeline,&lt;br /&gt;
which performs correction and analysis.&lt;br /&gt;
&lt;br /&gt;
The pipeline is independent from the camera&lt;br /&gt;
interface, so tests can use stored frames.&lt;br /&gt;
&lt;br /&gt;
Results are returned to the CLI and can also&lt;br /&gt;
be saved through the storage module.&lt;br /&gt;
&lt;br /&gt;
Hardware failures are converted into&lt;br /&gt;
application-specific exceptions at the&lt;br /&gt;
device boundary.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is more useful than describing source files in arbitrary order.&lt;br /&gt;
&lt;br /&gt;
== 70. Final project maturity ==&lt;br /&gt;
&lt;br /&gt;
A mature final project should demonstrate:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
correctness&lt;br /&gt;
    +&lt;br /&gt;
structure&lt;br /&gt;
    +&lt;br /&gt;
testing&lt;br /&gt;
    +&lt;br /&gt;
robustness&lt;br /&gt;
    +&lt;br /&gt;
documentation&lt;br /&gt;
    +&lt;br /&gt;
technical understanding&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No single category replaces the others.&lt;br /&gt;
&lt;br /&gt;
== 71. Exercise 1 — Final architecture explanation ==&lt;br /&gt;
&lt;br /&gt;
Prepare a diagram containing no more than approximately 5–8 major components.&lt;br /&gt;
&lt;br /&gt;
For every component, write one responsibility.&lt;br /&gt;
&lt;br /&gt;
Practice explaining the architecture in less than two minutes.&lt;br /&gt;
&lt;br /&gt;
== 72. Exercise 2 — Main workflow walkthrough ==&lt;br /&gt;
&lt;br /&gt;
Trace one real input through the project.&lt;br /&gt;
&lt;br /&gt;
Document:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Input:&lt;br /&gt;
Entry point:&lt;br /&gt;
Modules involved:&lt;br /&gt;
Important transformation:&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use this path during the code walkthrough.&lt;br /&gt;
&lt;br /&gt;
== 73. Exercise 3 — Design decision ==&lt;br /&gt;
&lt;br /&gt;
Choose one design decision and document:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Decision:&lt;br /&gt;
Alternative:&lt;br /&gt;
Reason:&lt;br /&gt;
Advantage:&lt;br /&gt;
Disadvantage:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Be prepared to explain it during evaluation.&lt;br /&gt;
&lt;br /&gt;
== 74. Exercise 4 — Technical difficulty ==&lt;br /&gt;
&lt;br /&gt;
Document:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Problem:&lt;br /&gt;
Observed behavior:&lt;br /&gt;
Root cause:&lt;br /&gt;
Diagnostic method:&lt;br /&gt;
Solution:&lt;br /&gt;
Regression prevention:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use a real difficulty from the project.&lt;br /&gt;
&lt;br /&gt;
== 75. Exercise 5 — Test walkthrough ==&lt;br /&gt;
&lt;br /&gt;
Choose one meaningful test.&lt;br /&gt;
&lt;br /&gt;
Explain:&lt;br /&gt;
&lt;br /&gt;
* what behavior it verifies;&lt;br /&gt;
* why the behavior matters;&lt;br /&gt;
* whether it is a unit or integration test;&lt;br /&gt;
* what would happen if the implementation were broken.&lt;br /&gt;
&lt;br /&gt;
== 76. Exercise 6 — Error-path walkthrough ==&lt;br /&gt;
&lt;br /&gt;
Choose one predictable failure.&lt;br /&gt;
&lt;br /&gt;
Trace:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
failure&lt;br /&gt;
  ↓&lt;br /&gt;
detection&lt;br /&gt;
  ↓&lt;br /&gt;
exception/error&lt;br /&gt;
  ↓&lt;br /&gt;
handling layer&lt;br /&gt;
  ↓&lt;br /&gt;
user/log output&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 77. Exercise 7 — Clean installation ==&lt;br /&gt;
&lt;br /&gt;
Using a fresh environment:&lt;br /&gt;
&lt;br /&gt;
# install the project;&lt;br /&gt;
# run the tests;&lt;br /&gt;
# run the main workflow;&lt;br /&gt;
# compare the process with the README.&lt;br /&gt;
&lt;br /&gt;
Correct any discrepancy.&lt;br /&gt;
&lt;br /&gt;
== 78. Exercise 8 — Final repository audit ==&lt;br /&gt;
&lt;br /&gt;
Run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git status&lt;br /&gt;
git log --oneline --graph --decorate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Confirm:&lt;br /&gt;
&lt;br /&gt;
* final code is committed;&lt;br /&gt;
* tests are committed;&lt;br /&gt;
* documentation is committed;&lt;br /&gt;
* no sensitive data exists;&lt;br /&gt;
* no generated files are accidentally tracked.&lt;br /&gt;
&lt;br /&gt;
== 79. Exercise 9 — Demonstration rehearsal ==&lt;br /&gt;
&lt;br /&gt;
Perform the demonstration exactly as it will be shown.&lt;br /&gt;
&lt;br /&gt;
Measure whether it is concise enough.&lt;br /&gt;
&lt;br /&gt;
Record any manual step that could fail.&lt;br /&gt;
&lt;br /&gt;
Simplify the demonstration where possible.&lt;br /&gt;
&lt;br /&gt;
== 80. Exercise 10 — Final self-review ==&lt;br /&gt;
&lt;br /&gt;
Answer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
What is the strongest part of the project?&lt;br /&gt;
&lt;br /&gt;
What is the weakest part?&lt;br /&gt;
&lt;br /&gt;
What is the most important limitation?&lt;br /&gt;
&lt;br /&gt;
What would you improve with one more week?&lt;br /&gt;
&lt;br /&gt;
Which technical decision are you most prepared to defend?&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The answers should be based on the actual implementation.&lt;br /&gt;
&lt;br /&gt;
== 81. Final evaluation checklist ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Area&lt;br /&gt;
! Final question&lt;br /&gt;
|-&lt;br /&gt;
| objective&lt;br /&gt;
| Is the project goal clear?&lt;br /&gt;
|-&lt;br /&gt;
| functionality&lt;br /&gt;
| Does the primary workflow work?&lt;br /&gt;
|-&lt;br /&gt;
| architecture&lt;br /&gt;
| Can the structure be explained clearly?&lt;br /&gt;
|-&lt;br /&gt;
| Python&lt;br /&gt;
| Is the implementation appropriate for Python?&lt;br /&gt;
|-&lt;br /&gt;
| robustness&lt;br /&gt;
| Are expected failures handled?&lt;br /&gt;
|-&lt;br /&gt;
| testing&lt;br /&gt;
| Are important behaviors tested?&lt;br /&gt;
|-&lt;br /&gt;
| reproducibility&lt;br /&gt;
| Can the project be installed from documentation?&lt;br /&gt;
|-&lt;br /&gt;
| repository&lt;br /&gt;
| Is the submitted state clean and complete?&lt;br /&gt;
|-&lt;br /&gt;
| documentation&lt;br /&gt;
| Is usage sufficiently explained?&lt;br /&gt;
|-&lt;br /&gt;
| understanding&lt;br /&gt;
| Can the team explain its own code and decisions?&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 82. Course summary ==&lt;br /&gt;
&lt;br /&gt;
Across the seven laboratories, the progression has been:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Lab 1&lt;br /&gt;
Python syntax and idioms&lt;br /&gt;
        ↓&lt;br /&gt;
Lab 2&lt;br /&gt;
Object-oriented Python&lt;br /&gt;
        ↓&lt;br /&gt;
Lab 3&lt;br /&gt;
Modules, files, exceptions and typing&lt;br /&gt;
        ↓&lt;br /&gt;
Lab 4&lt;br /&gt;
Testing and software quality&lt;br /&gt;
        ↓&lt;br /&gt;
Lab 5&lt;br /&gt;
Project architecture and MVP&lt;br /&gt;
        ↓&lt;br /&gt;
Lab 6&lt;br /&gt;
Stabilization and release candidate&lt;br /&gt;
        ↓&lt;br /&gt;
Lab 7&lt;br /&gt;
Final integration and demonstration&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The objective of the course is therefore not only to learn Python syntax.&lt;br /&gt;
&lt;br /&gt;
It is to move from:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
C/C++ programmer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
toward:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Python software developer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
who can write code that is:&lt;br /&gt;
&lt;br /&gt;
* readable;&lt;br /&gt;
* modular;&lt;br /&gt;
* testable;&lt;br /&gt;
* robust;&lt;br /&gt;
* reproducible;&lt;br /&gt;
* maintainable.&lt;br /&gt;
&lt;br /&gt;
== 83. Final principle ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
The final project should not only work. Another developer should be able to understand what it does, install it, run it, test it and modify it without depending on undocumented knowledge from the original authors.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 84. Final submission checklist ==&lt;br /&gt;
&lt;br /&gt;
Before leaving the final laboratory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[ ] final repository pushed&lt;br /&gt;
[ ] final commit identified&lt;br /&gt;
[ ] tests pass&lt;br /&gt;
[ ] main workflow demonstrated&lt;br /&gt;
[ ] README complete&lt;br /&gt;
[ ] known limitations documented&lt;br /&gt;
[ ] repository clean&lt;br /&gt;
[ ] no secrets committed&lt;br /&gt;
[ ] project can be installed reproducibly&lt;br /&gt;
[ ] all team members understand the main architecture&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project is then ready for final submission.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_6&amp;diff=8375</id>
		<title>Lab 6</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_6&amp;diff=8375"/>
		<updated>2026-09-26T10:12:53Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: Pagină nouă: = Lab 6 — Project Development II =  == Objectives ==  This laboratory continues the supervised development of the semester project.  The objective is to move from a working minim...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 6 — Project Development II =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
This laboratory continues the supervised development of the semester project.&lt;br /&gt;
&lt;br /&gt;
The objective is to move from a working minimum viable product to a stable &amp;#039;&amp;#039;&amp;#039;release candidate&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
At this stage, the central functionality should already exist. The focus is therefore on completing missing features, improving reliability, removing technical debt, expanding tests, documenting the project and ensuring that another person can install and run it reproducibly.&lt;br /&gt;
&lt;br /&gt;
After completing this laboratory, you should be able to:&lt;br /&gt;
&lt;br /&gt;
* identify and prioritize remaining work;&lt;br /&gt;
* distinguish critical defects from optional improvements;&lt;br /&gt;
* complete the central functionality of the project;&lt;br /&gt;
* add regression tests for discovered bugs;&lt;br /&gt;
* test important edge cases;&lt;br /&gt;
* refactor code safely using tests;&lt;br /&gt;
* improve logging and diagnostics;&lt;br /&gt;
* improve project documentation;&lt;br /&gt;
* validate installation from a clean environment;&lt;br /&gt;
* verify dependency declarations;&lt;br /&gt;
* document known limitations;&lt;br /&gt;
* prepare a stable release candidate.&lt;br /&gt;
&lt;br /&gt;
Most of the laboratory should be spent working directly on the project.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== 1. Laboratory structure ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Time&lt;br /&gt;
! Activity&lt;br /&gt;
|-&lt;br /&gt;
| 0–15 min&lt;br /&gt;
| Project status and remaining-work review&lt;br /&gt;
|-&lt;br /&gt;
| 15–30 min&lt;br /&gt;
| Short discussion: release quality and regression testing&lt;br /&gt;
|-&lt;br /&gt;
| 30–90 min&lt;br /&gt;
| Supervised implementation and refactoring&lt;br /&gt;
|-&lt;br /&gt;
| 90–105 min&lt;br /&gt;
| Test and documentation review&lt;br /&gt;
|-&lt;br /&gt;
| 105–120 min&lt;br /&gt;
| Release-candidate verification&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 2. From MVP to release candidate ==&lt;br /&gt;
&lt;br /&gt;
The MVP demonstrated that the main technical idea works.&lt;br /&gt;
&lt;br /&gt;
A release candidate should demonstrate that the project is close to final delivery.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
prototype&lt;br /&gt;
    ↓&lt;br /&gt;
MVP&lt;br /&gt;
    ↓&lt;br /&gt;
feature complete&lt;br /&gt;
    ↓&lt;br /&gt;
tested&lt;br /&gt;
    ↓&lt;br /&gt;
documented&lt;br /&gt;
    ↓&lt;br /&gt;
release candidate&lt;br /&gt;
    ↓&lt;br /&gt;
final release&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A release candidate does not need to be perfect, but no major known defect should prevent the primary workflow from operating correctly.&lt;br /&gt;
&lt;br /&gt;
== 3. Feature completeness ==&lt;br /&gt;
&lt;br /&gt;
Before adding new features, classify remaining work.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Category&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| required&lt;br /&gt;
| necessary for the project to satisfy its main objective&lt;br /&gt;
|-&lt;br /&gt;
| important&lt;br /&gt;
| significantly improves usability or robustness&lt;br /&gt;
|-&lt;br /&gt;
| optional&lt;br /&gt;
| useful enhancement but not required&lt;br /&gt;
|-&lt;br /&gt;
| experimental&lt;br /&gt;
| interesting idea that may not be stable enough for final integration&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Required functionality should be completed before optional extensions.&lt;br /&gt;
&lt;br /&gt;
== 4. Priority levels ==&lt;br /&gt;
&lt;br /&gt;
A simple priority system is sufficient.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Priority&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| P0&lt;br /&gt;
| application cannot perform its primary function&lt;br /&gt;
|-&lt;br /&gt;
| P1&lt;br /&gt;
| major defect affecting normal use&lt;br /&gt;
|-&lt;br /&gt;
| P2&lt;br /&gt;
| important improvement or non-critical defect&lt;br /&gt;
|-&lt;br /&gt;
| P3&lt;br /&gt;
| optional improvement&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
P0 and P1 issues should be resolved before adding new optional features.&lt;br /&gt;
&lt;br /&gt;
== 5. Remaining-work list ==&lt;br /&gt;
&lt;br /&gt;
Maintain a short actionable list.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[ ] Handle malformed input&lt;br /&gt;
[ ] Add timeout handling&lt;br /&gt;
[ ] Add pipeline tests&lt;br /&gt;
[ ] Document configuration&lt;br /&gt;
[ ] Remove debug output&lt;br /&gt;
[ ] Verify clean installation&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Avoid vague items such as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[ ] Improve code&lt;br /&gt;
[ ] Fix things&lt;br /&gt;
[ ] Finish project&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 6. Stable software before more features ==&lt;br /&gt;
&lt;br /&gt;
At this stage, a smaller stable project is generally preferable to a larger unstable one.&lt;br /&gt;
&lt;br /&gt;
Prefer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
working feature A&lt;br /&gt;
working feature B&lt;br /&gt;
working feature C&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
over:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
feature A mostly works&lt;br /&gt;
feature B sometimes works&lt;br /&gt;
feature C works&lt;br /&gt;
feature D unfinished&lt;br /&gt;
feature E experimental&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 7. Regression testing ==&lt;br /&gt;
&lt;br /&gt;
When a defect is discovered:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
discover bug&lt;br /&gt;
    ↓&lt;br /&gt;
reproduce bug&lt;br /&gt;
    ↓&lt;br /&gt;
write failing test&lt;br /&gt;
    ↓&lt;br /&gt;
fix implementation&lt;br /&gt;
    ↓&lt;br /&gt;
test passes&lt;br /&gt;
    ↓&lt;br /&gt;
keep test permanently&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The resulting test is a &amp;#039;&amp;#039;&amp;#039;regression test&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== 8. Regression test example ==&lt;br /&gt;
&lt;br /&gt;
Suppose:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(values):&lt;br /&gt;
    return sum(values) / len(values)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function fails incorrectly for an empty list.&lt;br /&gt;
&lt;br /&gt;
Add a test:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import pytest&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_average_rejects_empty_list():&lt;br /&gt;
    with pytest.raises(ValueError):&lt;br /&gt;
        average([])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then update the function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(values):&lt;br /&gt;
    if not values:&lt;br /&gt;
        raise ValueError(&lt;br /&gt;
            &amp;quot;values cannot be empty&amp;quot;&lt;br /&gt;
        )&lt;br /&gt;
&lt;br /&gt;
    return sum(values) / len(values)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The test should remain in the project after the defect is fixed.&lt;br /&gt;
&lt;br /&gt;
== 9. Edge-case review ==&lt;br /&gt;
&lt;br /&gt;
Normal input is not sufficient.&lt;br /&gt;
&lt;br /&gt;
For numerical data, consider:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
minimum valid value&lt;br /&gt;
maximum valid value&lt;br /&gt;
zero&lt;br /&gt;
negative values&lt;br /&gt;
very large values&lt;br /&gt;
empty input&lt;br /&gt;
one-element input&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
empty string&lt;br /&gt;
whitespace-only string&lt;br /&gt;
Unicode characters&lt;br /&gt;
very long strings&lt;br /&gt;
unexpected encoding&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For files:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
missing file&lt;br /&gt;
empty file&lt;br /&gt;
malformed file&lt;br /&gt;
permission error&lt;br /&gt;
missing output directory&lt;br /&gt;
unexpected format&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 10. Network projects ==&lt;br /&gt;
&lt;br /&gt;
Network projects should consider:&lt;br /&gt;
&lt;br /&gt;
* connection refused;&lt;br /&gt;
* timeout;&lt;br /&gt;
* server unavailable;&lt;br /&gt;
* malformed response;&lt;br /&gt;
* unexpected status code;&lt;br /&gt;
* lost connection;&lt;br /&gt;
* incomplete message.&lt;br /&gt;
&lt;br /&gt;
Do not assume every network operation succeeds.&lt;br /&gt;
&lt;br /&gt;
== 11. Hardware projects ==&lt;br /&gt;
&lt;br /&gt;
Hardware projects should consider:&lt;br /&gt;
&lt;br /&gt;
* device unavailable;&lt;br /&gt;
* device disconnected;&lt;br /&gt;
* incomplete data;&lt;br /&gt;
* invalid data;&lt;br /&gt;
* timeout;&lt;br /&gt;
* permission error;&lt;br /&gt;
* unsupported device or firmware version.&lt;br /&gt;
&lt;br /&gt;
Expected hardware failures should produce understandable application behavior.&lt;br /&gt;
&lt;br /&gt;
== 12. Image-processing projects ==&lt;br /&gt;
&lt;br /&gt;
Image-processing projects should consider:&lt;br /&gt;
&lt;br /&gt;
* unsupported format;&lt;br /&gt;
* invalid dimensions;&lt;br /&gt;
* empty image;&lt;br /&gt;
* grayscale versus color input;&lt;br /&gt;
* unexpected channel count;&lt;br /&gt;
* corrupted input;&lt;br /&gt;
* very large images.&lt;br /&gt;
&lt;br /&gt;
Validate assumptions close to the input boundary.&lt;br /&gt;
&lt;br /&gt;
== 13. Public behavior versus implementation detail ==&lt;br /&gt;
&lt;br /&gt;
Tests should focus on public behavior.&lt;br /&gt;
&lt;br /&gt;
Prefer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
result = process(image)&lt;br /&gt;
&lt;br /&gt;
assert result.shape == expected_shape&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
over:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
assert processor._temporary_buffer == ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Private implementation details should remain free to change during refactoring.&lt;br /&gt;
&lt;br /&gt;
== 14. Do not delete inconvenient tests ==&lt;br /&gt;
&lt;br /&gt;
If a valid test fails after a code change, investigate the reason.&lt;br /&gt;
&lt;br /&gt;
A test should normally be removed only when:&lt;br /&gt;
&lt;br /&gt;
* the requirement changed;&lt;br /&gt;
* the tested behavior no longer exists;&lt;br /&gt;
* the test itself is incorrect.&lt;br /&gt;
&lt;br /&gt;
== 15. Test organization ==&lt;br /&gt;
&lt;br /&gt;
A larger project can organize tests by component:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
tests/&lt;br /&gt;
├── test_models.py&lt;br /&gt;
├── test_services.py&lt;br /&gt;
├── test_storage.py&lt;br /&gt;
├── test_protocol.py&lt;br /&gt;
└── test_pipeline.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For larger projects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
tests/&lt;br /&gt;
├── unit/&lt;br /&gt;
│   ├── test_filters.py&lt;br /&gt;
│   └── test_protocol.py&lt;br /&gt;
└── integration/&lt;br /&gt;
    ├── test_storage.py&lt;br /&gt;
    └── test_pipeline.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use the simplest structure that remains understandable.&lt;br /&gt;
&lt;br /&gt;
== 16. Unit versus integration tests ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Example&lt;br /&gt;
! Type&lt;br /&gt;
|-&lt;br /&gt;
| validate one grade value&lt;br /&gt;
| unit&lt;br /&gt;
|-&lt;br /&gt;
| decode one packet&lt;br /&gt;
| unit&lt;br /&gt;
|-&lt;br /&gt;
| encode and then decode a packet&lt;br /&gt;
| integration&lt;br /&gt;
|-&lt;br /&gt;
| save and reload data from a temporary file&lt;br /&gt;
| integration&lt;br /&gt;
|-&lt;br /&gt;
| run a complete processing pipeline&lt;br /&gt;
| integration&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Both kinds of tests are useful.&lt;br /&gt;
&lt;br /&gt;
== 17. End-to-end testing ==&lt;br /&gt;
&lt;br /&gt;
An end-to-end test exercises a complete workflow.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
input&lt;br /&gt;
  ↓&lt;br /&gt;
application&lt;br /&gt;
  ↓&lt;br /&gt;
processing&lt;br /&gt;
  ↓&lt;br /&gt;
output&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
End-to-end tests are valuable but should not replace focused unit tests.&lt;br /&gt;
&lt;br /&gt;
== 18. Testing CLI applications ==&lt;br /&gt;
&lt;br /&gt;
Separate argument parsing from application logic.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Tightly coupled&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Easier to test&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
    args = parse_args()&lt;br /&gt;
&lt;br /&gt;
    # All processing here.&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def process_file(&lt;br /&gt;
    input_path,&lt;br /&gt;
    output_path,&lt;br /&gt;
):&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    args = parse_args()&lt;br /&gt;
&lt;br /&gt;
    process_file(&lt;br /&gt;
        args.input,&lt;br /&gt;
        args.output,&lt;br /&gt;
    )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The processing function can now be tested without command-line parsing.&lt;br /&gt;
&lt;br /&gt;
== 19. Deterministic tests ==&lt;br /&gt;
&lt;br /&gt;
Good tests produce the same result every time under the same conditions.&lt;br /&gt;
&lt;br /&gt;
Avoid uncontrolled dependencies on:&lt;br /&gt;
&lt;br /&gt;
* current time;&lt;br /&gt;
* random values;&lt;br /&gt;
* network availability;&lt;br /&gt;
* arbitrary filesystem state;&lt;br /&gt;
* execution order.&lt;br /&gt;
&lt;br /&gt;
For controlled randomness:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import random&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
rng = random.Random(1234)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 20. Refactoring with tests ==&lt;br /&gt;
&lt;br /&gt;
Use short cycles:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
run tests&lt;br /&gt;
   ↓&lt;br /&gt;
all pass&lt;br /&gt;
   ↓&lt;br /&gt;
small refactor&lt;br /&gt;
   ↓&lt;br /&gt;
run tests&lt;br /&gt;
   ↓&lt;br /&gt;
all pass&lt;br /&gt;
   ↓&lt;br /&gt;
commit&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not perform a very large refactoring before checking whether behavior is still correct.&lt;br /&gt;
&lt;br /&gt;
== 21. Refactoring nested control flow ==&lt;br /&gt;
&lt;br /&gt;
Before:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def process(record):&lt;br /&gt;
    if record is not None:&lt;br /&gt;
        if record.valid:&lt;br /&gt;
            if record.enabled:&lt;br /&gt;
                return transform(record)&lt;br /&gt;
&lt;br /&gt;
    return None&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using guard clauses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def process(record):&lt;br /&gt;
    if record is None:&lt;br /&gt;
        return None&lt;br /&gt;
&lt;br /&gt;
    if not record.valid:&lt;br /&gt;
        return None&lt;br /&gt;
&lt;br /&gt;
    if not record.enabled:&lt;br /&gt;
        return None&lt;br /&gt;
&lt;br /&gt;
    return transform(record)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second version reduces nesting.&lt;br /&gt;
&lt;br /&gt;
== 22. Refactoring long conditions ==&lt;br /&gt;
&lt;br /&gt;
Before:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if (&lt;br /&gt;
    device.connected&lt;br /&gt;
    and device.enabled&lt;br /&gt;
    and not device.error&lt;br /&gt;
    and device.temperature &amp;lt; 80&lt;br /&gt;
    and device.mode == &amp;quot;ready&amp;quot;&lt;br /&gt;
):&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def is_ready(device):&lt;br /&gt;
    return (&lt;br /&gt;
        device.connected&lt;br /&gt;
        and device.enabled&lt;br /&gt;
        and not device.error&lt;br /&gt;
        and device.temperature &amp;lt; 80&lt;br /&gt;
        and device.mode == &amp;quot;ready&amp;quot;&lt;br /&gt;
    )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if is_ready(device):&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function name expresses the meaning of the condition.&lt;br /&gt;
&lt;br /&gt;
== 23. Structured data instead of loose dictionaries ==&lt;br /&gt;
&lt;br /&gt;
For stable domain data, a dataclass may be clearer than an unstructured dictionary.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Dictionary&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Dataclass&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
student = {&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;br /&gt;
    &amp;quot;grade&amp;quot;: 9.5,&lt;br /&gt;
    &amp;quot;year&amp;quot;: 2,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from dataclasses import dataclass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@dataclass&lt;br /&gt;
class Student:&lt;br /&gt;
    name: str&lt;br /&gt;
    grade: float&lt;br /&gt;
    year: int&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Do not convert every dictionary into a class. Use the structure that best represents the data.&lt;br /&gt;
&lt;br /&gt;
== 24. Avoid over-refactoring ==&lt;br /&gt;
&lt;br /&gt;
Refactor when it improves:&lt;br /&gt;
&lt;br /&gt;
* clarity;&lt;br /&gt;
* correctness;&lt;br /&gt;
* maintainability;&lt;br /&gt;
* testability;&lt;br /&gt;
* reuse.&lt;br /&gt;
&lt;br /&gt;
Do not introduce abstractions simply to make the architecture appear more complicated.&lt;br /&gt;
&lt;br /&gt;
== 25. Logging review ==&lt;br /&gt;
&lt;br /&gt;
Search for temporary debug output such as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
print(&amp;quot;debug&amp;quot;)&lt;br /&gt;
print(variable)&lt;br /&gt;
print(&amp;quot;here&amp;quot;)&lt;br /&gt;
print(&amp;quot;test&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For each occurrence, decide whether it should be:&lt;br /&gt;
&lt;br /&gt;
* removed;&lt;br /&gt;
* replaced with logging;&lt;br /&gt;
* retained as intentional user output.&lt;br /&gt;
&lt;br /&gt;
== 26. Logging levels ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Level&lt;br /&gt;
! Typical use&lt;br /&gt;
|-&lt;br /&gt;
| DEBUG&lt;br /&gt;
| detailed internal processing information&lt;br /&gt;
|-&lt;br /&gt;
| INFO&lt;br /&gt;
| normal application events&lt;br /&gt;
|-&lt;br /&gt;
| WARNING&lt;br /&gt;
| unusual condition from which execution can continue&lt;br /&gt;
|-&lt;br /&gt;
| ERROR&lt;br /&gt;
| requested operation failed&lt;br /&gt;
|-&lt;br /&gt;
| CRITICAL&lt;br /&gt;
| application cannot continue&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 27. Useful logging context ==&lt;br /&gt;
&lt;br /&gt;
Poor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
logger.error(&amp;quot;Failed&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Better:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
logger.error(&lt;br /&gt;
    &amp;quot;Failed to load configuration from %s&amp;quot;,&lt;br /&gt;
    path,&lt;br /&gt;
)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Diagnostic messages should contain enough context to be useful.&lt;br /&gt;
&lt;br /&gt;
== 28. Exception logging ==&lt;br /&gt;
&lt;br /&gt;
Inside an exception handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    load_device()&lt;br /&gt;
except DeviceError:&lt;br /&gt;
    logger.exception(&lt;br /&gt;
        &amp;quot;Device initialization failed&amp;quot;&lt;br /&gt;
    )&lt;br /&gt;
    raise&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;logger.exception()&amp;lt;/code&amp;gt; includes traceback information.&lt;br /&gt;
&lt;br /&gt;
== 29. User-facing errors ==&lt;br /&gt;
&lt;br /&gt;
Expected errors should usually produce concise user-facing messages.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Error: input file &amp;#039;data.json&amp;#039; does not exist.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A predictable user error does not normally require displaying an internal traceback.&lt;br /&gt;
&lt;br /&gt;
== 30. Documentation layers ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Documentation&lt;br /&gt;
! Audience&lt;br /&gt;
|-&lt;br /&gt;
| README&lt;br /&gt;
| user or developer starting the project&lt;br /&gt;
|-&lt;br /&gt;
| docstrings&lt;br /&gt;
| developer using functions and classes&lt;br /&gt;
|-&lt;br /&gt;
| comments&lt;br /&gt;
| developer understanding non-obvious implementation decisions&lt;br /&gt;
|-&lt;br /&gt;
| release notes&lt;br /&gt;
| user/evaluator understanding changes and limitations&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 31. README checklist ==&lt;br /&gt;
&lt;br /&gt;
The README should include:&lt;br /&gt;
&lt;br /&gt;
* project name;&lt;br /&gt;
* short description;&lt;br /&gt;
* main features;&lt;br /&gt;
* requirements;&lt;br /&gt;
* installation;&lt;br /&gt;
* configuration;&lt;br /&gt;
* how to run;&lt;br /&gt;
* example usage;&lt;br /&gt;
* how to run tests;&lt;br /&gt;
* high-level architecture;&lt;br /&gt;
* known limitations.&lt;br /&gt;
&lt;br /&gt;
== 32. Reproducible installation ==&lt;br /&gt;
&lt;br /&gt;
Installation instructions should be explicit.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git clone ...&lt;br /&gt;
cd project&lt;br /&gt;
&lt;br /&gt;
python -m venv .venv&lt;br /&gt;
source .venv/bin/activate&lt;br /&gt;
&lt;br /&gt;
python -m pip install -e .&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Windows PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
.venv\Scripts\Activate.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 33. Running instructions ==&lt;br /&gt;
&lt;br /&gt;
Document the actual command.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m project_name&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m project_name \&lt;br /&gt;
    --input data/input.json \&lt;br /&gt;
    --output results/output.json&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 34. Document configuration ==&lt;br /&gt;
&lt;br /&gt;
If a project uses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;json&amp;quot;&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;server&amp;quot;: &amp;quot;localhost&amp;quot;,&lt;br /&gt;
    &amp;quot;port&amp;quot;: 8000,&lt;br /&gt;
    &amp;quot;timeout&amp;quot;: 5.0&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
document every field.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Field&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;server&amp;lt;/code&amp;gt;&lt;br /&gt;
| remote server hostname&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;port&amp;lt;/code&amp;gt;&lt;br /&gt;
| remote service port&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;timeout&amp;lt;/code&amp;gt;&lt;br /&gt;
| request timeout in seconds&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 35. Environment variables ==&lt;br /&gt;
&lt;br /&gt;
Do not document or commit real secrets.&lt;br /&gt;
&lt;br /&gt;
Document variable names:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
API_KEY&lt;br /&gt;
SERVER_URL&lt;br /&gt;
LOG_LEVEL&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export API_KEY=&amp;quot;...&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 36. Docstrings ==&lt;br /&gt;
&lt;br /&gt;
Use docstrings when behavior is not obvious.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def normalize(&lt;br /&gt;
    values: list[float],&lt;br /&gt;
) -&amp;gt; list[float]:&lt;br /&gt;
    &amp;#039;&amp;#039;&amp;#039;Scale values to the interval [0, 1].&lt;br /&gt;
&lt;br /&gt;
    Raises:&lt;br /&gt;
        ValueError: If all values are equal.&lt;br /&gt;
    &amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not duplicate obvious type information already expressed by type hints.&lt;br /&gt;
&lt;br /&gt;
== 37. Public versus internal API ==&lt;br /&gt;
&lt;br /&gt;
Internal names can begin with an underscore.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def _parse_header(data):&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def load_frame(path):&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This indicates that &amp;lt;code&amp;gt;_parse_header()&amp;lt;/code&amp;gt; is an implementation detail.&lt;br /&gt;
&lt;br /&gt;
== 38. Keep the public API small ==&lt;br /&gt;
&lt;br /&gt;
Prefer a small meaningful interface:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
load_image()&lt;br /&gt;
process_image()&lt;br /&gt;
save_image()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
rather than exposing every internal intermediate operation.&lt;br /&gt;
&lt;br /&gt;
== 39. Clean-environment verification ==&lt;br /&gt;
&lt;br /&gt;
Do not rely on globally installed packages.&lt;br /&gt;
&lt;br /&gt;
A useful verification is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m venv test-env&lt;br /&gt;
source test-env/bin/activate&lt;br /&gt;
&lt;br /&gt;
python -m pip install -e .&lt;br /&gt;
pytest&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If this fails, installation instructions or dependency declarations are incomplete.&lt;br /&gt;
&lt;br /&gt;
== 40. Verify declared dependencies ==&lt;br /&gt;
&lt;br /&gt;
If code contains:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import requests&lt;br /&gt;
import numpy&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the required packages must be declared in project metadata.&lt;br /&gt;
&lt;br /&gt;
Another machine should not depend on packages that happened to be installed on the developer&amp;#039;s system.&lt;br /&gt;
&lt;br /&gt;
== 41. Remove unused dependencies ==&lt;br /&gt;
&lt;br /&gt;
If the project declares:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
dependencies = [&lt;br /&gt;
    &amp;quot;requests&amp;quot;,&lt;br /&gt;
    &amp;quot;numpy&amp;quot;,&lt;br /&gt;
    &amp;quot;pandas&amp;quot;,&lt;br /&gt;
    &amp;quot;matplotlib&amp;quot;,&lt;br /&gt;
    &amp;quot;opencv-python&amp;quot;,&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
but only uses &amp;lt;code&amp;gt;requests&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;numpy&amp;lt;/code&amp;gt;, remove unnecessary packages.&lt;br /&gt;
&lt;br /&gt;
== 42. Version constraints ==&lt;br /&gt;
&lt;br /&gt;
Avoid unnecessarily strict pinning unless required.&lt;br /&gt;
&lt;br /&gt;
Possibly too strict:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
dependencies = [&lt;br /&gt;
    &amp;quot;requests==2.32.4&amp;quot;,&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potentially more flexible:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
dependencies = [&lt;br /&gt;
    &amp;quot;requests&amp;gt;=2.32,&amp;lt;3&amp;quot;,&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The exact constraint depends on project compatibility requirements.&lt;br /&gt;
&lt;br /&gt;
== 43. Supported Python version ==&lt;br /&gt;
&lt;br /&gt;
Declare the expected Python version:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[project]&lt;br /&gt;
requires-python = &amp;quot;&amp;gt;=3.11&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not claim compatibility with an older version while using unsupported newer syntax.&lt;br /&gt;
&lt;br /&gt;
== 44. Clean repository ==&lt;br /&gt;
&lt;br /&gt;
The repository should generally exclude generated content such as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.venv/&lt;br /&gt;
__pycache__/&lt;br /&gt;
*.pyc&lt;br /&gt;
.coverage&lt;br /&gt;
.pytest_cache/&lt;br /&gt;
.mypy_cache/&lt;br /&gt;
.ruff_cache/&lt;br /&gt;
build/&lt;br /&gt;
dist/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 45. Example .gitignore ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.venv/&lt;br /&gt;
__pycache__/&lt;br /&gt;
*.pyc&lt;br /&gt;
&lt;br /&gt;
.pytest_cache/&lt;br /&gt;
.mypy_cache/&lt;br /&gt;
.ruff_cache/&lt;br /&gt;
&lt;br /&gt;
.coverage&lt;br /&gt;
htmlcov/&lt;br /&gt;
&lt;br /&gt;
build/&lt;br /&gt;
dist/&lt;br /&gt;
*.egg-info/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add project-specific generated files where appropriate.&lt;br /&gt;
&lt;br /&gt;
== 46. Sample data ==&lt;br /&gt;
&lt;br /&gt;
If the project requires sample input for demonstration, include a small suitable example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
examples/&lt;br /&gt;
├── sample_input.json&lt;br /&gt;
└── expected_output.json&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not rely on private local data that cannot be reproduced.&lt;br /&gt;
&lt;br /&gt;
== 47. Sample configuration ==&lt;br /&gt;
&lt;br /&gt;
For machine-specific configuration, provide:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
config.example.json&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
rather than committing real credentials in:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
config.json&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 48. Security review ==&lt;br /&gt;
&lt;br /&gt;
Check for accidental secrets:&lt;br /&gt;
&lt;br /&gt;
* passwords;&lt;br /&gt;
* API keys;&lt;br /&gt;
* private keys;&lt;br /&gt;
* access tokens;&lt;br /&gt;
* database credentials;&lt;br /&gt;
* credential-bearing URLs.&lt;br /&gt;
&lt;br /&gt;
If a credential was committed, removing it from the latest version may not be sufficient. The credential should be rotated.&lt;br /&gt;
&lt;br /&gt;
== 49. Input validation review ==&lt;br /&gt;
&lt;br /&gt;
Review every external input boundary:&lt;br /&gt;
&lt;br /&gt;
* command-line arguments;&lt;br /&gt;
* configuration;&lt;br /&gt;
* files;&lt;br /&gt;
* network messages;&lt;br /&gt;
* user input;&lt;br /&gt;
* hardware data;&lt;br /&gt;
* API responses.&lt;br /&gt;
&lt;br /&gt;
Ask:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
What assumptions does this code make?&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Validate important assumptions before processing the data.&lt;br /&gt;
&lt;br /&gt;
== 50. Serialization round trip ==&lt;br /&gt;
&lt;br /&gt;
If data is stored and later loaded, test a round trip.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
object&lt;br /&gt;
  ↓&lt;br /&gt;
serialize&lt;br /&gt;
  ↓&lt;br /&gt;
file&lt;br /&gt;
  ↓&lt;br /&gt;
deserialize&lt;br /&gt;
  ↓&lt;br /&gt;
object&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_round_trip(tmp_path):&lt;br /&gt;
    path = tmp_path / &amp;quot;data.json&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    original = [&lt;br /&gt;
        Student(&amp;quot;Alice&amp;quot;, 9.5),&lt;br /&gt;
        Student(&amp;quot;Bob&amp;quot;, 8.0),&lt;br /&gt;
    ]&lt;br /&gt;
&lt;br /&gt;
    save_students(path, original)&lt;br /&gt;
&lt;br /&gt;
    restored = load_students(path)&lt;br /&gt;
&lt;br /&gt;
    assert restored == original&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 51. Performance sanity check ==&lt;br /&gt;
&lt;br /&gt;
Full optimization is not required, but the application should be tested on representative input.&lt;br /&gt;
&lt;br /&gt;
If the intended workload is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
100000 records&lt;br /&gt;
100 images&lt;br /&gt;
continuous packets&lt;br /&gt;
large matrices&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
do not test only with one tiny input and assume performance is acceptable.&lt;br /&gt;
&lt;br /&gt;
== 52. Resource review ==&lt;br /&gt;
&lt;br /&gt;
Check for:&lt;br /&gt;
&lt;br /&gt;
* files left open;&lt;br /&gt;
* sockets not closed;&lt;br /&gt;
* unnecessary large copies;&lt;br /&gt;
* unbounded queues/lists;&lt;br /&gt;
* repeated loading of large data;&lt;br /&gt;
* infinite retry loops;&lt;br /&gt;
* threads or processes not terminated.&lt;br /&gt;
&lt;br /&gt;
Use context managers where appropriate.&lt;br /&gt;
&lt;br /&gt;
== 53. Graceful shutdown ==&lt;br /&gt;
&lt;br /&gt;
Applications managing external resources should stop predictably.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    application.run()&lt;br /&gt;
finally:&lt;br /&gt;
    application.close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When appropriate, make resources context managers.&lt;br /&gt;
&lt;br /&gt;
== 54. Keyboard interrupt ==&lt;br /&gt;
&lt;br /&gt;
A CLI application may handle &amp;lt;code&amp;gt;Ctrl+C&amp;lt;/code&amp;gt; cleanly:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
    try:&lt;br /&gt;
        run()&lt;br /&gt;
    except KeyboardInterrupt:&lt;br /&gt;
        print(&amp;quot;\nInterrupted.&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Only add this behavior when it improves the application.&lt;br /&gt;
&lt;br /&gt;
== 55. Release version ==&lt;br /&gt;
&lt;br /&gt;
The project should have a version identifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[project]&lt;br /&gt;
version = &amp;quot;0.9.0&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A release candidate can be described as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
0.9.0-rc1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 56. Release notes ==&lt;br /&gt;
&lt;br /&gt;
A short release note can contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Version&lt;br /&gt;
Main features&lt;br /&gt;
Important fixes&lt;br /&gt;
Known limitations&lt;br /&gt;
Installation notes&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It does not need to be long.&lt;br /&gt;
&lt;br /&gt;
== 57. Known limitations ==&lt;br /&gt;
&lt;br /&gt;
Document real limitations explicitly.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Only PNG input is supported.&lt;br /&gt;
IPv6 is not supported.&lt;br /&gt;
The program has only been tested on Linux.&lt;br /&gt;
Maximum tested image size is 4096×4096.&lt;br /&gt;
Device reconnect requires restart.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 58. Known issue versus critical defect ==&lt;br /&gt;
&lt;br /&gt;
A known issue may remain if:&lt;br /&gt;
&lt;br /&gt;
* it does not prevent the primary workflow;&lt;br /&gt;
* its scope is clear;&lt;br /&gt;
* fixing it immediately would create disproportionate risk.&lt;br /&gt;
&lt;br /&gt;
A critical defect should not simply be renamed a limitation.&lt;br /&gt;
&lt;br /&gt;
== 59. Manual acceptance test ==&lt;br /&gt;
&lt;br /&gt;
In addition to automated tests, perform a manual end-to-end test.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[ ] clean startup&lt;br /&gt;
[ ] representative input accepted&lt;br /&gt;
[ ] main feature works&lt;br /&gt;
[ ] output correct&lt;br /&gt;
[ ] expected error handled&lt;br /&gt;
[ ] application exits cleanly&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 60. Clean-machine test ==&lt;br /&gt;
&lt;br /&gt;
Ask:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Could another student clone this repository&lt;br /&gt;
and run the project using only the README?&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test it:&lt;br /&gt;
&lt;br /&gt;
# create a clean environment;&lt;br /&gt;
# install dependencies;&lt;br /&gt;
# run tests;&lt;br /&gt;
# start the application;&lt;br /&gt;
# follow only documented steps.&lt;br /&gt;
&lt;br /&gt;
== 61. Project review checklist ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Area&lt;br /&gt;
! Question&lt;br /&gt;
|-&lt;br /&gt;
| features&lt;br /&gt;
| Are all required features implemented?&lt;br /&gt;
|-&lt;br /&gt;
| defects&lt;br /&gt;
| Are any P0 or P1 defects open?&lt;br /&gt;
|-&lt;br /&gt;
| tests&lt;br /&gt;
| Are central behavior and major failures tested?&lt;br /&gt;
|-&lt;br /&gt;
| architecture&lt;br /&gt;
| Is remaining technical debt acceptable?&lt;br /&gt;
|-&lt;br /&gt;
| logging&lt;br /&gt;
| Are diagnostics useful and appropriately leveled?&lt;br /&gt;
|-&lt;br /&gt;
| errors&lt;br /&gt;
| Are expected failures handled?&lt;br /&gt;
|-&lt;br /&gt;
| documentation&lt;br /&gt;
| Can another user install and run the project?&lt;br /&gt;
|-&lt;br /&gt;
| dependencies&lt;br /&gt;
| Are all required packages declared?&lt;br /&gt;
|-&lt;br /&gt;
| repository&lt;br /&gt;
| Is generated and sensitive content excluded?&lt;br /&gt;
|-&lt;br /&gt;
| release&lt;br /&gt;
| Can the application be demonstrated reliably?&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 62. Exercise 1 — Remaining-work triage ==&lt;br /&gt;
&lt;br /&gt;
List all remaining tasks.&lt;br /&gt;
&lt;br /&gt;
Assign:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
P0&lt;br /&gt;
P1&lt;br /&gt;
P2&lt;br /&gt;
P3&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Resolve P0 and P1 issues before optional work.&lt;br /&gt;
&lt;br /&gt;
== 63. Exercise 2 — Regression test ==&lt;br /&gt;
&lt;br /&gt;
Choose one defect discovered since Lab 5.&lt;br /&gt;
&lt;br /&gt;
Then:&lt;br /&gt;
&lt;br /&gt;
# reproduce the defect;&lt;br /&gt;
# add a failing automated test;&lt;br /&gt;
# fix the implementation;&lt;br /&gt;
# confirm the new test passes;&lt;br /&gt;
# run the complete test suite.&lt;br /&gt;
&lt;br /&gt;
== 64. Exercise 3 — Edge-case audit ==&lt;br /&gt;
&lt;br /&gt;
Choose the most important input to the project.&lt;br /&gt;
&lt;br /&gt;
Document at least five edge cases.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Input&lt;br /&gt;
! Expected behavior&lt;br /&gt;
|-&lt;br /&gt;
| empty input&lt;br /&gt;
| clear validation error or defined empty result&lt;br /&gt;
|-&lt;br /&gt;
| valid minimal input&lt;br /&gt;
| processed successfully&lt;br /&gt;
|-&lt;br /&gt;
| malformed input&lt;br /&gt;
| parsing error reported&lt;br /&gt;
|-&lt;br /&gt;
| missing input&lt;br /&gt;
| missing-resource error&lt;br /&gt;
|-&lt;br /&gt;
| large representative input&lt;br /&gt;
| completes within reasonable resources&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Add missing tests where practical.&lt;br /&gt;
&lt;br /&gt;
== 65. Exercise 4 — Test-suite cleanup ==&lt;br /&gt;
&lt;br /&gt;
Review the test suite for:&lt;br /&gt;
&lt;br /&gt;
* duplicated setup;&lt;br /&gt;
* unclear names;&lt;br /&gt;
* order dependencies;&lt;br /&gt;
* permanent local test files;&lt;br /&gt;
* unnecessary real network access;&lt;br /&gt;
* missing assertions;&lt;br /&gt;
* disabled tests.&lt;br /&gt;
&lt;br /&gt;
Refactor where appropriate.&lt;br /&gt;
&lt;br /&gt;
== 66. Exercise 5 — Refactoring checkpoint ==&lt;br /&gt;
&lt;br /&gt;
Choose one area with technical debt:&lt;br /&gt;
&lt;br /&gt;
* long function;&lt;br /&gt;
* large class;&lt;br /&gt;
* duplicated logic;&lt;br /&gt;
* deep nesting;&lt;br /&gt;
* hard-coded configuration;&lt;br /&gt;
* global state.&lt;br /&gt;
&lt;br /&gt;
Use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
tests pass&lt;br /&gt;
   ↓&lt;br /&gt;
small refactor&lt;br /&gt;
   ↓&lt;br /&gt;
tests pass&lt;br /&gt;
   ↓&lt;br /&gt;
commit&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 67. Exercise 6 — Logging audit ==&lt;br /&gt;
&lt;br /&gt;
Search for:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
print(&lt;br /&gt;
logger.debug(&lt;br /&gt;
logger.info(&lt;br /&gt;
logger.warning(&lt;br /&gt;
logger.error(&lt;br /&gt;
logger.exception(&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Check that:&lt;br /&gt;
&lt;br /&gt;
* user output intentionally uses &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt;;&lt;br /&gt;
* diagnostics use logging;&lt;br /&gt;
* levels are appropriate;&lt;br /&gt;
* errors include context;&lt;br /&gt;
* secrets are never logged.&lt;br /&gt;
&lt;br /&gt;
== 68. Exercise 7 — README installation test ==&lt;br /&gt;
&lt;br /&gt;
Follow the README in a clean environment.&lt;br /&gt;
&lt;br /&gt;
Record every missing step.&lt;br /&gt;
&lt;br /&gt;
Update the README until no undocumented knowledge is required to start the project.&lt;br /&gt;
&lt;br /&gt;
== 69. Exercise 8 — Dependency verification ==&lt;br /&gt;
&lt;br /&gt;
Create a fresh virtual environment.&lt;br /&gt;
&lt;br /&gt;
Install the project using its documented procedure.&lt;br /&gt;
&lt;br /&gt;
Then run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pytest&lt;br /&gt;
ruff check .&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If used by the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mypy src&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fix dependency or configuration problems.&lt;br /&gt;
&lt;br /&gt;
== 70. Exercise 9 — Repository audit ==&lt;br /&gt;
&lt;br /&gt;
Run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git status&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inspect the repository for:&lt;br /&gt;
&lt;br /&gt;
* virtual environments;&lt;br /&gt;
* caches;&lt;br /&gt;
* generated output;&lt;br /&gt;
* secrets;&lt;br /&gt;
* large temporary files;&lt;br /&gt;
* local configuration;&lt;br /&gt;
* editor-specific files.&lt;br /&gt;
&lt;br /&gt;
Update &amp;lt;code&amp;gt;.gitignore&amp;lt;/code&amp;gt; if required.&lt;br /&gt;
&lt;br /&gt;
== 71. Exercise 10 — Known limitations ==&lt;br /&gt;
&lt;br /&gt;
Add:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
## Known limitations&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to the README.&lt;br /&gt;
&lt;br /&gt;
List only real limitations of the current implementation.&lt;br /&gt;
&lt;br /&gt;
== 72. Exercise 11 — Release notes ==&lt;br /&gt;
&lt;br /&gt;
Write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Release candidate: 0.9&lt;br /&gt;
&lt;br /&gt;
Implemented:&lt;br /&gt;
- ...&lt;br /&gt;
&lt;br /&gt;
Fixed:&lt;br /&gt;
- ...&lt;br /&gt;
&lt;br /&gt;
Known limitations:&lt;br /&gt;
- ...&lt;br /&gt;
&lt;br /&gt;
Run:&lt;br /&gt;
- ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 73. Exercise 12 — Acceptance test ==&lt;br /&gt;
&lt;br /&gt;
Execute the complete main workflow.&lt;br /&gt;
&lt;br /&gt;
Record:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Input:&lt;br /&gt;
Command:&lt;br /&gt;
Expected output:&lt;br /&gt;
Actual output:&lt;br /&gt;
Result: PASS / FAIL&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A failure in the primary workflow should be treated as a high-priority issue.&lt;br /&gt;
&lt;br /&gt;
== 74. Release-candidate requirements ==&lt;br /&gt;
&lt;br /&gt;
By the end of Lab 6:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Requirement&lt;br /&gt;
! Expected state&lt;br /&gt;
|-&lt;br /&gt;
| core functionality&lt;br /&gt;
| complete&lt;br /&gt;
|-&lt;br /&gt;
| P0 defects&lt;br /&gt;
| none known&lt;br /&gt;
|-&lt;br /&gt;
| P1 defects&lt;br /&gt;
| ideally none&lt;br /&gt;
|-&lt;br /&gt;
| tests&lt;br /&gt;
| central functionality and major failures covered&lt;br /&gt;
|-&lt;br /&gt;
| regression tests&lt;br /&gt;
| added for important discovered defects&lt;br /&gt;
|-&lt;br /&gt;
| documentation&lt;br /&gt;
| installation and usage complete&lt;br /&gt;
|-&lt;br /&gt;
| dependencies&lt;br /&gt;
| reproducible in a clean environment&lt;br /&gt;
|-&lt;br /&gt;
| logging&lt;br /&gt;
| temporary debug output removed&lt;br /&gt;
|-&lt;br /&gt;
| repository&lt;br /&gt;
| clean and free of secrets&lt;br /&gt;
|-&lt;br /&gt;
| limitations&lt;br /&gt;
| explicitly documented&lt;br /&gt;
|-&lt;br /&gt;
| demonstration&lt;br /&gt;
| repeatable end-to-end&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 75. Instructor review ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Area&lt;br /&gt;
! Review question&lt;br /&gt;
|-&lt;br /&gt;
| stability&lt;br /&gt;
| Does the primary workflow work consistently?&lt;br /&gt;
|-&lt;br /&gt;
| completeness&lt;br /&gt;
| Are required features implemented?&lt;br /&gt;
|-&lt;br /&gt;
| testing&lt;br /&gt;
| Are important edge cases tested?&lt;br /&gt;
|-&lt;br /&gt;
| regression&lt;br /&gt;
| Were discovered defects converted into tests?&lt;br /&gt;
|-&lt;br /&gt;
| design&lt;br /&gt;
| Is the architecture still understandable?&lt;br /&gt;
|-&lt;br /&gt;
| documentation&lt;br /&gt;
| Can the project be installed without assistance?&lt;br /&gt;
|-&lt;br /&gt;
| robustness&lt;br /&gt;
| Are predictable failures handled?&lt;br /&gt;
|-&lt;br /&gt;
| release readiness&lt;br /&gt;
| Can the team demonstrate the project reliably?&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 76. C/C++ habits to reconsider ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ habit&lt;br /&gt;
! Typical Python project approach&lt;br /&gt;
|-&lt;br /&gt;
| rely mainly on compile success&lt;br /&gt;
| verify tests, linting and runtime behavior&lt;br /&gt;
|-&lt;br /&gt;
| test manually only at the end&lt;br /&gt;
| maintain regression tests continuously&lt;br /&gt;
|-&lt;br /&gt;
| treat documentation as optional&lt;br /&gt;
| document installation, configuration and execution&lt;br /&gt;
|-&lt;br /&gt;
| assume the developer machine represents deployment&lt;br /&gt;
| verify in a clean virtual environment&lt;br /&gt;
|-&lt;br /&gt;
| leave debug output in code&lt;br /&gt;
| use structured logging&lt;br /&gt;
|-&lt;br /&gt;
| keep redesigning until submission&lt;br /&gt;
| stabilize architecture before final delivery&lt;br /&gt;
|-&lt;br /&gt;
| add features until the last moment&lt;br /&gt;
| freeze core features and focus on reliability&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 77. Feature freeze ==&lt;br /&gt;
&lt;br /&gt;
After Lab 6, the project should enter a practical &amp;#039;&amp;#039;&amp;#039;feature freeze&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
This means:&lt;br /&gt;
&lt;br /&gt;
* no major architecture changes;&lt;br /&gt;
* no unnecessary new dependencies;&lt;br /&gt;
* no large experimental features;&lt;br /&gt;
* focus on defects;&lt;br /&gt;
* focus on documentation;&lt;br /&gt;
* focus on final demonstration reliability.&lt;br /&gt;
&lt;br /&gt;
Small low-risk improvements may still be made.&lt;br /&gt;
&lt;br /&gt;
== 78. Final preparation order ==&lt;br /&gt;
&lt;br /&gt;
A useful order is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
critical defects&lt;br /&gt;
     ↓&lt;br /&gt;
required features&lt;br /&gt;
     ↓&lt;br /&gt;
tests&lt;br /&gt;
     ↓&lt;br /&gt;
documentation&lt;br /&gt;
     ↓&lt;br /&gt;
clean installation&lt;br /&gt;
     ↓&lt;br /&gt;
demo preparation&lt;br /&gt;
     ↓&lt;br /&gt;
optional polish&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not prioritize presentation polish while critical project defects remain.&lt;br /&gt;
&lt;br /&gt;
== 79. Summary ==&lt;br /&gt;
&lt;br /&gt;
Lab 6 is primarily a stabilization laboratory.&lt;br /&gt;
&lt;br /&gt;
The project should move from:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;quot;It basically works.&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;quot;It can be installed,&lt;br /&gt;
tested,&lt;br /&gt;
run,&lt;br /&gt;
demonstrated,&lt;br /&gt;
and understood&lt;br /&gt;
by another person.&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The central idea is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
A release candidate is not simply code with all planned features. It is software whose behavior, dependencies, limitations and execution procedure are sufficiently controlled to be demonstrated and evaluated reliably.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 80. Preparation for Lab 7 ==&lt;br /&gt;
&lt;br /&gt;
Before the final laboratory:&lt;br /&gt;
&lt;br /&gt;
# freeze the main feature set;&lt;br /&gt;
# fix remaining critical defects;&lt;br /&gt;
# run the complete automated test suite;&lt;br /&gt;
# perform a clean-environment installation;&lt;br /&gt;
# verify the README;&lt;br /&gt;
# document known limitations;&lt;br /&gt;
# prepare a stable demonstration dataset or scenario;&lt;br /&gt;
# ensure the demonstration does not depend on unavailable external resources;&lt;br /&gt;
# prepare a concise architecture explanation;&lt;br /&gt;
# prepare to explain one important design decision;&lt;br /&gt;
# prepare to explain one significant technical difficulty.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Lab 7&amp;#039;&amp;#039;&amp;#039;, each team will perform the final demonstration, code walkthrough and technical discussion.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_5&amp;diff=8374</id>
		<title>Lab 5</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_5&amp;diff=8374"/>
		<updated>2026-09-14T06:22:15Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: Pagină nouă: = Lab 5 — Project Development I =  == Objectives ==  This laboratory marks the transition from language-oriented laboratories to supervised project development.  The main objecti...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 5 — Project Development I =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
This laboratory marks the transition from language-oriented laboratories to supervised project development.&lt;br /&gt;
&lt;br /&gt;
The main objective is to transform the semester project from an initial prototype into a structured, maintainable application with a clear architecture and a working minimum viable product (MVP).&lt;br /&gt;
&lt;br /&gt;
Unlike the previous laboratories, most of the time in this session should be dedicated to project implementation, code review and technical discussion.&lt;br /&gt;
&lt;br /&gt;
After completing this laboratory, you should be able to:&lt;br /&gt;
&lt;br /&gt;
* describe the architecture of your project;&lt;br /&gt;
* separate responsibilities between modules;&lt;br /&gt;
* identify unnecessary coupling between components;&lt;br /&gt;
* define clear interfaces between parts of the application;&lt;br /&gt;
* distinguish between domain logic, input/output and infrastructure code;&lt;br /&gt;
* use composition and dependency injection where appropriate;&lt;br /&gt;
* manage project dependencies;&lt;br /&gt;
* maintain a clean Git history;&lt;br /&gt;
* use branches and commits effectively;&lt;br /&gt;
* handle configuration separately from application logic;&lt;br /&gt;
* identify and remove global mutable state;&lt;br /&gt;
* refactor large functions and classes;&lt;br /&gt;
* improve testability;&lt;br /&gt;
* document the main project structure;&lt;br /&gt;
* demonstrate a working MVP.&lt;br /&gt;
&lt;br /&gt;
The focus of this laboratory is not to introduce many new Python language features. Instead, the concepts from Labs 1–4 are applied to the semester project.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== 1. Laboratory structure ==&lt;br /&gt;
&lt;br /&gt;
A recommended structure for the two-hour laboratory is:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Time&lt;br /&gt;
! Activity&lt;br /&gt;
|-&lt;br /&gt;
| 0–15 min&lt;br /&gt;
| Project status and architecture review&lt;br /&gt;
|-&lt;br /&gt;
| 15–35 min&lt;br /&gt;
| Short discussion: architecture, responsibilities and interfaces&lt;br /&gt;
|-&lt;br /&gt;
| 35–95 min&lt;br /&gt;
| Supervised project implementation&lt;br /&gt;
|-&lt;br /&gt;
| 95–110 min&lt;br /&gt;
| Code review and technical feedback&lt;br /&gt;
|-&lt;br /&gt;
| 110–120 min&lt;br /&gt;
| MVP demonstration and next milestone&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The exact timing may vary depending on the number and size of project teams.&lt;br /&gt;
&lt;br /&gt;
== 2. The goal of the MVP ==&lt;br /&gt;
&lt;br /&gt;
By the end of this laboratory, every project should have a working &amp;#039;&amp;#039;&amp;#039;minimum viable product&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
An MVP is not a complete final application. It should demonstrate that the central technical idea works.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Project type&lt;br /&gt;
! Possible MVP&lt;br /&gt;
|-&lt;br /&gt;
| image processing&lt;br /&gt;
| load an image, apply one meaningful processing operation and save/display the result&lt;br /&gt;
|-&lt;br /&gt;
| network application&lt;br /&gt;
| establish communication and exchange one meaningful message&lt;br /&gt;
|-&lt;br /&gt;
| data analysis&lt;br /&gt;
| load real data and produce one correct analysis result&lt;br /&gt;
|-&lt;br /&gt;
| hardware interface&lt;br /&gt;
| communicate with hardware and perform one useful operation&lt;br /&gt;
|-&lt;br /&gt;
| REST application&lt;br /&gt;
| expose one functional endpoint and process a real request&lt;br /&gt;
|-&lt;br /&gt;
| monitoring application&lt;br /&gt;
| collect one real metric and present or store it correctly&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The MVP should prove the technical feasibility of the project.&lt;br /&gt;
&lt;br /&gt;
== 3. Prototype versus project ==&lt;br /&gt;
&lt;br /&gt;
A prototype is often written quickly to prove that something is possible.&lt;br /&gt;
&lt;br /&gt;
A project must also be maintainable.&lt;br /&gt;
&lt;br /&gt;
A prototype may look like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
main.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
containing:&lt;br /&gt;
&lt;br /&gt;
* configuration;&lt;br /&gt;
* file handling;&lt;br /&gt;
* calculations;&lt;br /&gt;
* network communication;&lt;br /&gt;
* user interaction;&lt;br /&gt;
* error handling.&lt;br /&gt;
&lt;br /&gt;
This may be acceptable for the first experiment, but it is not a suitable final structure.&lt;br /&gt;
&lt;br /&gt;
== 4. Separating responsibilities ==&lt;br /&gt;
&lt;br /&gt;
A more maintainable structure separates different responsibilities.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
project/&lt;br /&gt;
├── src/&lt;br /&gt;
│   └── project_name/&lt;br /&gt;
│       ├── __init__.py&lt;br /&gt;
│       ├── models.py&lt;br /&gt;
│       ├── services.py&lt;br /&gt;
│       ├── storage.py&lt;br /&gt;
│       ├── config.py&lt;br /&gt;
│       └── main.py&lt;br /&gt;
└── tests/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A possible responsibility split is:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Module&lt;br /&gt;
! Responsibility&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;models.py&amp;lt;/code&amp;gt;&lt;br /&gt;
| application data structures&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;services.py&amp;lt;/code&amp;gt;&lt;br /&gt;
| application/domain logic&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;storage.py&amp;lt;/code&amp;gt;&lt;br /&gt;
| persistence and file/database access&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;config.py&amp;lt;/code&amp;gt;&lt;br /&gt;
| configuration loading and validation&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;main.py&amp;lt;/code&amp;gt;&lt;br /&gt;
| application startup and orchestration&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The exact names depend on the project.&lt;br /&gt;
&lt;br /&gt;
== 5. Single responsibility ==&lt;br /&gt;
&lt;br /&gt;
A module, class or function should ideally have one clear reason to change.&lt;br /&gt;
&lt;br /&gt;
Consider:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def process_students():&lt;br /&gt;
    # Read JSON file.&lt;br /&gt;
    # Parse students.&lt;br /&gt;
    # Validate grades.&lt;br /&gt;
    # Calculate statistics.&lt;br /&gt;
    # Print results.&lt;br /&gt;
    # Write CSV file.&lt;br /&gt;
    # Send HTTP request.&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function contains several unrelated responsibilities.&lt;br /&gt;
&lt;br /&gt;
A better design might separate:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
load_students()&lt;br /&gt;
validate_students()&lt;br /&gt;
calculate_statistics()&lt;br /&gt;
save_report()&lt;br /&gt;
send_report()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each function is easier to understand and test.&lt;br /&gt;
&lt;br /&gt;
== 6. Comparison with C++ project decomposition ==&lt;br /&gt;
&lt;br /&gt;
The same software-engineering principle applies in C++ and Python.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
src/&lt;br /&gt;
├── main.cpp&lt;br /&gt;
├── storage.cpp&lt;br /&gt;
├── services.cpp&lt;br /&gt;
└── models.cpp&lt;br /&gt;
&lt;br /&gt;
include/&lt;br /&gt;
├── storage.hpp&lt;br /&gt;
├── services.hpp&lt;br /&gt;
└── models.hpp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
src/&lt;br /&gt;
└── project/&lt;br /&gt;
    ├── main.py&lt;br /&gt;
    ├── storage.py&lt;br /&gt;
    ├── services.py&lt;br /&gt;
    └── models.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python usually does not separate declarations from implementations into header and source files.&lt;br /&gt;
&lt;br /&gt;
== 7. High cohesion ==&lt;br /&gt;
&lt;br /&gt;
Code inside one module should be strongly related.&lt;br /&gt;
&lt;br /&gt;
Good:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
storage.py&lt;br /&gt;
&lt;br /&gt;
load_students()&lt;br /&gt;
save_students()&lt;br /&gt;
load_configuration()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potentially poor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
utils.py&lt;br /&gt;
&lt;br /&gt;
load_students()&lt;br /&gt;
resize_image()&lt;br /&gt;
send_email()&lt;br /&gt;
calculate_average()&lt;br /&gt;
parse_network_packet()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A generic &amp;lt;code&amp;gt;utils.py&amp;lt;/code&amp;gt; file often becomes a collection of unrelated functions.&lt;br /&gt;
&lt;br /&gt;
Prefer meaningful modules with clear responsibilities.&lt;br /&gt;
&lt;br /&gt;
== 8. Low coupling ==&lt;br /&gt;
&lt;br /&gt;
Modules should avoid unnecessary dependencies on each other.&lt;br /&gt;
&lt;br /&gt;
A simple dependency direction such as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
main.py&lt;br /&gt;
  ↓&lt;br /&gt;
services.py&lt;br /&gt;
  ↓&lt;br /&gt;
storage.py&lt;br /&gt;
  ↓&lt;br /&gt;
models.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is easier to reason about than a design where every module imports and depends on every other module.&lt;br /&gt;
&lt;br /&gt;
== 9. Circular dependencies ==&lt;br /&gt;
&lt;br /&gt;
A common architectural problem is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
module_a imports module_b&lt;br /&gt;
module_b imports module_a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | models.py&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | services.py&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from services import calculate_score&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from models import Student&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This may create a circular import and usually indicates misplaced responsibilities.&lt;br /&gt;
&lt;br /&gt;
== 10. Fixing circular dependencies ==&lt;br /&gt;
&lt;br /&gt;
Possible solutions include:&lt;br /&gt;
&lt;br /&gt;
* move shared abstractions to a third module;&lt;br /&gt;
* move logic to the module where it belongs;&lt;br /&gt;
* invert the dependency;&lt;br /&gt;
* pass objects or functions as parameters instead of importing them globally.&lt;br /&gt;
&lt;br /&gt;
A simpler dependency structure is often:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
models.py&lt;br /&gt;
    ↑&lt;br /&gt;
services.py&lt;br /&gt;
    ↑&lt;br /&gt;
main.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
rather than having &amp;lt;code&amp;gt;models.py&amp;lt;/code&amp;gt; call into &amp;lt;code&amp;gt;services.py&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 11. Interfaces between components ==&lt;br /&gt;
&lt;br /&gt;
Components should communicate through small, explicit interfaces.&lt;br /&gt;
&lt;br /&gt;
Less clear:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
storage.data&lt;br /&gt;
storage.filename&lt;br /&gt;
storage.format&lt;br /&gt;
storage.cache&lt;br /&gt;
storage.internal_state&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prefer a small public interface:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
storage.load()&lt;br /&gt;
storage.save(data)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The implementation details remain inside the component.&lt;br /&gt;
&lt;br /&gt;
== 12. C++ interface versus Python protocol ==&lt;br /&gt;
&lt;br /&gt;
In C++, an interface may be represented using an abstract base class.&lt;br /&gt;
&lt;br /&gt;
Python can use inheritance, but often only the required behavior matters.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Storage {&lt;br /&gt;
public:&lt;br /&gt;
    virtual Data load() = 0;&lt;br /&gt;
&lt;br /&gt;
    virtual void save(&lt;br /&gt;
        const Data&amp;amp; data&lt;br /&gt;
    ) = 0;&lt;br /&gt;
&lt;br /&gt;
    virtual ~Storage() = default;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class JsonStorage:&lt;br /&gt;
    def load(self):&lt;br /&gt;
        ...&lt;br /&gt;
&lt;br /&gt;
    def save(self, data):&lt;br /&gt;
        ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Any Python object providing the expected methods can be used by code that relies on this behavior.&lt;br /&gt;
&lt;br /&gt;
== 13. Dependency injection ==&lt;br /&gt;
&lt;br /&gt;
A component should not always create its own dependencies internally.&lt;br /&gt;
&lt;br /&gt;
Less flexible:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class StudentService:&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        self.storage = JsonStorage(&lt;br /&gt;
            &amp;quot;students.json&amp;quot;&lt;br /&gt;
        )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More flexible:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class StudentService:&lt;br /&gt;
    def __init__(self, storage):&lt;br /&gt;
        self.storage = storage&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Application setup:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
storage = JsonStorage(&amp;quot;students.json&amp;quot;)&lt;br /&gt;
service = StudentService(storage)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Tests can now use a fake storage implementation.&lt;br /&gt;
&lt;br /&gt;
== 14. C++ dependency injection versus Python ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Service {&lt;br /&gt;
public:&lt;br /&gt;
    Service(Storage&amp;amp; storage)&lt;br /&gt;
        : storage(storage)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    Storage&amp;amp; storage;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Service:&lt;br /&gt;
    def __init__(self, storage):&lt;br /&gt;
        self.storage = storage&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The architectural idea is the same.&lt;br /&gt;
&lt;br /&gt;
== 15. Dependency inversion ==&lt;br /&gt;
&lt;br /&gt;
High-level application logic should ideally depend on behavior rather than a specific implementation.&lt;br /&gt;
&lt;br /&gt;
Instead of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
StudentService&lt;br /&gt;
      ↓&lt;br /&gt;
JsonStorage&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
think in terms of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
StudentService&lt;br /&gt;
      ↓&lt;br /&gt;
storage behavior&lt;br /&gt;
      ↑&lt;br /&gt;
JsonStorage&lt;br /&gt;
CsvStorage&lt;br /&gt;
MemoryStorage&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This makes testing and future changes easier.&lt;br /&gt;
&lt;br /&gt;
== 16. Avoid unnecessary abstractions ==&lt;br /&gt;
&lt;br /&gt;
Do not create interfaces, factories and abstraction layers merely because they are possible.&lt;br /&gt;
&lt;br /&gt;
For a project that will only ever read one small JSON file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def load_students(path):&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
may be sufficient.&lt;br /&gt;
&lt;br /&gt;
Use abstractions when they solve a real design problem.&lt;br /&gt;
&lt;br /&gt;
== 17. Functions versus classes ==&lt;br /&gt;
&lt;br /&gt;
Not every Python component needs to be a class.&lt;br /&gt;
&lt;br /&gt;
Use a function when:&lt;br /&gt;
&lt;br /&gt;
* no persistent state is required;&lt;br /&gt;
* the operation is naturally expressed as input → output;&lt;br /&gt;
* no meaningful object identity exists.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Unnecessary class&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Simpler function&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class AverageCalculator:&lt;br /&gt;
    def calculate(self, values):&lt;br /&gt;
        return sum(values) / len(values)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(values):&lt;br /&gt;
    return sum(values) / len(values)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A class is useful when state belongs together and several operations work on that state.&lt;br /&gt;
&lt;br /&gt;
== 18. Large functions ==&lt;br /&gt;
&lt;br /&gt;
A function that performs many distinct operations is difficult to:&lt;br /&gt;
&lt;br /&gt;
* understand;&lt;br /&gt;
* test;&lt;br /&gt;
* reuse;&lt;br /&gt;
* modify safely.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def process_file(path):&lt;br /&gt;
    # Open file.&lt;br /&gt;
    # Parse format.&lt;br /&gt;
    # Validate records.&lt;br /&gt;
    # Calculate metrics.&lt;br /&gt;
    # Print report.&lt;br /&gt;
    # Save results.&lt;br /&gt;
    # Send network request.&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Break large functions into smaller operations with clear names.&lt;br /&gt;
&lt;br /&gt;
== 19. Refactoring a large function ==&lt;br /&gt;
&lt;br /&gt;
Before:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def run(path):&lt;br /&gt;
    content = Path(path).read_text()&lt;br /&gt;
&lt;br /&gt;
    data = json.loads(content)&lt;br /&gt;
&lt;br /&gt;
    students = []&lt;br /&gt;
&lt;br /&gt;
    for item in data:&lt;br /&gt;
        if not 1 &amp;lt;= item[&amp;quot;grade&amp;quot;] &amp;lt;= 10:&lt;br /&gt;
            raise ValueError()&lt;br /&gt;
&lt;br /&gt;
        students.append(&lt;br /&gt;
            Student(&lt;br /&gt;
                item[&amp;quot;name&amp;quot;],&lt;br /&gt;
                item[&amp;quot;grade&amp;quot;],&lt;br /&gt;
            )&lt;br /&gt;
        )&lt;br /&gt;
&lt;br /&gt;
    avg = sum(&lt;br /&gt;
        student.grade&lt;br /&gt;
        for student in students&lt;br /&gt;
    ) / len(students)&lt;br /&gt;
&lt;br /&gt;
    print(avg)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def load_json(path):&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def parse_students(data):&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def average(students):&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    data = load_json(&amp;quot;students.json&amp;quot;)&lt;br /&gt;
    students = parse_students(data)&lt;br /&gt;
&lt;br /&gt;
    print(average(students))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second version separates responsibilities and is easier to test.&lt;br /&gt;
&lt;br /&gt;
== 20. Large classes ==&lt;br /&gt;
&lt;br /&gt;
A class that handles many unrelated responsibilities may become a &amp;quot;god object&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Warning signs include:&lt;br /&gt;
&lt;br /&gt;
* many unrelated methods;&lt;br /&gt;
* many attributes;&lt;br /&gt;
* methods manipulating unrelated subsets of attributes;&lt;br /&gt;
* knowledge of storage, user interface and domain logic simultaneously.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
ApplicationManager&lt;br /&gt;
├── load_json()&lt;br /&gt;
├── save_csv()&lt;br /&gt;
├── calculate_average()&lt;br /&gt;
├── send_email()&lt;br /&gt;
├── draw_graph()&lt;br /&gt;
├── authenticate_user()&lt;br /&gt;
└── create_report()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This should probably be several components.&lt;br /&gt;
&lt;br /&gt;
== 21. Data models ==&lt;br /&gt;
&lt;br /&gt;
Simple data structures should remain simple.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from dataclasses import dataclass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@dataclass&lt;br /&gt;
class Student:&lt;br /&gt;
    name: str&lt;br /&gt;
    grade: float&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not place unrelated application behavior inside a data model merely because the class already exists.&lt;br /&gt;
&lt;br /&gt;
== 22. Domain logic ==&lt;br /&gt;
&lt;br /&gt;
Domain logic represents the rules of the application.&lt;br /&gt;
&lt;br /&gt;
Examples include:&lt;br /&gt;
&lt;br /&gt;
* calculating grades;&lt;br /&gt;
* validating measurements;&lt;br /&gt;
* processing images;&lt;br /&gt;
* selecting routing decisions;&lt;br /&gt;
* determining alarms;&lt;br /&gt;
* filtering sensor data.&lt;br /&gt;
&lt;br /&gt;
Domain logic should generally not depend directly on:&lt;br /&gt;
&lt;br /&gt;
* terminal input;&lt;br /&gt;
* GUI widgets;&lt;br /&gt;
* HTTP request objects;&lt;br /&gt;
* database cursors.&lt;br /&gt;
&lt;br /&gt;
This separation makes domain logic reusable and testable.&lt;br /&gt;
&lt;br /&gt;
== 23. Input/output boundaries ==&lt;br /&gt;
&lt;br /&gt;
Try to separate:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Input&lt;br /&gt;
   ↓&lt;br /&gt;
Parsing&lt;br /&gt;
   ↓&lt;br /&gt;
Domain logic&lt;br /&gt;
   ↓&lt;br /&gt;
Formatting&lt;br /&gt;
   ↓&lt;br /&gt;
Output&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def calculate_average(students):&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    students = load_students(...)&lt;br /&gt;
&lt;br /&gt;
    result = calculate_average(students)&lt;br /&gt;
&lt;br /&gt;
    print(result)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The calculation does not know where the students came from or where the result will be displayed.&lt;br /&gt;
&lt;br /&gt;
== 24. Configuration ==&lt;br /&gt;
&lt;br /&gt;
Do not scatter configuration values throughout the code.&lt;br /&gt;
&lt;br /&gt;
Poor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
timeout = 5&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
requests.get(url, timeout=5)&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
if retry_count &amp;gt; 3:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prefer centralized configuration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from dataclasses import dataclass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@dataclass&lt;br /&gt;
class Config:&lt;br /&gt;
    timeout: float&lt;br /&gt;
    max_retries: int&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or a configuration file.&lt;br /&gt;
&lt;br /&gt;
== 25. Constants versus configuration ==&lt;br /&gt;
&lt;br /&gt;
A constant is part of the program&amp;#039;s logic.&lt;br /&gt;
&lt;br /&gt;
Configuration is something that may reasonably differ between installations or runs.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Value&lt;br /&gt;
! More likely&lt;br /&gt;
|-&lt;br /&gt;
| mathematical constant&lt;br /&gt;
| constant&lt;br /&gt;
|-&lt;br /&gt;
| network server hostname&lt;br /&gt;
| configuration&lt;br /&gt;
|-&lt;br /&gt;
| API timeout&lt;br /&gt;
| configuration&lt;br /&gt;
|-&lt;br /&gt;
| protocol packet size fixed by specification&lt;br /&gt;
| constant&lt;br /&gt;
|-&lt;br /&gt;
| output directory&lt;br /&gt;
| configuration&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 26. Environment variables ==&lt;br /&gt;
&lt;br /&gt;
Secrets and deployment-specific values should not be hard-coded.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Poor&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Better&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
API_KEY = &amp;quot;abc123-secret&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import os&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
api_key = os.environ[&amp;quot;API_KEY&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Do not commit passwords, tokens or private keys to Git.&lt;br /&gt;
&lt;br /&gt;
== 27. Paths should not be hard-coded ==&lt;br /&gt;
&lt;br /&gt;
Avoid:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
path = &amp;quot;/home/alice/project/data/file.json&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prefer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from pathlib import Path&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
path = Path(&amp;quot;data&amp;quot;) / &amp;quot;file.json&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or accept the path as configuration.&lt;br /&gt;
&lt;br /&gt;
== 28. Error boundaries ==&lt;br /&gt;
&lt;br /&gt;
Not every function should catch every exception.&lt;br /&gt;
&lt;br /&gt;
Low-level functions can often raise meaningful exceptions.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def load_students(path):&lt;br /&gt;
    return json.loads(&lt;br /&gt;
        path.read_text(&lt;br /&gt;
            encoding=&amp;quot;utf-8&amp;quot;&lt;br /&gt;
        )&lt;br /&gt;
    )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Higher-level application code decides what the user should see:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
    try:&lt;br /&gt;
        students = load_students(...)&lt;br /&gt;
    except FileNotFoundError:&lt;br /&gt;
        print(&amp;quot;Student file not found&amp;quot;)&lt;br /&gt;
        return&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 29. Do not suppress errors silently ==&lt;br /&gt;
&lt;br /&gt;
Avoid:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    process()&lt;br /&gt;
except Exception:&lt;br /&gt;
    pass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This hides failures and makes debugging difficult.&lt;br /&gt;
&lt;br /&gt;
If an error is intentionally ignored, the reason should be explicit and narrow.&lt;br /&gt;
&lt;br /&gt;
== 30. Logging project events ==&lt;br /&gt;
&lt;br /&gt;
Use logging for diagnostics that may be useful during operation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import logging&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
logger = logging.getLogger(__name__)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def load_students(path):&lt;br /&gt;
    logger.info(&lt;br /&gt;
        &amp;quot;Loading students from %s&amp;quot;,&lt;br /&gt;
        path,&lt;br /&gt;
    )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; for intentional user-facing terminal output and logging for diagnostics.&lt;br /&gt;
&lt;br /&gt;
== 31. Project dependencies ==&lt;br /&gt;
&lt;br /&gt;
Every third-party dependency should have a reason to exist.&lt;br /&gt;
&lt;br /&gt;
Before adding a package, ask:&lt;br /&gt;
&lt;br /&gt;
* does the standard library already provide the functionality?&lt;br /&gt;
* is the package significantly simplifying the implementation?&lt;br /&gt;
* is it compatible with the project&amp;#039;s Python version?&lt;br /&gt;
* is it required by the application or only by development tooling?&lt;br /&gt;
&lt;br /&gt;
== 32. Standard library versus external dependency ==&lt;br /&gt;
&lt;br /&gt;
For a small CSV file:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Standard library&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | External library&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import csv&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import pandas as pd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
If dataframe functionality is not required, &amp;lt;code&amp;gt;csv&amp;lt;/code&amp;gt; may be sufficient.&lt;br /&gt;
&lt;br /&gt;
Avoid dependencies that add complexity without solving a real problem.&lt;br /&gt;
&lt;br /&gt;
== 33. Dependency declaration ==&lt;br /&gt;
&lt;br /&gt;
Dependencies should be recorded in project metadata.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[project]&lt;br /&gt;
name = &amp;quot;example-project&amp;quot;&lt;br /&gt;
version = &amp;quot;0.1.0&amp;quot;&lt;br /&gt;
requires-python = &amp;quot;&amp;gt;=3.11&amp;quot;&lt;br /&gt;
&lt;br /&gt;
dependencies = [&lt;br /&gt;
    &amp;quot;requests&amp;gt;=2.32&amp;quot;,&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another developer should not have to guess which packages to install.&lt;br /&gt;
&lt;br /&gt;
== 34. Reproducibility ==&lt;br /&gt;
&lt;br /&gt;
Another student or evaluator should be able to clone the project and determine:&lt;br /&gt;
&lt;br /&gt;
* which Python version is expected;&lt;br /&gt;
* which dependencies are required;&lt;br /&gt;
* how to install them;&lt;br /&gt;
* how to start the application;&lt;br /&gt;
* how to run tests.&lt;br /&gt;
&lt;br /&gt;
These instructions belong in the &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 35. Git is part of the project ==&lt;br /&gt;
&lt;br /&gt;
The Git repository is not merely a place to submit the final files.&lt;br /&gt;
&lt;br /&gt;
It should document the development history.&lt;br /&gt;
&lt;br /&gt;
Useful commit messages include:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Add JSON storage layer&lt;br /&gt;
Implement student validation&lt;br /&gt;
Add average calculation tests&lt;br /&gt;
Refactor configuration loading&lt;br /&gt;
Handle missing input files&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 36. Poor commit messages ==&lt;br /&gt;
&lt;br /&gt;
Avoid:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
update&lt;br /&gt;
fix&lt;br /&gt;
stuff&lt;br /&gt;
changes&lt;br /&gt;
final&lt;br /&gt;
final2&lt;br /&gt;
final_final&lt;br /&gt;
asdf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Commit messages should indicate what changed.&lt;br /&gt;
&lt;br /&gt;
== 37. Commit scope ==&lt;br /&gt;
&lt;br /&gt;
A commit should ideally represent one coherent change.&lt;br /&gt;
&lt;br /&gt;
Poor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Implement API client,&lt;br /&gt;
rename every variable,&lt;br /&gt;
format whole project,&lt;br /&gt;
change storage,&lt;br /&gt;
add documentation&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Better:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Add API client&lt;br /&gt;
&lt;br /&gt;
Add retry handling&lt;br /&gt;
&lt;br /&gt;
Refactor storage interface&lt;br /&gt;
&lt;br /&gt;
Document API configuration&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Small coherent commits are easier to review and revert.&lt;br /&gt;
&lt;br /&gt;
== 38. Git workflow ==&lt;br /&gt;
&lt;br /&gt;
A simple project workflow is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
main&lt;br /&gt;
  │&lt;br /&gt;
  ├── feature/json-storage&lt;br /&gt;
  │&lt;br /&gt;
  ├── feature/report-generation&lt;br /&gt;
  │&lt;br /&gt;
  └── fix/input-validation&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For small student teams, a lightweight branch workflow is sufficient.&lt;br /&gt;
&lt;br /&gt;
== 39. Creating a feature branch ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git switch -c feature/json-storage&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Work normally:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git add .&lt;br /&gt;
git commit -m &amp;quot;Add JSON storage layer&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then merge after review.&lt;br /&gt;
&lt;br /&gt;
== 40. Keep main usable ==&lt;br /&gt;
&lt;br /&gt;
The main branch should ideally remain in a usable state.&lt;br /&gt;
&lt;br /&gt;
Before merging:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ruff check .&lt;br /&gt;
mypy src&lt;br /&gt;
pytest&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A branch that fails the project&amp;#039;s normal checks should generally not be merged.&lt;br /&gt;
&lt;br /&gt;
== 41. Merge conflicts ==&lt;br /&gt;
&lt;br /&gt;
A merge conflict means Git cannot determine automatically how two changes should be combined.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt; HEAD&lt;br /&gt;
timeout = 5&lt;br /&gt;
=======&lt;br /&gt;
timeout = 10&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; feature/config&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The developer must decide which final content is correct and remove the conflict markers.&lt;br /&gt;
&lt;br /&gt;
== 42. Team ownership ==&lt;br /&gt;
&lt;br /&gt;
Avoid assigning entire files permanently to individual team members.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Student A owns main.py&lt;br /&gt;
Student B owns services.py&lt;br /&gt;
Student C owns storage.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can produce isolated development where nobody understands the complete project.&lt;br /&gt;
&lt;br /&gt;
Instead, assign features or responsibilities while maintaining shared understanding of the architecture.&lt;br /&gt;
&lt;br /&gt;
== 43. Code review ==&lt;br /&gt;
&lt;br /&gt;
Before accepting a significant change, another team member should inspect it.&lt;br /&gt;
&lt;br /&gt;
A code review should consider:&lt;br /&gt;
&lt;br /&gt;
* correctness;&lt;br /&gt;
* clarity;&lt;br /&gt;
* architecture;&lt;br /&gt;
* duplication;&lt;br /&gt;
* error handling;&lt;br /&gt;
* test coverage;&lt;br /&gt;
* naming;&lt;br /&gt;
* unnecessary complexity.&lt;br /&gt;
&lt;br /&gt;
The purpose is to improve code, not to evaluate the programmer personally.&lt;br /&gt;
&lt;br /&gt;
== 44. Questions for code review ==&lt;br /&gt;
&lt;br /&gt;
Useful review questions include:&lt;br /&gt;
&lt;br /&gt;
* What responsibility does this function or class have?&lt;br /&gt;
* Why is this a class rather than a function?&lt;br /&gt;
* Why does this module import that module?&lt;br /&gt;
* What happens with invalid input?&lt;br /&gt;
* Can this dependency be replaced during testing?&lt;br /&gt;
* Is this behavior tested?&lt;br /&gt;
* Is this abstraction necessary?&lt;br /&gt;
* Can this code be simplified?&lt;br /&gt;
* Is this duplicated elsewhere?&lt;br /&gt;
* Does the caller need to know this implementation detail?&lt;br /&gt;
&lt;br /&gt;
== 45. Naming ==&lt;br /&gt;
&lt;br /&gt;
Names should communicate intent.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Poor&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Better&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def proc(x, d):&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def calculate_average(&lt;br /&gt;
    students,&lt;br /&gt;
    minimum_grade,&lt;br /&gt;
):&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Longer descriptive names are often preferable to short ambiguous names.&lt;br /&gt;
&lt;br /&gt;
== 46. Python naming conventions ==&lt;br /&gt;
&lt;br /&gt;
Common conventions include:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Element&lt;br /&gt;
! Convention&lt;br /&gt;
! Example&lt;br /&gt;
|-&lt;br /&gt;
| variable&lt;br /&gt;
| &amp;lt;code&amp;gt;snake_case&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;student_count&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| function&lt;br /&gt;
| &amp;lt;code&amp;gt;snake_case&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;load_students&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| class&lt;br /&gt;
| &amp;lt;code&amp;gt;PascalCase&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;StudentRepository&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| constant&lt;br /&gt;
| &amp;lt;code&amp;gt;UPPER_CASE&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;MAX_RETRIES&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| internal name&lt;br /&gt;
| leading underscore&lt;br /&gt;
| &amp;lt;code&amp;gt;_parse_record&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 47. Comments ==&lt;br /&gt;
&lt;br /&gt;
Comments should explain &amp;#039;&amp;#039;&amp;#039;why&amp;#039;&amp;#039;&amp;#039;, not merely repeat the code.&lt;br /&gt;
&lt;br /&gt;
Poor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# Increment i.&lt;br /&gt;
i += 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Useful:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# Skip the first row because the&lt;br /&gt;
# device includes a proprietary header.&lt;br /&gt;
data = data[1:]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Readable code should explain most of the &amp;#039;&amp;#039;&amp;#039;what&amp;#039;&amp;#039;&amp;#039; by itself.&lt;br /&gt;
&lt;br /&gt;
== 48. Docstrings ==&lt;br /&gt;
&lt;br /&gt;
Public modules, classes and non-trivial functions may benefit from docstrings.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def calculate_average(&lt;br /&gt;
    grades: list[float],&lt;br /&gt;
) -&amp;gt; float:&lt;br /&gt;
    # Return the arithmetic mean of grades.&lt;br /&gt;
    if not grades:&lt;br /&gt;
        raise ValueError(&amp;quot;grades is empty&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    return sum(grades) / len(grades)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the actual project, a short function docstring can be used when additional explanation is useful.&lt;br /&gt;
&lt;br /&gt;
Do not write verbose documentation for trivial code when the name and type hints already make the behavior obvious.&lt;br /&gt;
&lt;br /&gt;
== 49. Type hints as interfaces ==&lt;br /&gt;
&lt;br /&gt;
Type hints make module boundaries clearer.&lt;br /&gt;
&lt;br /&gt;
Compare:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Less explicit&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | More explicit&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def process(data):&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def process(&lt;br /&gt;
    students: list[Student],&lt;br /&gt;
) -&amp;gt; Report:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The second version communicates more about the expected interface.&lt;br /&gt;
&lt;br /&gt;
== 50. Return values should be predictable ==&lt;br /&gt;
&lt;br /&gt;
Avoid functions that sometimes return unrelated types.&lt;br /&gt;
&lt;br /&gt;
Poor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def load_data(path):&lt;br /&gt;
    if error:&lt;br /&gt;
        return False&lt;br /&gt;
&lt;br /&gt;
    if empty:&lt;br /&gt;
        return None&lt;br /&gt;
&lt;br /&gt;
    return records&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prefer a consistent return type and exceptions for failure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def load_data(&lt;br /&gt;
    path: Path,&lt;br /&gt;
) -&amp;gt; list[Record]:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 51. Avoid magic values ==&lt;br /&gt;
&lt;br /&gt;
Poor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if status == 3:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Better:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
STATUS_READY = 3&lt;br /&gt;
&lt;br /&gt;
if status == STATUS_READY:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or use an enumeration when appropriate.&lt;br /&gt;
&lt;br /&gt;
== 52. Enumerations ==&lt;br /&gt;
&lt;br /&gt;
Python provides &amp;lt;code&amp;gt;Enum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
enum class Status {&lt;br /&gt;
    Idle,&lt;br /&gt;
    Running,&lt;br /&gt;
    Error&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from enum import Enum&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Status(Enum):&lt;br /&gt;
    IDLE = &amp;quot;idle&amp;quot;&lt;br /&gt;
    RUNNING = &amp;quot;running&amp;quot;&lt;br /&gt;
    ERROR = &amp;quot;error&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Use enums when a value belongs to a small, fixed set of meaningful states.&lt;br /&gt;
&lt;br /&gt;
== 53. Avoid unnecessary global variables ==&lt;br /&gt;
&lt;br /&gt;
Poor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
students = []&lt;br /&gt;
config = {}&lt;br /&gt;
connection = None&lt;br /&gt;
current_user = None&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Global mutable state creates hidden dependencies.&lt;br /&gt;
&lt;br /&gt;
Prefer explicit state:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Application:&lt;br /&gt;
    def __init__(&lt;br /&gt;
        self,&lt;br /&gt;
        config,&lt;br /&gt;
        storage,&lt;br /&gt;
    ):&lt;br /&gt;
        self.config = config&lt;br /&gt;
        self.storage = storage&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or pass data between functions.&lt;br /&gt;
&lt;br /&gt;
== 54. Mutable default arguments ==&lt;br /&gt;
&lt;br /&gt;
A common Python-specific bug is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def add_student(&lt;br /&gt;
    student,&lt;br /&gt;
    students=[],&lt;br /&gt;
):&lt;br /&gt;
    students.append(student)&lt;br /&gt;
&lt;br /&gt;
    return students&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same list is reused across calls.&lt;br /&gt;
&lt;br /&gt;
Correct:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def add_student(&lt;br /&gt;
    student,&lt;br /&gt;
    students=None,&lt;br /&gt;
):&lt;br /&gt;
    if students is None:&lt;br /&gt;
        students = []&lt;br /&gt;
&lt;br /&gt;
    students.append(student)&lt;br /&gt;
&lt;br /&gt;
    return students&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This issue should be checked during code review.&lt;br /&gt;
&lt;br /&gt;
== 55. Resource management ==&lt;br /&gt;
&lt;br /&gt;
Files and other resources should be managed explicitly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Less robust&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Preferred&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
file = open(path)&lt;br /&gt;
&lt;br /&gt;
data = file.read()&lt;br /&gt;
&lt;br /&gt;
file.close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
with path.open(&lt;br /&gt;
    &amp;quot;r&amp;quot;,&lt;br /&gt;
    encoding=&amp;quot;utf-8&amp;quot;,&lt;br /&gt;
) as file:&lt;br /&gt;
    data = file.read()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The context manager guarantees cleanup even if an exception occurs.&lt;br /&gt;
&lt;br /&gt;
== 56. Avoid duplicated code ==&lt;br /&gt;
&lt;br /&gt;
If the same non-trivial logic appears several times, consider extracting it.&lt;br /&gt;
&lt;br /&gt;
Before:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if not 1 &amp;lt;= grade &amp;lt;= 10:&lt;br /&gt;
    raise ValueError()&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
if not 1 &amp;lt;= grade &amp;lt;= 10:&lt;br /&gt;
    raise ValueError()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def validate_grade(grade):&lt;br /&gt;
    if not 1 &amp;lt;= grade &amp;lt;= 10:&lt;br /&gt;
        raise ValueError()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not remove every repeated line mechanically. Extract repeated concepts, not coincidental similarity.&lt;br /&gt;
&lt;br /&gt;
== 57. Avoid premature optimization ==&lt;br /&gt;
&lt;br /&gt;
Do not complicate the project for performance before measuring a real problem.&lt;br /&gt;
&lt;br /&gt;
Correctness and clarity come first.&lt;br /&gt;
&lt;br /&gt;
If performance is part of the project, measure it.&lt;br /&gt;
&lt;br /&gt;
A simple timing experiment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from time import perf_counter&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
start = perf_counter()&lt;br /&gt;
&lt;br /&gt;
result = process(data)&lt;br /&gt;
&lt;br /&gt;
elapsed = perf_counter() - start&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;{elapsed:.6f} s&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For serious benchmarking, use repeated measurements and dedicated tools.&lt;br /&gt;
&lt;br /&gt;
== 58. Project startup ==&lt;br /&gt;
&lt;br /&gt;
The project should have one obvious way to start.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m project_name&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m project_name.main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The README should document the expected command.&lt;br /&gt;
&lt;br /&gt;
== 59. Application entry point ==&lt;br /&gt;
&lt;br /&gt;
Keep startup code thin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
    config = load_config()&lt;br /&gt;
    storage = create_storage(config)&lt;br /&gt;
&lt;br /&gt;
    app = Application(&lt;br /&gt;
        config,&lt;br /&gt;
        storage,&lt;br /&gt;
    )&lt;br /&gt;
&lt;br /&gt;
    app.run()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The entry point assembles components rather than implementing the entire application.&lt;br /&gt;
&lt;br /&gt;
== 60. Architecture should follow the problem ==&lt;br /&gt;
&lt;br /&gt;
An image-processing project may need:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
io.py&lt;br /&gt;
pipeline.py&lt;br /&gt;
filters.py&lt;br /&gt;
models.py&lt;br /&gt;
cli.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A network application may need:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
protocol.py&lt;br /&gt;
transport.py&lt;br /&gt;
messages.py&lt;br /&gt;
client.py&lt;br /&gt;
server.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A data-analysis project may need:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
loaders.py&lt;br /&gt;
preprocessing.py&lt;br /&gt;
analysis.py&lt;br /&gt;
visualization.py&lt;br /&gt;
main.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choose module boundaries based on responsibilities.&lt;br /&gt;
&lt;br /&gt;
== 61. Example — image-processing pipeline ==&lt;br /&gt;
&lt;br /&gt;
A poor design:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
    image = cv2.imread(...)&lt;br /&gt;
&lt;br /&gt;
    # preprocessing&lt;br /&gt;
    ...&lt;br /&gt;
    # filtering&lt;br /&gt;
    ...&lt;br /&gt;
    # segmentation&lt;br /&gt;
    ...&lt;br /&gt;
    # statistics&lt;br /&gt;
    ...&lt;br /&gt;
    # visualization&lt;br /&gt;
    ...&lt;br /&gt;
    # saving&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A clearer design:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def load_image(path):&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def preprocess(image):&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def segment(image):&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def calculate_metrics(image):&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def save_result(path, result):&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 62. Example — networking project ==&lt;br /&gt;
&lt;br /&gt;
Separate protocol logic from transport where possible.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
protocol.py&lt;br /&gt;
    encode_message()&lt;br /&gt;
    decode_message()&lt;br /&gt;
&lt;br /&gt;
transport.py&lt;br /&gt;
    send()&lt;br /&gt;
    receive()&lt;br /&gt;
&lt;br /&gt;
client.py&lt;br /&gt;
    application workflow&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows protocol parsing to be tested without opening a real network connection.&lt;br /&gt;
&lt;br /&gt;
== 63. Example — hardware project ==&lt;br /&gt;
&lt;br /&gt;
Separate hardware access from application logic.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
device.py&lt;br /&gt;
    low-level communication&lt;br /&gt;
&lt;br /&gt;
controller.py&lt;br /&gt;
    application behavior&lt;br /&gt;
&lt;br /&gt;
models.py&lt;br /&gt;
    data structures&lt;br /&gt;
&lt;br /&gt;
main.py&lt;br /&gt;
    startup&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Tests can replace the device implementation with a fake device.&lt;br /&gt;
&lt;br /&gt;
== 64. Project review checklist ==&lt;br /&gt;
&lt;br /&gt;
During the laboratory, each project should be reviewed using the following questions:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Area&lt;br /&gt;
! Question&lt;br /&gt;
|-&lt;br /&gt;
| goal&lt;br /&gt;
| What problem does the project solve?&lt;br /&gt;
|-&lt;br /&gt;
| MVP&lt;br /&gt;
| What is the smallest useful end-to-end workflow?&lt;br /&gt;
|-&lt;br /&gt;
| architecture&lt;br /&gt;
| What are the main components?&lt;br /&gt;
|-&lt;br /&gt;
| modules&lt;br /&gt;
| Does each module have a clear responsibility?&lt;br /&gt;
|-&lt;br /&gt;
| dependencies&lt;br /&gt;
| Are dependencies explicit?&lt;br /&gt;
|-&lt;br /&gt;
| state&lt;br /&gt;
| Is unnecessary global mutable state present?&lt;br /&gt;
|-&lt;br /&gt;
| errors&lt;br /&gt;
| What failures are expected and how are they handled?&lt;br /&gt;
|-&lt;br /&gt;
| tests&lt;br /&gt;
| Can central logic be tested without external systems?&lt;br /&gt;
|-&lt;br /&gt;
| configuration&lt;br /&gt;
| Are machine-specific values separated from code?&lt;br /&gt;
|-&lt;br /&gt;
| Git&lt;br /&gt;
| Does the history contain meaningful commits?&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 65. Student architecture explanation ==&lt;br /&gt;
&lt;br /&gt;
Each team should be able to explain its architecture in approximately two minutes.&lt;br /&gt;
&lt;br /&gt;
A useful structure is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Input&lt;br /&gt;
  ↓&lt;br /&gt;
Component A&lt;br /&gt;
  ↓&lt;br /&gt;
Component B&lt;br /&gt;
  ↓&lt;br /&gt;
Component C&lt;br /&gt;
  ↓&lt;br /&gt;
Output&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The explanation should identify:&lt;br /&gt;
&lt;br /&gt;
* where data enters the system;&lt;br /&gt;
* where central processing occurs;&lt;br /&gt;
* where external dependencies exist;&lt;br /&gt;
* where data leaves or is stored.&lt;br /&gt;
&lt;br /&gt;
== 66. Module dependency diagram ==&lt;br /&gt;
&lt;br /&gt;
Each team should create a simple dependency diagram.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
main&lt;br /&gt;
 ├── config&lt;br /&gt;
 └── service&lt;br /&gt;
      ├── model&lt;br /&gt;
      └── storage&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Avoid diagrams that include every individual function.&lt;br /&gt;
&lt;br /&gt;
The objective is to understand high-level structure.&lt;br /&gt;
&lt;br /&gt;
== 67. MVP demonstration ==&lt;br /&gt;
&lt;br /&gt;
The MVP demonstration should be based on running software.&lt;br /&gt;
&lt;br /&gt;
A useful demonstration includes:&lt;br /&gt;
&lt;br /&gt;
# starting the application;&lt;br /&gt;
# providing real or representative input;&lt;br /&gt;
# executing the central workflow;&lt;br /&gt;
# showing the result;&lt;br /&gt;
# demonstrating one handled error condition.&lt;br /&gt;
&lt;br /&gt;
Slides are not required for this milestone.&lt;br /&gt;
&lt;br /&gt;
== 68. Failure demonstration ==&lt;br /&gt;
&lt;br /&gt;
A robust project should also demonstrate expected failure behavior.&lt;br /&gt;
&lt;br /&gt;
Examples include:&lt;br /&gt;
&lt;br /&gt;
* missing file;&lt;br /&gt;
* invalid input;&lt;br /&gt;
* unavailable device;&lt;br /&gt;
* malformed message;&lt;br /&gt;
* timeout;&lt;br /&gt;
* unsupported format.&lt;br /&gt;
&lt;br /&gt;
The program should fail predictably and explain the problem appropriately.&lt;br /&gt;
&lt;br /&gt;
== 69. No silent fallback ==&lt;br /&gt;
&lt;br /&gt;
Avoid behavior such as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    load_real_data()&lt;br /&gt;
except Exception:&lt;br /&gt;
    data = []&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
unless an empty dataset is genuinely the intended fallback.&lt;br /&gt;
&lt;br /&gt;
Unexpected errors should remain visible.&lt;br /&gt;
&lt;br /&gt;
== 70. Code review exercise ==&lt;br /&gt;
&lt;br /&gt;
Consider:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def run():&lt;br /&gt;
    data = json.load(&lt;br /&gt;
        open(&amp;quot;data.json&amp;quot;)&lt;br /&gt;
    )&lt;br /&gt;
&lt;br /&gt;
    x = []&lt;br /&gt;
&lt;br /&gt;
    for d in data:&lt;br /&gt;
        if d[&amp;quot;v&amp;quot;] &amp;gt; 5:&lt;br /&gt;
            x.append(d[&amp;quot;v&amp;quot;] * 2)&lt;br /&gt;
&lt;br /&gt;
    print(x)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Identify at least five issues or improvement opportunities.&lt;br /&gt;
&lt;br /&gt;
Possible topics include:&lt;br /&gt;
&lt;br /&gt;
* resource management;&lt;br /&gt;
* naming;&lt;br /&gt;
* hard-coded path;&lt;br /&gt;
* separation of I/O and logic;&lt;br /&gt;
* type hints;&lt;br /&gt;
* testability;&lt;br /&gt;
* error handling.&lt;br /&gt;
&lt;br /&gt;
== 71. Possible refactoring ==&lt;br /&gt;
&lt;br /&gt;
One possible direction is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import json&lt;br /&gt;
from pathlib import Path&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def load_data(path: Path):&lt;br /&gt;
    with path.open(&lt;br /&gt;
        encoding=&amp;quot;utf-8&amp;quot;&lt;br /&gt;
    ) as file:&lt;br /&gt;
        return json.load(file)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def transform_values(records):&lt;br /&gt;
    return [&lt;br /&gt;
        record[&amp;quot;v&amp;quot;] * 2&lt;br /&gt;
        for record in records&lt;br /&gt;
        if record[&amp;quot;v&amp;quot;] &amp;gt; 5&lt;br /&gt;
    ]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    records = load_data(&lt;br /&gt;
        Path(&amp;quot;data.json&amp;quot;)&lt;br /&gt;
    )&lt;br /&gt;
&lt;br /&gt;
    values = transform_values(records)&lt;br /&gt;
&lt;br /&gt;
    print(values)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is not the only correct design.&lt;br /&gt;
&lt;br /&gt;
== 72. Testing after refactoring ==&lt;br /&gt;
&lt;br /&gt;
The transformation is now easy to test:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_transform_values():&lt;br /&gt;
    records = [&lt;br /&gt;
        {&amp;quot;v&amp;quot;: 2},&lt;br /&gt;
        {&amp;quot;v&amp;quot;: 7},&lt;br /&gt;
        {&amp;quot;v&amp;quot;: 10},&lt;br /&gt;
    ]&lt;br /&gt;
&lt;br /&gt;
    assert transform_values(records) == [&lt;br /&gt;
        14,&lt;br /&gt;
        20,&lt;br /&gt;
    ]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The architecture improves testability without adding unnecessary complexity.&lt;br /&gt;
&lt;br /&gt;
== 73. Technical debt ==&lt;br /&gt;
&lt;br /&gt;
Technical debt is code that works now but makes future changes more difficult.&lt;br /&gt;
&lt;br /&gt;
Common examples in student projects include:&lt;br /&gt;
&lt;br /&gt;
* duplicated logic;&lt;br /&gt;
* giant functions;&lt;br /&gt;
* unexplained global variables;&lt;br /&gt;
* hard-coded paths;&lt;br /&gt;
* temporary debug code;&lt;br /&gt;
* inconsistent naming;&lt;br /&gt;
* missing error handling;&lt;br /&gt;
* unnecessary dependencies;&lt;br /&gt;
* tests that only work on one machine.&lt;br /&gt;
&lt;br /&gt;
Lab 5 is the appropriate moment to remove obvious technical debt before more features are added.&lt;br /&gt;
&lt;br /&gt;
== 74. Do not rewrite everything ==&lt;br /&gt;
&lt;br /&gt;
Refactoring should be targeted.&lt;br /&gt;
&lt;br /&gt;
A complete rewrite immediately before the final project phase is risky.&lt;br /&gt;
&lt;br /&gt;
Prefer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
identify problem&lt;br /&gt;
      ↓&lt;br /&gt;
add/verify tests&lt;br /&gt;
      ↓&lt;br /&gt;
refactor small area&lt;br /&gt;
      ↓&lt;br /&gt;
run tests&lt;br /&gt;
      ↓&lt;br /&gt;
continue&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Keep working behavior intact while improving the design.&lt;br /&gt;
&lt;br /&gt;
== 75. MVP checkpoint requirements ==&lt;br /&gt;
&lt;br /&gt;
By the end of Lab 5, the project should satisfy the following minimum requirements:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Requirement&lt;br /&gt;
! Expected state&lt;br /&gt;
|-&lt;br /&gt;
| repository&lt;br /&gt;
| project stored in Git&lt;br /&gt;
|-&lt;br /&gt;
| structure&lt;br /&gt;
| application split into meaningful modules&lt;br /&gt;
|-&lt;br /&gt;
| environment&lt;br /&gt;
| dependencies installable in a virtual environment&lt;br /&gt;
|-&lt;br /&gt;
| entry point&lt;br /&gt;
| one documented way to start the application&lt;br /&gt;
|-&lt;br /&gt;
| central feature&lt;br /&gt;
| working end-to-end&lt;br /&gt;
|-&lt;br /&gt;
| errors&lt;br /&gt;
| at least one expected failure handled&lt;br /&gt;
|-&lt;br /&gt;
| tests&lt;br /&gt;
| important domain logic covered by automated tests&lt;br /&gt;
|-&lt;br /&gt;
| configuration&lt;br /&gt;
| machine-specific values not scattered through code&lt;br /&gt;
|-&lt;br /&gt;
| README&lt;br /&gt;
| installation and execution instructions present&lt;br /&gt;
|-&lt;br /&gt;
| quality&lt;br /&gt;
| normal lint/test commands succeed&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 76. Exercise 1 — Architecture review ==&lt;br /&gt;
&lt;br /&gt;
For your project, write down:&lt;br /&gt;
&lt;br /&gt;
# the main input;&lt;br /&gt;
# the main output;&lt;br /&gt;
# the central processing step;&lt;br /&gt;
# external dependencies;&lt;br /&gt;
# persistent storage, if any;&lt;br /&gt;
# the main modules.&lt;br /&gt;
&lt;br /&gt;
Draw a simple dependency diagram.&lt;br /&gt;
&lt;br /&gt;
== 77. Exercise 2 — Responsibility audit ==&lt;br /&gt;
&lt;br /&gt;
For every source module, write one sentence:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
This module is responsible for ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the sentence contains several unrelated responsibilities joined by &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt;, consider whether the module should be split.&lt;br /&gt;
&lt;br /&gt;
== 78. Exercise 3 — Large function review ==&lt;br /&gt;
&lt;br /&gt;
Find the largest function in the project.&lt;br /&gt;
&lt;br /&gt;
Answer:&lt;br /&gt;
&lt;br /&gt;
* How many responsibilities does it have?&lt;br /&gt;
* Which parts are pure computation?&lt;br /&gt;
* Which parts perform I/O?&lt;br /&gt;
* Which parts can become separate functions?&lt;br /&gt;
* Can the central logic be tested independently?&lt;br /&gt;
&lt;br /&gt;
Refactor it if appropriate.&lt;br /&gt;
&lt;br /&gt;
== 79. Exercise 4 — Dependency review ==&lt;br /&gt;
&lt;br /&gt;
List all third-party Python packages used by the project.&lt;br /&gt;
&lt;br /&gt;
For each package, record:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Package&lt;br /&gt;
! Why it is needed&lt;br /&gt;
! Where it is used&lt;br /&gt;
|-&lt;br /&gt;
| example&lt;br /&gt;
| HTTP communication&lt;br /&gt;
| &amp;lt;code&amp;gt;client.py&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Remove unused dependencies.&lt;br /&gt;
&lt;br /&gt;
== 80. Exercise 5 — Global state review ==&lt;br /&gt;
&lt;br /&gt;
Search the project for mutable global variables.&lt;br /&gt;
&lt;br /&gt;
For every one, determine whether it should instead be:&lt;br /&gt;
&lt;br /&gt;
* a local variable;&lt;br /&gt;
* an object attribute;&lt;br /&gt;
* configuration;&lt;br /&gt;
* passed as a function parameter;&lt;br /&gt;
* intentionally constant.&lt;br /&gt;
&lt;br /&gt;
Refactor unnecessary global state.&lt;br /&gt;
&lt;br /&gt;
== 81. Exercise 6 — Error handling review ==&lt;br /&gt;
&lt;br /&gt;
Identify at least three realistic failure conditions for the project.&lt;br /&gt;
&lt;br /&gt;
For each one, document:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Failure:&lt;br /&gt;
Detection:&lt;br /&gt;
Exception / return behavior:&lt;br /&gt;
User-visible behavior:&lt;br /&gt;
Log behavior:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Implement at least one missing error path.&lt;br /&gt;
&lt;br /&gt;
== 82. Exercise 7 — Testability review ==&lt;br /&gt;
&lt;br /&gt;
Choose one component that is difficult to test.&lt;br /&gt;
&lt;br /&gt;
Identify why.&lt;br /&gt;
&lt;br /&gt;
Possible causes include:&lt;br /&gt;
&lt;br /&gt;
* direct network access;&lt;br /&gt;
* direct hardware access;&lt;br /&gt;
* global state;&lt;br /&gt;
* direct input();&lt;br /&gt;
* hard-coded paths;&lt;br /&gt;
* mixed I/O and computation.&lt;br /&gt;
&lt;br /&gt;
Refactor the component so its central logic can be tested without the external dependency.&lt;br /&gt;
&lt;br /&gt;
== 83. Exercise 8 — Git review ==&lt;br /&gt;
&lt;br /&gt;
Inspect:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git log --oneline --graph --decorate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Check:&lt;br /&gt;
&lt;br /&gt;
* commit messages;&lt;br /&gt;
* commit size;&lt;br /&gt;
* accidental generated files;&lt;br /&gt;
* secrets;&lt;br /&gt;
* temporary files;&lt;br /&gt;
* branch organization.&lt;br /&gt;
&lt;br /&gt;
Correct repository hygiene problems.&lt;br /&gt;
&lt;br /&gt;
== 84. Exercise 9 — README review ==&lt;br /&gt;
&lt;br /&gt;
The README should contain at least:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Project name&lt;br /&gt;
Short description&lt;br /&gt;
Requirements&lt;br /&gt;
Installation&lt;br /&gt;
Running the application&lt;br /&gt;
Running the tests&lt;br /&gt;
Project structure&lt;br /&gt;
Current limitations&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another student should be able to start the project using only the README.&lt;br /&gt;
&lt;br /&gt;
== 85. Exercise 10 — MVP demonstration ==&lt;br /&gt;
&lt;br /&gt;
Demonstrate the current MVP to another student or instructor.&lt;br /&gt;
&lt;br /&gt;
The demonstration must show:&lt;br /&gt;
&lt;br /&gt;
# application startup;&lt;br /&gt;
# one representative input;&lt;br /&gt;
# central project functionality;&lt;br /&gt;
# output;&lt;br /&gt;
# one error condition.&lt;br /&gt;
&lt;br /&gt;
Record any issue discovered during the demonstration and fix the highest-priority one.&lt;br /&gt;
&lt;br /&gt;
== 86. Project milestone submission ==&lt;br /&gt;
&lt;br /&gt;
At the end of Lab 5, the project repository should contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
project/&lt;br /&gt;
├── README.md&lt;br /&gt;
├── pyproject.toml&lt;br /&gt;
├── .gitignore&lt;br /&gt;
├── src/&lt;br /&gt;
│   └── project_name/&lt;br /&gt;
│       └── ...&lt;br /&gt;
└── tests/&lt;br /&gt;
    └── ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The repository should not contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.venv/&lt;br /&gt;
__pycache__/&lt;br /&gt;
*.pyc&lt;br /&gt;
passwords&lt;br /&gt;
API keys&lt;br /&gt;
large temporary files&lt;br /&gt;
IDE-specific build artifacts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
unless a specific file is intentionally required.&lt;br /&gt;
&lt;br /&gt;
== 87. Suggested instructor review ==&lt;br /&gt;
&lt;br /&gt;
For each team, the instructor can perform a short review based on:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Area&lt;br /&gt;
! Review question&lt;br /&gt;
|-&lt;br /&gt;
| architecture&lt;br /&gt;
| Can the team explain the main components?&lt;br /&gt;
|-&lt;br /&gt;
| MVP&lt;br /&gt;
| Does the central workflow actually run?&lt;br /&gt;
|-&lt;br /&gt;
| Python&lt;br /&gt;
| Is the code written idiomatically?&lt;br /&gt;
|-&lt;br /&gt;
| coupling&lt;br /&gt;
| Are components unnecessarily dependent on each other?&lt;br /&gt;
|-&lt;br /&gt;
| testability&lt;br /&gt;
| Can important logic be tested independently?&lt;br /&gt;
|-&lt;br /&gt;
| robustness&lt;br /&gt;
| Are expected errors handled?&lt;br /&gt;
|-&lt;br /&gt;
| Git&lt;br /&gt;
| Is the development history meaningful?&lt;br /&gt;
|-&lt;br /&gt;
| documentation&lt;br /&gt;
| Can another developer run the project?&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 88. C/C++ habits to reconsider ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ habit&lt;br /&gt;
! Typical Python project approach&lt;br /&gt;
|-&lt;br /&gt;
| create classes for every operation&lt;br /&gt;
| use functions for stateless behavior&lt;br /&gt;
|-&lt;br /&gt;
| create header/source pairs&lt;br /&gt;
| organize behavior into modules&lt;br /&gt;
|-&lt;br /&gt;
| hide every field behind getters&lt;br /&gt;
| expose simple state directly when appropriate&lt;br /&gt;
|-&lt;br /&gt;
| use globals for shared application state&lt;br /&gt;
| pass dependencies explicitly&lt;br /&gt;
|-&lt;br /&gt;
| create deep inheritance hierarchies&lt;br /&gt;
| prefer simple composition where possible&lt;br /&gt;
|-&lt;br /&gt;
| tightly couple code to concrete implementations&lt;br /&gt;
| inject replaceable dependencies when useful&lt;br /&gt;
|-&lt;br /&gt;
| depend on compiler diagnostics alone&lt;br /&gt;
| use tests, linting and type checking&lt;br /&gt;
|-&lt;br /&gt;
| large final integration&lt;br /&gt;
| maintain a continuously working MVP&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 89. Summary ==&lt;br /&gt;
&lt;br /&gt;
The main goal of this laboratory is not to learn another Python syntax feature.&lt;br /&gt;
&lt;br /&gt;
It is to improve the structure of the semester project.&lt;br /&gt;
&lt;br /&gt;
A healthy project should increasingly have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
clear modules&lt;br /&gt;
     ↓&lt;br /&gt;
clear responsibilities&lt;br /&gt;
     ↓&lt;br /&gt;
explicit dependencies&lt;br /&gt;
     ↓&lt;br /&gt;
testable logic&lt;br /&gt;
     ↓&lt;br /&gt;
controlled configuration&lt;br /&gt;
     ↓&lt;br /&gt;
meaningful Git history&lt;br /&gt;
     ↓&lt;br /&gt;
working MVP&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The central idea of this laboratory is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
A successful software project is not only a collection of working features. Its structure should make those features understandable, testable and safe to change.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 90. Preparation for Lab 6 ==&lt;br /&gt;
&lt;br /&gt;
Before the next laboratory:&lt;br /&gt;
&lt;br /&gt;
# ensure the MVP works end-to-end;&lt;br /&gt;
# resolve the highest-priority architecture issues identified during review;&lt;br /&gt;
# remove unnecessary global state;&lt;br /&gt;
# improve error handling;&lt;br /&gt;
# make central logic independently testable;&lt;br /&gt;
# ensure dependencies are documented;&lt;br /&gt;
# update the README;&lt;br /&gt;
# ensure tests and linting pass;&lt;br /&gt;
# create a short list of remaining features and known issues.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Lab 6&amp;#039;&amp;#039;&amp;#039;, the focus will be on completing the implementation, expanding tests, refactoring remaining technical debt, improving documentation and preparing a release candidate.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_4&amp;diff=8373</id>
		<title>Lab 4</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_4&amp;diff=8373"/>
		<updated>2026-09-14T06:15:53Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: Pagină nouă: = Lab 4 — Testing, Debugging and Software Quality =  == Objectives ==  This laboratory introduces software testing and quality practices for Python projects.  Students already kn...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 4 — Testing, Debugging and Software Quality =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
This laboratory introduces software testing and quality practices for Python projects.&lt;br /&gt;
&lt;br /&gt;
Students already know how to write C/C++ programs and have seen assertions, debugging and compiler diagnostics. The objective of this laboratory is to understand how these practices are applied in Python and how automated tests become part of the normal development workflow.&lt;br /&gt;
&lt;br /&gt;
After completing this laboratory, you should be able to:&lt;br /&gt;
&lt;br /&gt;
* understand the role of automated tests;&lt;br /&gt;
* distinguish between unit, integration and system tests;&lt;br /&gt;
* write tests using &amp;lt;code&amp;gt;pytest&amp;lt;/code&amp;gt;;&lt;br /&gt;
* use &amp;lt;code&amp;gt;assert&amp;lt;/code&amp;gt; statements effectively;&lt;br /&gt;
* test expected exceptions;&lt;br /&gt;
* organize tests in a dedicated &amp;lt;code&amp;gt;tests/&amp;lt;/code&amp;gt; directory;&lt;br /&gt;
* use fixtures for reusable test setup;&lt;br /&gt;
* use parametrized tests;&lt;br /&gt;
* test file-based code using temporary directories;&lt;br /&gt;
* understand basic mocking;&lt;br /&gt;
* use logging instead of uncontrolled &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; debugging;&lt;br /&gt;
* use the Python debugger;&lt;br /&gt;
* run static analysis and formatting tools;&lt;br /&gt;
* use type checking as part of code-quality verification;&lt;br /&gt;
* understand test coverage;&lt;br /&gt;
* integrate linting and tests into a simple development workflow;&lt;br /&gt;
* prepare the semester project for continuous integration.&lt;br /&gt;
&lt;br /&gt;
Throughout this laboratory, Python practices are compared with familiar C/C++ testing and debugging concepts whenever appropriate.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== 1. Why automated testing? ==&lt;br /&gt;
&lt;br /&gt;
A program that runs correctly once is not necessarily correct.&lt;br /&gt;
&lt;br /&gt;
Automated tests allow us to repeatedly verify that known behavior still works after the code changes.&lt;br /&gt;
&lt;br /&gt;
A typical development cycle becomes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
write code&lt;br /&gt;
    ↓&lt;br /&gt;
run tests&lt;br /&gt;
    ↓&lt;br /&gt;
change code&lt;br /&gt;
    ↓&lt;br /&gt;
run tests again&lt;br /&gt;
    ↓&lt;br /&gt;
refactor&lt;br /&gt;
    ↓&lt;br /&gt;
run tests again&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is particularly important in larger projects where changing one component may accidentally break another.&lt;br /&gt;
&lt;br /&gt;
== 2. Manual testing versus automated testing ==&lt;br /&gt;
&lt;br /&gt;
Consider a function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def add(a, b):&lt;br /&gt;
    return a + b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Manual testing might look like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(add(2, 3))&lt;br /&gt;
print(add(-1, 1))&lt;br /&gt;
print(add(100, 200))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The programmer must inspect the output manually.&lt;br /&gt;
&lt;br /&gt;
An automated test describes the expected result:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_add():&lt;br /&gt;
    assert add(2, 3) == 5&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the behavior changes incorrectly, the test fails automatically.&lt;br /&gt;
&lt;br /&gt;
== 3. Assertions ==&lt;br /&gt;
&lt;br /&gt;
C/C++ and Python both provide assertions.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;cassert&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int result = add(2, 3);&lt;br /&gt;
&lt;br /&gt;
assert(result == 5);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
result = add(2, 3)&lt;br /&gt;
&lt;br /&gt;
assert result == 5&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
An assertion expresses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
This condition must be true at this point in the program.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 4. Assertions are not input validation ==&lt;br /&gt;
&lt;br /&gt;
Assertions should not normally be used to validate user input or external data.&lt;br /&gt;
&lt;br /&gt;
For example, this is not a good validation strategy:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def set_grade(grade):&lt;br /&gt;
    assert 1 &amp;lt;= grade &amp;lt;= 10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions may be disabled in optimized Python execution.&lt;br /&gt;
&lt;br /&gt;
For input validation, use explicit exceptions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def set_grade(grade):&lt;br /&gt;
    if not 1 &amp;lt;= grade &amp;lt;= 10:&lt;br /&gt;
        raise ValueError(&lt;br /&gt;
            &amp;quot;grade must be between 1 and 10&amp;quot;&lt;br /&gt;
        )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are useful for:&lt;br /&gt;
&lt;br /&gt;
* tests;&lt;br /&gt;
* internal invariants;&lt;br /&gt;
* assumptions that indicate programmer errors when violated.&lt;br /&gt;
&lt;br /&gt;
== 5. Test levels ==&lt;br /&gt;
&lt;br /&gt;
A useful simplified classification is:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Test level&lt;br /&gt;
! Purpose&lt;br /&gt;
! Example&lt;br /&gt;
|-&lt;br /&gt;
| unit test&lt;br /&gt;
| tests one small unit of logic&lt;br /&gt;
| test one function&lt;br /&gt;
|-&lt;br /&gt;
| integration test&lt;br /&gt;
| tests interaction between components&lt;br /&gt;
| storage + service layer&lt;br /&gt;
|-&lt;br /&gt;
| system test&lt;br /&gt;
| tests a complete application workflow&lt;br /&gt;
| run application with realistic input&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For student projects, most automated tests should be unit tests, with several integration tests for important workflows.&lt;br /&gt;
&lt;br /&gt;
== 6. pytest ==&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s standard library contains &amp;lt;code&amp;gt;unittest&amp;lt;/code&amp;gt;, but this laboratory uses &amp;lt;code&amp;gt;pytest&amp;lt;/code&amp;gt; because it provides a concise and widely used testing style.&lt;br /&gt;
&lt;br /&gt;
Install it inside the project&amp;#039;s virtual environment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m pip install pytest&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run all tests:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pytest&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A more verbose run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pytest -v&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 7. A first pytest test ==&lt;br /&gt;
&lt;br /&gt;
Suppose:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# calculator.py&lt;br /&gt;
&lt;br /&gt;
def add(a, b):&lt;br /&gt;
    return a + b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
tests/&lt;br /&gt;
└── test_calculator.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from calculator import add&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_add():&lt;br /&gt;
    assert add(2, 3) == 5&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pytest&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pytest discovers functions whose names begin with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
test_&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 8. C++ test framework versus pytest ==&lt;br /&gt;
&lt;br /&gt;
The overall structure is similar to a C++ testing framework such as GoogleTest.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++ / GoogleTest&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python / pytest&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
TEST(CalculatorTest, Add)&lt;br /&gt;
{&lt;br /&gt;
    EXPECT_EQ(add(2, 3), 5);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_add():&lt;br /&gt;
    assert add(2, 3) == 5&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
pytest uses normal Python &amp;lt;code&amp;gt;assert&amp;lt;/code&amp;gt; statements rather than a large family of assertion macros.&lt;br /&gt;
&lt;br /&gt;
== 9. pytest assertion introspection ==&lt;br /&gt;
&lt;br /&gt;
Suppose:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_add():&lt;br /&gt;
    assert add(2, 3) == 6&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pytest can display the values involved in the failed expression.&lt;br /&gt;
&lt;br /&gt;
This means tests can remain simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
assert actual == expected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
rather than requiring specialized assertion methods.&lt;br /&gt;
&lt;br /&gt;
== 10. Naming tests ==&lt;br /&gt;
&lt;br /&gt;
Test names should describe behavior.&lt;br /&gt;
&lt;br /&gt;
Prefer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_average_returns_mean_of_values():&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
over:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test1():&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A useful naming style is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
test_&amp;lt;behavior&amp;gt;_&amp;lt;condition&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_withdraw_rejects_negative_amount():&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 11. Arrange, Act, Assert ==&lt;br /&gt;
&lt;br /&gt;
A useful test structure is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Arrange&lt;br /&gt;
Act&lt;br /&gt;
Assert&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_bank_account_deposit():&lt;br /&gt;
    # Arrange&lt;br /&gt;
    account = BankAccount(&amp;quot;Alice&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    # Act&lt;br /&gt;
    account.deposit(100)&lt;br /&gt;
&lt;br /&gt;
    # Assert&lt;br /&gt;
    assert account.balance == 100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is not mandatory syntax. It is a way to keep tests readable.&lt;br /&gt;
&lt;br /&gt;
== 12. Testing multiple conditions ==&lt;br /&gt;
&lt;br /&gt;
A test can contain more than one assertion when they describe the same behavior.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_new_student():&lt;br /&gt;
    student = Student(&lt;br /&gt;
        name=&amp;quot;Alice&amp;quot;,&lt;br /&gt;
        grade=9.5,&lt;br /&gt;
    )&lt;br /&gt;
&lt;br /&gt;
    assert student.name == &amp;quot;Alice&amp;quot;&lt;br /&gt;
    assert student.grade == 9.5&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, unrelated behaviors should generally be tested separately.&lt;br /&gt;
&lt;br /&gt;
== 13. Testing floating-point values ==&lt;br /&gt;
&lt;br /&gt;
Direct floating-point equality can be unreliable.&lt;br /&gt;
&lt;br /&gt;
C++ testing frameworks commonly provide approximate comparison helpers.&lt;br /&gt;
&lt;br /&gt;
pytest provides &amp;lt;code&amp;gt;pytest.approx()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++ / GoogleTest&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python / pytest&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
EXPECT_NEAR(&lt;br /&gt;
    result,&lt;br /&gt;
    expected,&lt;br /&gt;
    1e-6&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import pytest&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
assert result == pytest.approx(&lt;br /&gt;
    expected&lt;br /&gt;
)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_average():&lt;br /&gt;
    result = average([1.0, 2.0, 3.0])&lt;br /&gt;
&lt;br /&gt;
    assert result == pytest.approx(2.0)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 14. Testing exceptions ==&lt;br /&gt;
&lt;br /&gt;
Suppose:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def divide(a, b):&lt;br /&gt;
    if b == 0:&lt;br /&gt;
        raise ValueError(&lt;br /&gt;
            &amp;quot;division by zero&amp;quot;&lt;br /&gt;
        )&lt;br /&gt;
&lt;br /&gt;
    return a / b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pytest can verify that an exception is raised:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import pytest&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_divide_rejects_zero():&lt;br /&gt;
    with pytest.raises(ValueError):&lt;br /&gt;
        divide(10, 0)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 15. Testing exception messages ==&lt;br /&gt;
&lt;br /&gt;
The exception object can also be inspected.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_divide_error_message():&lt;br /&gt;
    with pytest.raises(ValueError) as exc_info:&lt;br /&gt;
        divide(10, 0)&lt;br /&gt;
&lt;br /&gt;
    assert &amp;quot;division by zero&amp;quot; in str(&lt;br /&gt;
        exc_info.value&lt;br /&gt;
    )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Only test exact error messages when the wording itself is part of the required interface.&lt;br /&gt;
&lt;br /&gt;
== 16. C++ exception testing versus pytest ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++ / GoogleTest&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python / pytest&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
EXPECT_THROW(&lt;br /&gt;
    divide(10, 0),&lt;br /&gt;
    std::invalid_argument&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
with pytest.raises(ValueError):&lt;br /&gt;
    divide(10, 0)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 17. Test organization ==&lt;br /&gt;
&lt;br /&gt;
A useful project layout is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
project/&lt;br /&gt;
├── pyproject.toml&lt;br /&gt;
├── src/&lt;br /&gt;
│   └── project/&lt;br /&gt;
│       ├── __init__.py&lt;br /&gt;
│       ├── models.py&lt;br /&gt;
│       └── services.py&lt;br /&gt;
└── tests/&lt;br /&gt;
    ├── test_models.py&lt;br /&gt;
    └── test_services.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test files should generally mirror the logical organization of the source code.&lt;br /&gt;
&lt;br /&gt;
== 18. One behavior per test ==&lt;br /&gt;
&lt;br /&gt;
Avoid tests that try to verify the entire application at once.&lt;br /&gt;
&lt;br /&gt;
Less useful:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_everything():&lt;br /&gt;
    # Create students.&lt;br /&gt;
    # Save file.&lt;br /&gt;
    # Load file.&lt;br /&gt;
    # Calculate average.&lt;br /&gt;
    # Modify student.&lt;br /&gt;
    # Delete file.&lt;br /&gt;
    # Check all values.&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prefer focused tests:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_student_creation():&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_save_students():&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_load_students():&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_average():&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Focused tests make failures easier to diagnose.&lt;br /&gt;
&lt;br /&gt;
== 19. Fixtures ==&lt;br /&gt;
&lt;br /&gt;
A fixture provides reusable setup for tests.&lt;br /&gt;
&lt;br /&gt;
Suppose many tests need the same student list.&lt;br /&gt;
&lt;br /&gt;
Without a fixture:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_average():&lt;br /&gt;
    students = [&lt;br /&gt;
        Student(&amp;quot;Alice&amp;quot;, 9.0),&lt;br /&gt;
        Student(&amp;quot;Bob&amp;quot;, 7.0),&lt;br /&gt;
    ]&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_highest_grade():&lt;br /&gt;
    students = [&lt;br /&gt;
        Student(&amp;quot;Alice&amp;quot;, 9.0),&lt;br /&gt;
        Student(&amp;quot;Bob&amp;quot;, 7.0),&lt;br /&gt;
    ]&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With a fixture:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import pytest&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@pytest.fixture&lt;br /&gt;
def students():&lt;br /&gt;
    return [&lt;br /&gt;
        Student(&amp;quot;Alice&amp;quot;, 9.0),&lt;br /&gt;
        Student(&amp;quot;Bob&amp;quot;, 7.0),&lt;br /&gt;
    ]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 20. Using fixtures ==&lt;br /&gt;
&lt;br /&gt;
A test requests a fixture by using its name as a parameter.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_average(students):&lt;br /&gt;
    assert average(students) == 8.0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_highest_grade(students):&lt;br /&gt;
    student = highest_grade(students)&lt;br /&gt;
&lt;br /&gt;
    assert student.name == &amp;quot;Alice&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pytest resolves the fixture automatically.&lt;br /&gt;
&lt;br /&gt;
== 21. Fixture setup and cleanup ==&lt;br /&gt;
&lt;br /&gt;
Fixtures can perform both setup and cleanup.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import pytest&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@pytest.fixture&lt;br /&gt;
def resource():&lt;br /&gt;
    resource = create_resource()&lt;br /&gt;
&lt;br /&gt;
    yield resource&lt;br /&gt;
&lt;br /&gt;
    resource.close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Everything before &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is setup.&lt;br /&gt;
&lt;br /&gt;
Everything after &amp;lt;code&amp;gt;yield&amp;lt;/code&amp;gt; is cleanup.&lt;br /&gt;
&lt;br /&gt;
This is conceptually similar to structured resource setup and teardown in C++ tests.&lt;br /&gt;
&lt;br /&gt;
== 22. Fixture scope ==&lt;br /&gt;
&lt;br /&gt;
Fixtures can have different scopes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
@pytest.fixture(scope=&amp;quot;function&amp;quot;)&lt;br /&gt;
def item():&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Common scopes include:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Scope&lt;br /&gt;
! Lifetime&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;function&amp;lt;/code&amp;gt;&lt;br /&gt;
| one test function&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;class&amp;lt;/code&amp;gt;&lt;br /&gt;
| one test class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;module&amp;lt;/code&amp;gt;&lt;br /&gt;
| one test module&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;session&amp;lt;/code&amp;gt;&lt;br /&gt;
| entire pytest run&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Use the smallest appropriate scope.&lt;br /&gt;
&lt;br /&gt;
== 23. Parametrized tests ==&lt;br /&gt;
&lt;br /&gt;
Suppose the same behavior should be tested for many inputs.&lt;br /&gt;
&lt;br /&gt;
Without parametrization:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_square_2():&lt;br /&gt;
    assert square(2) == 4&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_square_3():&lt;br /&gt;
    assert square(3) == 9&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_square_4():&lt;br /&gt;
    assert square(4) == 16&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With pytest parametrization:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import pytest&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@pytest.mark.parametrize(&lt;br /&gt;
    &amp;quot;value, expected&amp;quot;,&lt;br /&gt;
    [&lt;br /&gt;
        (2, 4),&lt;br /&gt;
        (3, 9),&lt;br /&gt;
        (4, 16),&lt;br /&gt;
    ],&lt;br /&gt;
)&lt;br /&gt;
def test_square(value, expected):&lt;br /&gt;
    assert square(value) == expected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 24. Parametrization versus repeated C++ tests ==&lt;br /&gt;
&lt;br /&gt;
Parameterized tests exist in C++ frameworks as well.&lt;br /&gt;
&lt;br /&gt;
The conceptual mapping is:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++ test framework&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | pytest&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
define test cases&lt;br /&gt;
instantiate parameter set&lt;br /&gt;
run same test logic&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
@pytest.mark.parametrize(&lt;br /&gt;
    &amp;quot;input_value, expected&amp;quot;,&lt;br /&gt;
    [&lt;br /&gt;
        (..., ...),&lt;br /&gt;
        (..., ...),&lt;br /&gt;
    ],&lt;br /&gt;
)&lt;br /&gt;
def test_behavior(&lt;br /&gt;
    input_value,&lt;br /&gt;
    expected,&lt;br /&gt;
):&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 25. Parametrizing invalid inputs ==&lt;br /&gt;
&lt;br /&gt;
Parametrization is also useful for validating input rules.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
@pytest.mark.parametrize(&lt;br /&gt;
    &amp;quot;grade&amp;quot;,&lt;br /&gt;
    [&lt;br /&gt;
        -1,&lt;br /&gt;
        0,&lt;br /&gt;
        10.1,&lt;br /&gt;
        100,&lt;br /&gt;
    ],&lt;br /&gt;
)&lt;br /&gt;
def test_invalid_grades(grade):&lt;br /&gt;
    with pytest.raises(ValueError):&lt;br /&gt;
        Student(&lt;br /&gt;
            name=&amp;quot;Alice&amp;quot;,&lt;br /&gt;
            grade=grade,&lt;br /&gt;
        )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 26. Testing dataclasses ==&lt;br /&gt;
&lt;br /&gt;
Dataclasses often make tests concise because they provide value-based equality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from dataclasses import dataclass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@dataclass&lt;br /&gt;
class Student:&lt;br /&gt;
    name: str&lt;br /&gt;
    grade: float&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_student_equality():&lt;br /&gt;
    a = Student(&amp;quot;Alice&amp;quot;, 9.0)&lt;br /&gt;
    b = Student(&amp;quot;Alice&amp;quot;, 9.0)&lt;br /&gt;
&lt;br /&gt;
    assert a == b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 27. Testing collections ==&lt;br /&gt;
&lt;br /&gt;
pytest assertions work naturally with lists, dictionaries, sets and tuples.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_filter_passed_students():&lt;br /&gt;
    result = filter_passed_students(&lt;br /&gt;
        [&lt;br /&gt;
            Student(&amp;quot;Alice&amp;quot;, 9.0),&lt;br /&gt;
            Student(&amp;quot;Bob&amp;quot;, 4.0),&lt;br /&gt;
        ]&lt;br /&gt;
    )&lt;br /&gt;
&lt;br /&gt;
    assert result == [&lt;br /&gt;
        Student(&amp;quot;Alice&amp;quot;, 9.0),&lt;br /&gt;
    ]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For sets:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
assert result == {&amp;quot;Alice&amp;quot;, &amp;quot;Carol&amp;quot;}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 28. Testing file-based code ==&lt;br /&gt;
&lt;br /&gt;
Tests should not normally depend on manually created files in arbitrary locations.&lt;br /&gt;
&lt;br /&gt;
pytest provides temporary directories through the built-in &amp;lt;code&amp;gt;tmp_path&amp;lt;/code&amp;gt; fixture.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_save_file(tmp_path):&lt;br /&gt;
    path = tmp_path / &amp;quot;output.txt&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    save_message(&lt;br /&gt;
        path,&lt;br /&gt;
        &amp;quot;hello&amp;quot;,&lt;br /&gt;
    )&lt;br /&gt;
&lt;br /&gt;
    assert path.read_text(&lt;br /&gt;
        encoding=&amp;quot;utf-8&amp;quot;&lt;br /&gt;
    ) == &amp;quot;hello&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The temporary directory is managed automatically by pytest.&lt;br /&gt;
&lt;br /&gt;
== 29. Testing JSON storage ==&lt;br /&gt;
&lt;br /&gt;
Suppose:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def save_students(path, students):&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A test can create a temporary file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_save_and_load_students(tmp_path):&lt;br /&gt;
    path = tmp_path / &amp;quot;students.json&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    original = [&lt;br /&gt;
        Student(&amp;quot;Alice&amp;quot;, 9.0),&lt;br /&gt;
        Student(&amp;quot;Bob&amp;quot;, 8.0),&lt;br /&gt;
    ]&lt;br /&gt;
&lt;br /&gt;
    save_students(path, original)&lt;br /&gt;
&lt;br /&gt;
    loaded = load_students(path)&lt;br /&gt;
&lt;br /&gt;
    assert loaded == original&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is an integration test because it verifies multiple storage operations together.&lt;br /&gt;
&lt;br /&gt;
== 30. Testing missing files ==&lt;br /&gt;
&lt;br /&gt;
Suppose &amp;lt;code&amp;gt;load_students()&amp;lt;/code&amp;gt; should return an empty list when a file does not exist.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_missing_file_returns_empty_list(&lt;br /&gt;
    tmp_path,&lt;br /&gt;
):&lt;br /&gt;
    path = tmp_path / &amp;quot;missing.json&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    result = load_students(path)&lt;br /&gt;
&lt;br /&gt;
    assert result == []&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Tests should explicitly document such expected behavior.&lt;br /&gt;
&lt;br /&gt;
== 31. Testing malformed input ==&lt;br /&gt;
&lt;br /&gt;
Suppose malformed JSON should raise an exception.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import json&lt;br /&gt;
import pytest&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_invalid_json(tmp_path):&lt;br /&gt;
    path = tmp_path / &amp;quot;students.json&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    path.write_text(&lt;br /&gt;
        &amp;quot;{invalid json&amp;quot;,&lt;br /&gt;
        encoding=&amp;quot;utf-8&amp;quot;,&lt;br /&gt;
    )&lt;br /&gt;
&lt;br /&gt;
    with pytest.raises(&lt;br /&gt;
        json.JSONDecodeError&lt;br /&gt;
    ):&lt;br /&gt;
        load_students(path)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 32. Unit tests should minimize external dependencies ==&lt;br /&gt;
&lt;br /&gt;
A unit test should ideally test one unit without requiring:&lt;br /&gt;
&lt;br /&gt;
* internet access;&lt;br /&gt;
* a real database;&lt;br /&gt;
* external hardware;&lt;br /&gt;
* arbitrary filesystem state;&lt;br /&gt;
* real user input;&lt;br /&gt;
* another service.&lt;br /&gt;
&lt;br /&gt;
Dependencies that are slow or uncontrollable should often be replaced with controlled test substitutes.&lt;br /&gt;
&lt;br /&gt;
This leads to the concept of mocking.&lt;br /&gt;
&lt;br /&gt;
== 33. What is mocking? ==&lt;br /&gt;
&lt;br /&gt;
A mock replaces a dependency with a controlled object during a test.&lt;br /&gt;
&lt;br /&gt;
Suppose:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def get_temperature(api_client):&lt;br /&gt;
    response = api_client.fetch()&lt;br /&gt;
    return response[&amp;quot;temperature&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The test does not need to access a real remote API.&lt;br /&gt;
&lt;br /&gt;
A controlled fake object can be used instead.&lt;br /&gt;
&lt;br /&gt;
== 34. A simple fake object ==&lt;br /&gt;
&lt;br /&gt;
Before using a mocking framework, a simple fake class is often enough.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class FakeApiClient:&lt;br /&gt;
    def fetch(self):&lt;br /&gt;
        return {&lt;br /&gt;
            &amp;quot;temperature&amp;quot;: 23.5,&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_get_temperature():&lt;br /&gt;
    client = FakeApiClient()&lt;br /&gt;
&lt;br /&gt;
    result = get_temperature(client)&lt;br /&gt;
&lt;br /&gt;
    assert result == 23.5&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is often clearer than complex mocking.&lt;br /&gt;
&lt;br /&gt;
== 35. unittest.mock ==&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s standard library provides &amp;lt;code&amp;gt;unittest.mock&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from unittest.mock import Mock&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_get_temperature():&lt;br /&gt;
    client = Mock()&lt;br /&gt;
&lt;br /&gt;
    client.fetch.return_value = {&lt;br /&gt;
        &amp;quot;temperature&amp;quot;: 23.5,&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    result = get_temperature(client)&lt;br /&gt;
&lt;br /&gt;
    assert result == 23.5&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 36. Verifying calls on a mock ==&lt;br /&gt;
&lt;br /&gt;
Mocks can verify interactions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from unittest.mock import Mock&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_temperature_fetches_once():&lt;br /&gt;
    client = Mock()&lt;br /&gt;
&lt;br /&gt;
    client.fetch.return_value = {&lt;br /&gt;
        &amp;quot;temperature&amp;quot;: 23.5,&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    get_temperature(client)&lt;br /&gt;
&lt;br /&gt;
    client.fetch.assert_called_once()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use interaction assertions only when the interaction itself is important behavior.&lt;br /&gt;
&lt;br /&gt;
== 37. Do not overuse mocks ==&lt;br /&gt;
&lt;br /&gt;
A test that mocks every object can become tightly coupled to implementation details.&lt;br /&gt;
&lt;br /&gt;
Prefer:&lt;br /&gt;
&lt;br /&gt;
* real simple objects;&lt;br /&gt;
* small fake implementations;&lt;br /&gt;
* temporary files;&lt;br /&gt;
* dependency injection;&lt;br /&gt;
&lt;br /&gt;
before introducing complex mocks.&lt;br /&gt;
&lt;br /&gt;
Mock external or expensive dependencies when doing so makes the test more reliable and focused.&lt;br /&gt;
&lt;br /&gt;
== 38. Dependency injection ==&lt;br /&gt;
&lt;br /&gt;
Code is easier to test when dependencies are supplied from outside.&lt;br /&gt;
&lt;br /&gt;
Less testable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def load_remote_data():&lt;br /&gt;
    client = ApiClient()&lt;br /&gt;
    return client.fetch()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More testable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def load_remote_data(client):&lt;br /&gt;
    return client.fetch()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second version allows tests to provide a fake or mock client.&lt;br /&gt;
&lt;br /&gt;
This principle is not specific to Python.&lt;br /&gt;
&lt;br /&gt;
== 39. Debugging with print() ==&lt;br /&gt;
&lt;br /&gt;
Temporary print debugging is common:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(&amp;quot;value =&amp;quot;, value)&lt;br /&gt;
print(&amp;quot;students =&amp;quot;, students)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be useful for quick inspection, but uncontrolled &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; statements should not remain throughout production code.&lt;br /&gt;
&lt;br /&gt;
For structured diagnostics, use logging.&lt;br /&gt;
&lt;br /&gt;
== 40. The logging module ==&lt;br /&gt;
&lt;br /&gt;
Python provides the standard &amp;lt;code&amp;gt;logging&amp;lt;/code&amp;gt; module.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import logging&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
logging.basicConfig(&lt;br /&gt;
    level=logging.INFO&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
logging.info(&amp;quot;Application started&amp;quot;)&lt;br /&gt;
logging.warning(&amp;quot;Configuration missing&amp;quot;)&lt;br /&gt;
logging.error(&amp;quot;Cannot load file&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 41. Logging levels ==&lt;br /&gt;
&lt;br /&gt;
Common logging levels are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Level&lt;br /&gt;
! Typical use&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt;&lt;br /&gt;
| detailed diagnostic information&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;INFO&amp;lt;/code&amp;gt;&lt;br /&gt;
| normal application events&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;WARNING&amp;lt;/code&amp;gt;&lt;br /&gt;
| unexpected condition that does not stop execution&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;ERROR&amp;lt;/code&amp;gt;&lt;br /&gt;
| operation failed&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;CRITICAL&amp;lt;/code&amp;gt;&lt;br /&gt;
| severe failure&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 42. C++ logging idea versus Python logging ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++ idea&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// Using a logging library:&lt;br /&gt;
//&lt;br /&gt;
// logger.info(&amp;quot;Started&amp;quot;);&lt;br /&gt;
// logger.error(&amp;quot;Failed&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
logger.info(&amp;quot;Started&amp;quot;)&lt;br /&gt;
logger.error(&amp;quot;Failed&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The main principle is the same: diagnostic output should have severity levels and configurable destinations.&lt;br /&gt;
&lt;br /&gt;
== 43. Module-level loggers ==&lt;br /&gt;
&lt;br /&gt;
A common pattern is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import logging&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
logger = logging.getLogger(__name__)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def load_data():&lt;br /&gt;
    logger.debug(&amp;quot;Loading data&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using &amp;lt;code&amp;gt;__name__&amp;lt;/code&amp;gt; identifies the module that produced the log message.&lt;br /&gt;
&lt;br /&gt;
== 44. Logging exceptions ==&lt;br /&gt;
&lt;br /&gt;
Inside an exception handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    load_configuration()&lt;br /&gt;
except OSError:&lt;br /&gt;
    logger.exception(&lt;br /&gt;
        &amp;quot;Failed to load configuration&amp;quot;&lt;br /&gt;
    )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;logger.exception()&amp;lt;/code&amp;gt; includes traceback information when used inside an exception handler.&lt;br /&gt;
&lt;br /&gt;
== 45. The Python debugger ==&lt;br /&gt;
&lt;br /&gt;
Python includes a debugger module called &amp;lt;code&amp;gt;pdb&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A breakpoint can be inserted with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
breakpoint()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def calculate(values):&lt;br /&gt;
    total = sum(values)&lt;br /&gt;
&lt;br /&gt;
    breakpoint()&lt;br /&gt;
&lt;br /&gt;
    return total / len(values)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When execution reaches the breakpoint, the program enters an interactive debugger.&lt;br /&gt;
&lt;br /&gt;
== 46. Useful debugger commands ==&lt;br /&gt;
&lt;br /&gt;
Common commands include:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Command&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;&lt;br /&gt;
| execute next line&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;&lt;br /&gt;
| step into function&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt;&lt;br /&gt;
| continue execution&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;p expression&amp;lt;/code&amp;gt;&lt;br /&gt;
| print expression&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;pp expression&amp;lt;/code&amp;gt;&lt;br /&gt;
| pretty-print expression&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;&lt;br /&gt;
| list source code&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;q&amp;lt;/code&amp;gt;&lt;br /&gt;
| quit debugger&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Modern IDEs such as VS Code and PyCharm also provide graphical debugger interfaces.&lt;br /&gt;
&lt;br /&gt;
== 47. Debugging a failing pytest test ==&lt;br /&gt;
&lt;br /&gt;
pytest can stop in the debugger when a test fails:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pytest --pdb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or a specific test can be executed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pytest -v tests/test_services.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A specific test function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pytest -v \&lt;br /&gt;
    tests/test_services.py::test_average&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 48. Static analysis ==&lt;br /&gt;
&lt;br /&gt;
Tests verify behavior for selected inputs.&lt;br /&gt;
&lt;br /&gt;
Static analysis examines source code without executing it.&lt;br /&gt;
&lt;br /&gt;
Useful tools can detect:&lt;br /&gt;
&lt;br /&gt;
* unused imports;&lt;br /&gt;
* undefined names;&lt;br /&gt;
* suspicious constructs;&lt;br /&gt;
* style problems;&lt;br /&gt;
* some type errors.&lt;br /&gt;
&lt;br /&gt;
These tools complement tests rather than replace them.&lt;br /&gt;
&lt;br /&gt;
== 49. Ruff ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ruff&amp;lt;/code&amp;gt; is a fast Python linter and formatter.&lt;br /&gt;
&lt;br /&gt;
Install:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m pip install ruff&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Check the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ruff check .&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Automatically fix supported issues:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ruff check . --fix&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Format code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ruff format .&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 50. Example lint issue ==&lt;br /&gt;
&lt;br /&gt;
Consider:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import json&lt;br /&gt;
import os&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def add(a, b):&lt;br /&gt;
    return a + b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;os&amp;lt;/code&amp;gt; is unused, a linter can report it.&lt;br /&gt;
&lt;br /&gt;
This is similar to enabling compiler warnings in C/C++.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
g++ -Wall -Wextra ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ruff check .&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 51. Compiler warnings versus linting ==&lt;br /&gt;
&lt;br /&gt;
C/C++ compilers naturally perform significant static checking because the language is statically typed.&lt;br /&gt;
&lt;br /&gt;
Python requires separate tools for many similar checks.&lt;br /&gt;
&lt;br /&gt;
A useful mental mapping is:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ workflow&lt;br /&gt;
! Python workflow&lt;br /&gt;
|-&lt;br /&gt;
| compiler diagnostics&lt;br /&gt;
| interpreter + linter&lt;br /&gt;
|-&lt;br /&gt;
| type checking by compiler&lt;br /&gt;
| optional static type checker&lt;br /&gt;
|-&lt;br /&gt;
| formatter such as clang-format&lt;br /&gt;
| formatter such as Ruff&lt;br /&gt;
|-&lt;br /&gt;
| GoogleTest / Catch2&lt;br /&gt;
| pytest&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 52. Type checking with mypy ==&lt;br /&gt;
&lt;br /&gt;
Install:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m pip install mypy&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mypy src&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def add(a: int, b: int) -&amp;gt; int:&lt;br /&gt;
    return a + b&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
result = add(&amp;quot;hello&amp;quot;, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A static type checker can detect the incompatible argument.&lt;br /&gt;
&lt;br /&gt;
== 53. Tests and type checking solve different problems ==&lt;br /&gt;
&lt;br /&gt;
Consider:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def divide(a: float, b: float) -&amp;gt; float:&lt;br /&gt;
    return a / b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A type checker can detect:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
divide(&amp;quot;hello&amp;quot;, 2)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
but it does not prove that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
divide(10, 0)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is safe.&lt;br /&gt;
&lt;br /&gt;
Tests and runtime validation are still necessary.&lt;br /&gt;
&lt;br /&gt;
== 54. Code formatting ==&lt;br /&gt;
&lt;br /&gt;
Consistent formatting reduces irrelevant differences between programmers&amp;#039; code.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ruff format .&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A formatter decides details such as:&lt;br /&gt;
&lt;br /&gt;
* whitespace;&lt;br /&gt;
* line wrapping;&lt;br /&gt;
* indentation;&lt;br /&gt;
* consistent layout.&lt;br /&gt;
&lt;br /&gt;
Students should not spend project time manually debating formatting details that can be automated.&lt;br /&gt;
&lt;br /&gt;
== 55. Test coverage ==&lt;br /&gt;
&lt;br /&gt;
Coverage measures which lines or branches were executed while tests ran.&lt;br /&gt;
&lt;br /&gt;
Install:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m pip install pytest-cov&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pytest --cov=src&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A terminal report:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pytest \&lt;br /&gt;
    --cov=src \&lt;br /&gt;
    --cov-report=term-missing&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 56. Coverage is not correctness ==&lt;br /&gt;
&lt;br /&gt;
A project can have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
100% line coverage&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and still contain bugs.&lt;br /&gt;
&lt;br /&gt;
Coverage only tells us whether code was executed by tests.&lt;br /&gt;
&lt;br /&gt;
It does not tell us whether:&lt;br /&gt;
&lt;br /&gt;
* the correct assertions were made;&lt;br /&gt;
* important edge cases were tested;&lt;br /&gt;
* the specification is correct.&lt;br /&gt;
&lt;br /&gt;
Use coverage to find untested areas, not as the sole quality metric.&lt;br /&gt;
&lt;br /&gt;
== 57. Edge cases ==&lt;br /&gt;
&lt;br /&gt;
Tests should include normal behavior and important boundary conditions.&lt;br /&gt;
&lt;br /&gt;
For a grade validator:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def valid_grade(grade):&lt;br /&gt;
    return 1 &amp;lt;= grade &amp;lt;= 10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Relevant cases include:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
1&lt;br /&gt;
10&lt;br /&gt;
0.999&lt;br /&gt;
10.001&lt;br /&gt;
negative values&lt;br /&gt;
unexpected types&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Boundary values frequently reveal defects.&lt;br /&gt;
&lt;br /&gt;
== 58. Regression tests ==&lt;br /&gt;
&lt;br /&gt;
When a bug is discovered:&lt;br /&gt;
&lt;br /&gt;
# reproduce the bug;&lt;br /&gt;
# write a test that fails because of the bug;&lt;br /&gt;
# fix the implementation;&lt;br /&gt;
# verify that the test now passes.&lt;br /&gt;
&lt;br /&gt;
The new test becomes a &amp;#039;&amp;#039;&amp;#039;regression test&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
It prevents the same defect from being reintroduced later.&lt;br /&gt;
&lt;br /&gt;
== 59. Testing pure functions ==&lt;br /&gt;
&lt;br /&gt;
Pure functions are particularly easy to test.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(values):&lt;br /&gt;
    return sum(values) / len(values)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output depends only on the input.&lt;br /&gt;
&lt;br /&gt;
Test:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_average():&lt;br /&gt;
    assert average([2, 4, 6]) == 4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No filesystem, network or global state is involved.&lt;br /&gt;
&lt;br /&gt;
== 60. Side effects make testing harder ==&lt;br /&gt;
&lt;br /&gt;
Compare:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def calculate_and_print(values):&lt;br /&gt;
    result = sum(values) / len(values)&lt;br /&gt;
    print(result)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def calculate_average(values):&lt;br /&gt;
    return sum(values) / len(values)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second function is easier to test.&lt;br /&gt;
&lt;br /&gt;
Printing can be performed separately:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
result = calculate_average(values)&lt;br /&gt;
print(result)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Separating computation from input/output improves testability.&lt;br /&gt;
&lt;br /&gt;
== 61. Testing standard output ==&lt;br /&gt;
&lt;br /&gt;
Sometimes output itself is behavior that should be tested.&lt;br /&gt;
&lt;br /&gt;
pytest provides the &amp;lt;code&amp;gt;capsys&amp;lt;/code&amp;gt; fixture.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def greet(name):&lt;br /&gt;
    print(f&amp;quot;Hello, {name}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_greet(capsys):&lt;br /&gt;
    greet(&amp;quot;Alice&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    captured = capsys.readouterr()&lt;br /&gt;
&lt;br /&gt;
    assert captured.out == &amp;quot;Hello, Alice\n&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use this when console output is part of the intended interface.&lt;br /&gt;
&lt;br /&gt;
== 62. Testing user input ==&lt;br /&gt;
&lt;br /&gt;
Code that directly calls &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt; is less convenient to test.&lt;br /&gt;
&lt;br /&gt;
Less testable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def ask_grade():&lt;br /&gt;
    value = input(&amp;quot;Grade: &amp;quot;)&lt;br /&gt;
    return float(value)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A better design often separates input from parsing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def parse_grade(text):&lt;br /&gt;
    return float(text)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
text = input(&amp;quot;Grade: &amp;quot;)&lt;br /&gt;
grade = parse_grade(text)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now &amp;lt;code&amp;gt;parse_grade()&amp;lt;/code&amp;gt; can be tested independently.&lt;br /&gt;
&lt;br /&gt;
== 63. Designing for testability ==&lt;br /&gt;
&lt;br /&gt;
Testable code often has these properties:&lt;br /&gt;
&lt;br /&gt;
* small functions;&lt;br /&gt;
* explicit inputs;&lt;br /&gt;
* explicit return values;&lt;br /&gt;
* limited global state;&lt;br /&gt;
* dependencies passed as parameters;&lt;br /&gt;
* input/output separated from logic;&lt;br /&gt;
* clear error behavior.&lt;br /&gt;
&lt;br /&gt;
These are also generally good software-design properties.&lt;br /&gt;
&lt;br /&gt;
== 64. Global state and testing ==&lt;br /&gt;
&lt;br /&gt;
Global mutable state makes tests influence one another.&lt;br /&gt;
&lt;br /&gt;
Problematic:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
students = []&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def add_student(student):&lt;br /&gt;
    students.append(student)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One test may leave data behind for another test.&lt;br /&gt;
&lt;br /&gt;
Prefer explicit objects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Course:&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        self.students = []&lt;br /&gt;
&lt;br /&gt;
    def add_student(self, student):&lt;br /&gt;
        self.students.append(student)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each test can create a fresh &amp;lt;code&amp;gt;Course&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 65. Tests must be independent ==&lt;br /&gt;
&lt;br /&gt;
A test should not rely on another test running first.&lt;br /&gt;
&lt;br /&gt;
Do not design:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
test_1 creates file&lt;br /&gt;
test_2 assumes file exists&lt;br /&gt;
test_3 deletes file&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each test should set up its own required state.&lt;br /&gt;
&lt;br /&gt;
pytest does not guarantee that tests should be treated as an ordered sequence.&lt;br /&gt;
&lt;br /&gt;
== 66. Deterministic tests ==&lt;br /&gt;
&lt;br /&gt;
A good test should produce the same result every time under the same conditions.&lt;br /&gt;
&lt;br /&gt;
Avoid uncontrolled dependencies on:&lt;br /&gt;
&lt;br /&gt;
* current time;&lt;br /&gt;
* random numbers;&lt;br /&gt;
* network state;&lt;br /&gt;
* external services;&lt;br /&gt;
* execution order.&lt;br /&gt;
&lt;br /&gt;
If randomness is required, control it.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import random&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
random.seed(1234)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Better still, inject the random-number generator or test deterministic subcomponents when possible.&lt;br /&gt;
&lt;br /&gt;
== 67. Testing time-dependent code ==&lt;br /&gt;
&lt;br /&gt;
Code that directly reads the current time is harder to test.&lt;br /&gt;
&lt;br /&gt;
Less testable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from datetime import datetime&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def greeting():&lt;br /&gt;
    hour = datetime.now().hour&lt;br /&gt;
&lt;br /&gt;
    if hour &amp;lt; 12:&lt;br /&gt;
        return &amp;quot;Good morning&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    return &amp;quot;Good afternoon&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More testable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def greeting(hour):&lt;br /&gt;
    if hour &amp;lt; 12:&lt;br /&gt;
        return &amp;quot;Good morning&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    return &amp;quot;Good afternoon&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The system clock interaction can be kept in a thin outer layer.&lt;br /&gt;
&lt;br /&gt;
== 68. Test-driven development ==&lt;br /&gt;
&lt;br /&gt;
One possible workflow is Test-Driven Development (TDD):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
1. Write a failing test&lt;br /&gt;
2. Implement the minimum code to pass&lt;br /&gt;
3. Refactor&lt;br /&gt;
4. Repeat&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TDD is not mandatory for this course.&lt;br /&gt;
&lt;br /&gt;
However, writing tests before fixing a known bug is strongly recommended.&lt;br /&gt;
&lt;br /&gt;
== 69. Refactoring ==&lt;br /&gt;
&lt;br /&gt;
Refactoring means improving the internal structure of code without intentionally changing its external behavior.&lt;br /&gt;
&lt;br /&gt;
Examples include:&lt;br /&gt;
&lt;br /&gt;
* splitting large functions;&lt;br /&gt;
* renaming unclear variables;&lt;br /&gt;
* removing duplication;&lt;br /&gt;
* extracting classes;&lt;br /&gt;
* simplifying conditionals;&lt;br /&gt;
* separating I/O from logic.&lt;br /&gt;
&lt;br /&gt;
Tests make refactoring safer because they detect accidental behavior changes.&lt;br /&gt;
&lt;br /&gt;
== 70. Example refactoring ==&lt;br /&gt;
&lt;br /&gt;
Before:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def process(students):&lt;br /&gt;
    total = 0&lt;br /&gt;
&lt;br /&gt;
    for student in students:&lt;br /&gt;
        total += student.grade&lt;br /&gt;
&lt;br /&gt;
    average = total / len(students)&lt;br /&gt;
&lt;br /&gt;
    for student in students:&lt;br /&gt;
        if student.grade &amp;gt;= average:&lt;br /&gt;
            print(student.name)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(students):&lt;br /&gt;
    return sum(&lt;br /&gt;
        student.grade&lt;br /&gt;
        for student in students&lt;br /&gt;
    ) / len(students)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def above_average(students):&lt;br /&gt;
    threshold = average(students)&lt;br /&gt;
&lt;br /&gt;
    return [&lt;br /&gt;
        student&lt;br /&gt;
        for student in students&lt;br /&gt;
        if student.grade &amp;gt;= threshold&lt;br /&gt;
    ]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second design separates calculation from output and is easier to test.&lt;br /&gt;
&lt;br /&gt;
== 71. Testing the refactored code ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_average():&lt;br /&gt;
    students = [&lt;br /&gt;
        Student(&amp;quot;Alice&amp;quot;, 10),&lt;br /&gt;
        Student(&amp;quot;Bob&amp;quot;, 8),&lt;br /&gt;
    ]&lt;br /&gt;
&lt;br /&gt;
    assert average(students) == 9&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_above_average():&lt;br /&gt;
    students = [&lt;br /&gt;
        Student(&amp;quot;Alice&amp;quot;, 10),&lt;br /&gt;
        Student(&amp;quot;Bob&amp;quot;, 8),&lt;br /&gt;
    ]&lt;br /&gt;
&lt;br /&gt;
    assert above_average(students) == [&lt;br /&gt;
        Student(&amp;quot;Alice&amp;quot;, 10),&lt;br /&gt;
    ]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 72. Code quality workflow ==&lt;br /&gt;
&lt;br /&gt;
A useful local workflow is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
write code&lt;br /&gt;
   ↓&lt;br /&gt;
ruff format .&lt;br /&gt;
   ↓&lt;br /&gt;
ruff check .&lt;br /&gt;
   ↓&lt;br /&gt;
mypy src&lt;br /&gt;
   ↓&lt;br /&gt;
pytest&lt;br /&gt;
   ↓&lt;br /&gt;
commit&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Not every project must use every tool, but automated checks should become normal development habits.&lt;br /&gt;
&lt;br /&gt;
== 73. A simple quality command sequence ==&lt;br /&gt;
&lt;br /&gt;
For the semester project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ruff format .&lt;br /&gt;
ruff check .&lt;br /&gt;
mypy src&lt;br /&gt;
pytest -v&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Before submitting a milestone, all commands should complete successfully.&lt;br /&gt;
&lt;br /&gt;
== 74. Configuration in pyproject.toml ==&lt;br /&gt;
&lt;br /&gt;
Many Python development tools can be configured in &amp;lt;code&amp;gt;pyproject.toml&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[project]&lt;br /&gt;
name = &amp;quot;student-project&amp;quot;&lt;br /&gt;
version = &amp;quot;0.1.0&amp;quot;&lt;br /&gt;
requires-python = &amp;quot;&amp;gt;=3.11&amp;quot;&lt;br /&gt;
&lt;br /&gt;
[tool.pytest.ini_options]&lt;br /&gt;
testpaths = [&amp;quot;tests&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[tool.ruff]&lt;br /&gt;
line-length = 88&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Centralized configuration avoids unnecessary tool-specific configuration files.&lt;br /&gt;
&lt;br /&gt;
== 75. Development dependencies ==&lt;br /&gt;
&lt;br /&gt;
Testing and development tools are dependencies for developers, not necessarily for application users.&lt;br /&gt;
&lt;br /&gt;
Typical development tools include:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
pytest&lt;br /&gt;
pytest-cov&lt;br /&gt;
ruff&lt;br /&gt;
mypy&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a simple student project they can be installed with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m pip install \&lt;br /&gt;
    pytest \&lt;br /&gt;
    pytest-cov \&lt;br /&gt;
    ruff \&lt;br /&gt;
    mypy&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 76. Continuous integration ==&lt;br /&gt;
&lt;br /&gt;
Continuous Integration (CI) automatically runs project checks when code is pushed to a repository.&lt;br /&gt;
&lt;br /&gt;
A simple pipeline might perform:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
checkout repository&lt;br /&gt;
        ↓&lt;br /&gt;
install Python&lt;br /&gt;
        ↓&lt;br /&gt;
install dependencies&lt;br /&gt;
        ↓&lt;br /&gt;
run linter&lt;br /&gt;
        ↓&lt;br /&gt;
run type checker&lt;br /&gt;
        ↓&lt;br /&gt;
run tests&lt;br /&gt;
        ↓&lt;br /&gt;
report success/failure&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This prevents code that only works on one developer&amp;#039;s machine from being accepted unnoticed.&lt;br /&gt;
&lt;br /&gt;
== 77. Example CI idea ==&lt;br /&gt;
&lt;br /&gt;
A CI system might execute:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m pip install -e .&lt;br /&gt;
python -m pip install pytest ruff mypy&lt;br /&gt;
&lt;br /&gt;
ruff check .&lt;br /&gt;
mypy src&lt;br /&gt;
pytest&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The exact CI configuration depends on the Git hosting platform and will not be the focus of this laboratory.&lt;br /&gt;
&lt;br /&gt;
== 78. Definition of done ==&lt;br /&gt;
&lt;br /&gt;
For this course, a feature should not be considered complete merely because:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;quot;It works on my machine.&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A better definition is:&lt;br /&gt;
&lt;br /&gt;
* implementation completed;&lt;br /&gt;
* expected errors handled;&lt;br /&gt;
* automated tests added;&lt;br /&gt;
* existing tests still pass;&lt;br /&gt;
* linting passes;&lt;br /&gt;
* public functions have reasonable type hints;&lt;br /&gt;
* code is committed to Git;&lt;br /&gt;
* relevant documentation is updated.&lt;br /&gt;
&lt;br /&gt;
== 79. Example — testing the grade manager ==&lt;br /&gt;
&lt;br /&gt;
Suppose Lab 3 produced:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
grade_manager/&lt;br /&gt;
├── src/&lt;br /&gt;
│   └── grade_manager/&lt;br /&gt;
│       ├── models.py&lt;br /&gt;
│       ├── storage.py&lt;br /&gt;
│       └── services.py&lt;br /&gt;
└── tests/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;models.py&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from dataclasses import dataclass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@dataclass&lt;br /&gt;
class Student:&lt;br /&gt;
    name: str&lt;br /&gt;
    grade: float&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 80. Testing the average function ==&lt;br /&gt;
&lt;br /&gt;
Suppose:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(students):&lt;br /&gt;
    if not students:&lt;br /&gt;
        raise EmptyStudentListError()&lt;br /&gt;
&lt;br /&gt;
    return sum(&lt;br /&gt;
        student.grade&lt;br /&gt;
        for student in students&lt;br /&gt;
    ) / len(students)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Tests:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import pytest&lt;br /&gt;
&lt;br /&gt;
from grade_manager.models import Student&lt;br /&gt;
from grade_manager.services import (&lt;br /&gt;
    EmptyStudentListError,&lt;br /&gt;
    average,&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_average():&lt;br /&gt;
    students = [&lt;br /&gt;
        Student(&amp;quot;Alice&amp;quot;, 10),&lt;br /&gt;
        Student(&amp;quot;Bob&amp;quot;, 8),&lt;br /&gt;
    ]&lt;br /&gt;
&lt;br /&gt;
    assert average(students) == 9&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def test_average_rejects_empty_list():&lt;br /&gt;
    with pytest.raises(&lt;br /&gt;
        EmptyStudentListError&lt;br /&gt;
    ):&lt;br /&gt;
        average([])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 81. Testing storage ==&lt;br /&gt;
&lt;br /&gt;
Suppose:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def save_students(path, students):&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def load_students(path):&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def test_storage_round_trip(tmp_path):&lt;br /&gt;
    path = tmp_path / &amp;quot;students.json&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    students = [&lt;br /&gt;
        Student(&amp;quot;Alice&amp;quot;, 9.5),&lt;br /&gt;
        Student(&amp;quot;Bob&amp;quot;, 8.0),&lt;br /&gt;
    ]&lt;br /&gt;
&lt;br /&gt;
    save_students(path, students)&lt;br /&gt;
&lt;br /&gt;
    result = load_students(path)&lt;br /&gt;
&lt;br /&gt;
    assert result == students&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This test verifies a complete storage round trip.&lt;br /&gt;
&lt;br /&gt;
== 82. Testing invalid grades ==&lt;br /&gt;
&lt;br /&gt;
If the model validates grades:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
@dataclass&lt;br /&gt;
class Student:&lt;br /&gt;
    name: str&lt;br /&gt;
    grade: float&lt;br /&gt;
&lt;br /&gt;
    def __post_init__(self):&lt;br /&gt;
        if not 1 &amp;lt;= self.grade &amp;lt;= 10:&lt;br /&gt;
            raise InvalidGradeError()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
@pytest.mark.parametrize(&lt;br /&gt;
    &amp;quot;grade&amp;quot;,&lt;br /&gt;
    [&lt;br /&gt;
        -1,&lt;br /&gt;
        0,&lt;br /&gt;
        10.1,&lt;br /&gt;
        100,&lt;br /&gt;
    ],&lt;br /&gt;
)&lt;br /&gt;
def test_invalid_grade(grade):&lt;br /&gt;
    with pytest.raises(&lt;br /&gt;
        InvalidGradeError&lt;br /&gt;
    ):&lt;br /&gt;
        Student(&lt;br /&gt;
            &amp;quot;Alice&amp;quot;,&lt;br /&gt;
            grade,&lt;br /&gt;
        )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 83. What should be tested? ==&lt;br /&gt;
&lt;br /&gt;
Prioritize tests for:&lt;br /&gt;
&lt;br /&gt;
* calculations;&lt;br /&gt;
* validation rules;&lt;br /&gt;
* edge cases;&lt;br /&gt;
* error handling;&lt;br /&gt;
* parsing;&lt;br /&gt;
* storage;&lt;br /&gt;
* important transformations;&lt;br /&gt;
* previously discovered bugs.&lt;br /&gt;
&lt;br /&gt;
Do not prioritize tests for trivial implementation details.&lt;br /&gt;
&lt;br /&gt;
For example, testing that Python assignment works is unnecessary.&lt;br /&gt;
&lt;br /&gt;
== 84. What should not be tested directly? ==&lt;br /&gt;
&lt;br /&gt;
Avoid tests that merely confirm:&lt;br /&gt;
&lt;br /&gt;
* Python itself works;&lt;br /&gt;
* third-party libraries behave exactly as documented;&lt;br /&gt;
* private implementation details that callers should not depend on.&lt;br /&gt;
&lt;br /&gt;
Test your application&amp;#039;s behavior.&lt;br /&gt;
&lt;br /&gt;
== 85. C/C++ habits to reconsider ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ habit&lt;br /&gt;
! Typical Python approach&lt;br /&gt;
|-&lt;br /&gt;
| rely mainly on compiler errors&lt;br /&gt;
| combine interpreter, linting, type checking and tests&lt;br /&gt;
|-&lt;br /&gt;
| manual test executable&lt;br /&gt;
| pytest test suite&lt;br /&gt;
|-&lt;br /&gt;
| specialized assertion macros&lt;br /&gt;
| normal Python &amp;lt;code&amp;gt;assert&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| manually create temporary test files&lt;br /&gt;
| use &amp;lt;code&amp;gt;tmp_path&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| debug mainly with output statements&lt;br /&gt;
| use debugger and structured logging&lt;br /&gt;
|-&lt;br /&gt;
| test large workflows only&lt;br /&gt;
| prefer many focused unit tests plus selected integration tests&lt;br /&gt;
|-&lt;br /&gt;
| tightly construct dependencies inside functions&lt;br /&gt;
| inject dependencies when useful for testing&lt;br /&gt;
|-&lt;br /&gt;
| accept code because it runs once&lt;br /&gt;
| require repeatable automated checks&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 86. Exercise 1 — Basic pytest tests ==&lt;br /&gt;
&lt;br /&gt;
Given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def clamp(value, minimum, maximum):&lt;br /&gt;
    if value &amp;lt; minimum:&lt;br /&gt;
        return minimum&lt;br /&gt;
&lt;br /&gt;
    if value &amp;gt; maximum:&lt;br /&gt;
        return maximum&lt;br /&gt;
&lt;br /&gt;
    return value&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Write tests for:&lt;br /&gt;
&lt;br /&gt;
* a value inside the range;&lt;br /&gt;
* a value below the range;&lt;br /&gt;
* a value above the range;&lt;br /&gt;
* a value equal to the minimum;&lt;br /&gt;
* a value equal to the maximum.&lt;br /&gt;
&lt;br /&gt;
== 87. Exercise 2 — Exception testing ==&lt;br /&gt;
&lt;br /&gt;
Given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def validate_grade(grade):&lt;br /&gt;
    if not 1 &amp;lt;= grade &amp;lt;= 10:&lt;br /&gt;
        raise ValueError(&lt;br /&gt;
            &amp;quot;invalid grade&amp;quot;&lt;br /&gt;
        )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Write pytest tests that verify:&lt;br /&gt;
&lt;br /&gt;
* valid grades do not raise an exception;&lt;br /&gt;
* invalid grades raise &amp;lt;code&amp;gt;ValueError&amp;lt;/code&amp;gt;;&lt;br /&gt;
* the exception message contains &amp;lt;code&amp;gt;invalid grade&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Use parametrization for invalid values.&lt;br /&gt;
&lt;br /&gt;
== 88. Exercise 3 — Fixtures ==&lt;br /&gt;
&lt;br /&gt;
Create a fixture:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
@pytest.fixture&lt;br /&gt;
def students():&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
containing at least four students.&lt;br /&gt;
&lt;br /&gt;
Use the fixture to test:&lt;br /&gt;
&lt;br /&gt;
* average grade;&lt;br /&gt;
* highest grade;&lt;br /&gt;
* passed students;&lt;br /&gt;
* students above the average.&lt;br /&gt;
&lt;br /&gt;
Do not duplicate the same setup in every test.&lt;br /&gt;
&lt;br /&gt;
== 89. Exercise 4 — Parametrization ==&lt;br /&gt;
&lt;br /&gt;
Implement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def is_valid_username(username):&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assume a valid username:&lt;br /&gt;
&lt;br /&gt;
* is at least 3 characters long;&lt;br /&gt;
* is at most 20 characters long;&lt;br /&gt;
* contains only letters, digits and underscore;&lt;br /&gt;
* begins with a letter.&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;@pytest.mark.parametrize&amp;lt;/code&amp;gt; to test multiple valid and invalid usernames.&lt;br /&gt;
&lt;br /&gt;
== 90. Exercise 5 — Temporary files ==&lt;br /&gt;
&lt;br /&gt;
Write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def save_lines(&lt;br /&gt;
    path,&lt;br /&gt;
    lines,&lt;br /&gt;
):&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and test it using &amp;lt;code&amp;gt;tmp_path&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Verify:&lt;br /&gt;
&lt;br /&gt;
* the file is created;&lt;br /&gt;
* every input line is written;&lt;br /&gt;
* line endings are correct;&lt;br /&gt;
* an empty list creates an empty file.&lt;br /&gt;
&lt;br /&gt;
== 91. Exercise 6 — JSON round trip ==&lt;br /&gt;
&lt;br /&gt;
Using the grade-manager code from Lab 3:&lt;br /&gt;
&lt;br /&gt;
# create several &amp;lt;code&amp;gt;Student&amp;lt;/code&amp;gt; objects;&lt;br /&gt;
# save them to a temporary JSON file;&lt;br /&gt;
# load them again;&lt;br /&gt;
# verify that the loaded objects equal the originals.&lt;br /&gt;
&lt;br /&gt;
Do not use a permanent file in the project directory.&lt;br /&gt;
&lt;br /&gt;
== 92. Exercise 7 — Simple fake dependency ==&lt;br /&gt;
&lt;br /&gt;
Given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def fetch_status(client):&lt;br /&gt;
    response = client.get_status()&lt;br /&gt;
&lt;br /&gt;
    return response[&amp;quot;status&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Write a fake client class that returns:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
{&amp;quot;status&amp;quot;: &amp;quot;ok&amp;quot;}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then test &amp;lt;code&amp;gt;fetch_status()&amp;lt;/code&amp;gt; without using a real external service.&lt;br /&gt;
&lt;br /&gt;
== 93. Exercise 8 — Logging ==&lt;br /&gt;
&lt;br /&gt;
Modify a file-loading function so that it logs:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;INFO&amp;lt;/code&amp;gt; when loading begins;&lt;br /&gt;
* &amp;lt;code&amp;gt;WARNING&amp;lt;/code&amp;gt; when the file does not exist;&lt;br /&gt;
* &amp;lt;code&amp;gt;ERROR&amp;lt;/code&amp;gt; when parsing fails.&lt;br /&gt;
&lt;br /&gt;
Do not replace exception handling with logging. Logging describes what happened; exceptions still represent failures.&lt;br /&gt;
&lt;br /&gt;
== 94. Exercise 9 — Refactoring for testability ==&lt;br /&gt;
&lt;br /&gt;
Refactor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def run():&lt;br /&gt;
    first = float(input(&amp;quot;First: &amp;quot;))&lt;br /&gt;
    second = float(input(&amp;quot;Second: &amp;quot;))&lt;br /&gt;
&lt;br /&gt;
    result = first / second&lt;br /&gt;
&lt;br /&gt;
    print(f&amp;quot;Result: {result}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Separate:&lt;br /&gt;
&lt;br /&gt;
* parsing/input;&lt;br /&gt;
* calculation;&lt;br /&gt;
* output.&lt;br /&gt;
&lt;br /&gt;
Write unit tests for the calculation without using &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 95. Exercise 10 — Quality tools ==&lt;br /&gt;
&lt;br /&gt;
For the semester project:&lt;br /&gt;
&lt;br /&gt;
# install &amp;lt;code&amp;gt;pytest&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ruff&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;mypy&amp;lt;/code&amp;gt;;&lt;br /&gt;
# run &amp;lt;code&amp;gt;ruff format .&amp;lt;/code&amp;gt;;&lt;br /&gt;
# run &amp;lt;code&amp;gt;ruff check .&amp;lt;/code&amp;gt;;&lt;br /&gt;
# run &amp;lt;code&amp;gt;mypy src&amp;lt;/code&amp;gt;;&lt;br /&gt;
# run &amp;lt;code&amp;gt;pytest -v&amp;lt;/code&amp;gt;;&lt;br /&gt;
# correct all reasonable issues;&lt;br /&gt;
# commit the resulting changes.&lt;br /&gt;
&lt;br /&gt;
Record the commands in the project&amp;#039;s &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 96. Exercise 11 — Coverage ==&lt;br /&gt;
&lt;br /&gt;
Install:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m pip install pytest-cov&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pytest \&lt;br /&gt;
    --cov=src \&lt;br /&gt;
    --cov-report=term-missing&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Identify one important untested function.&lt;br /&gt;
&lt;br /&gt;
Add tests for it and compare the coverage report before and after.&lt;br /&gt;
&lt;br /&gt;
Do not attempt to maximize coverage by writing meaningless tests.&lt;br /&gt;
&lt;br /&gt;
== 97. Exercise 12 — Project milestone ==&lt;br /&gt;
&lt;br /&gt;
By the end of this laboratory, the semester project should contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
project/&lt;br /&gt;
├── pyproject.toml&lt;br /&gt;
├── README.md&lt;br /&gt;
├── src/&lt;br /&gt;
│   └── project_name/&lt;br /&gt;
│       └── ...&lt;br /&gt;
└── tests/&lt;br /&gt;
    └── ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Minimum requirements:&lt;br /&gt;
&lt;br /&gt;
* application starts successfully;&lt;br /&gt;
* central functionality is implemented;&lt;br /&gt;
* at least five meaningful automated tests exist;&lt;br /&gt;
* at least one error condition is tested;&lt;br /&gt;
* at least one fixture or parametrized test is used;&lt;br /&gt;
* tests do not depend on execution order;&lt;br /&gt;
* linting runs successfully;&lt;br /&gt;
* type checking is attempted;&lt;br /&gt;
* temporary files are used for file-based tests;&lt;br /&gt;
* no virtual environment or &amp;lt;code&amp;gt;__pycache__&amp;lt;/code&amp;gt; directory is committed.&lt;br /&gt;
&lt;br /&gt;
This represents the first project &amp;#039;&amp;#039;&amp;#039;MVP quality checkpoint&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== 98. Summary ==&lt;br /&gt;
&lt;br /&gt;
The main mappings introduced in this laboratory are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ / general concept&lt;br /&gt;
! Python&lt;br /&gt;
|-&lt;br /&gt;
| assertion&lt;br /&gt;
| &amp;lt;code&amp;gt;assert&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| GoogleTest / Catch2&lt;br /&gt;
| &amp;lt;code&amp;gt;pytest&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| assertion macro&lt;br /&gt;
| normal Python assertion expression&lt;br /&gt;
|-&lt;br /&gt;
| exception assertion&lt;br /&gt;
| &amp;lt;code&amp;gt;pytest.raises()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| parameterized test&lt;br /&gt;
| &amp;lt;code&amp;gt;@pytest.mark.parametrize&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| setup / teardown&lt;br /&gt;
| pytest fixture&lt;br /&gt;
|-&lt;br /&gt;
| temporary test directory&lt;br /&gt;
| &amp;lt;code&amp;gt;tmp_path&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| fake/mock dependency&lt;br /&gt;
| simple fake or &amp;lt;code&amp;gt;unittest.mock&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| debugger&lt;br /&gt;
| &amp;lt;code&amp;gt;breakpoint()&amp;lt;/code&amp;gt; / &amp;lt;code&amp;gt;pdb&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| logging library&lt;br /&gt;
| standard &amp;lt;code&amp;gt;logging&amp;lt;/code&amp;gt; module&lt;br /&gt;
|-&lt;br /&gt;
| compiler warnings&lt;br /&gt;
| linter such as &amp;lt;code&amp;gt;ruff&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| static compiler type checks&lt;br /&gt;
| optional checker such as &amp;lt;code&amp;gt;mypy&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| code formatter&lt;br /&gt;
| &amp;lt;code&amp;gt;ruff format&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| test coverage tool&lt;br /&gt;
| &amp;lt;code&amp;gt;pytest-cov&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| automated build checks&lt;br /&gt;
| CI pipeline&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The central idea of this laboratory is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Software quality is not a final step performed after implementation. Testing, static analysis, debugging and automated checks should be part of the normal development process.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 99. Preparation for Lab 5 ==&lt;br /&gt;
&lt;br /&gt;
Before the next laboratory:&lt;br /&gt;
&lt;br /&gt;
# complete the exercises from this laboratory;&lt;br /&gt;
# ensure the semester project has a working MVP;&lt;br /&gt;
# organize the test suite under &amp;lt;code&amp;gt;tests/&amp;lt;/code&amp;gt;;&lt;br /&gt;
# add tests for the most important functionality;&lt;br /&gt;
# run linting and formatting;&lt;br /&gt;
# remove temporary debugging output;&lt;br /&gt;
# replace useful diagnostics with logging;&lt;br /&gt;
# document how to run the application and tests.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Lab 5&amp;#039;&amp;#039;&amp;#039;, the laboratory changes from primarily theory-oriented work to supervised project development. The focus will be architecture review, implementation quality, debugging and completing the main project functionality.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_3&amp;diff=8372</id>
		<title>Lab 3</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_3&amp;diff=8372"/>
		<updated>2026-09-14T06:07:52Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: Pagină nouă: = Lab 3 — Modules, Packages, Files, Exceptions and Type Hints =  == Objectives ==  This laboratory moves from individual Python scripts and classes to the structure of complete P...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 3 — Modules, Packages, Files, Exceptions and Type Hints =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
This laboratory moves from individual Python scripts and classes to the structure of complete Python applications.&lt;br /&gt;
&lt;br /&gt;
Students already know how to split C/C++ applications into multiple source and header files. The objective of this laboratory is to understand the equivalent mechanisms in Python and to learn the conventions used for organizing real Python projects.&lt;br /&gt;
&lt;br /&gt;
After completing this laboratory, you should be able to:&lt;br /&gt;
&lt;br /&gt;
* split a Python program into multiple modules;&lt;br /&gt;
* create and use Python packages;&lt;br /&gt;
* understand the role of &amp;lt;code&amp;gt;__init__.py&amp;lt;/code&amp;gt;;&lt;br /&gt;
* use absolute and relative imports;&lt;br /&gt;
* understand the purpose of &amp;lt;code&amp;gt;if __name__ == &amp;quot;__main__&amp;quot;&amp;lt;/code&amp;gt;;&lt;br /&gt;
* read and write text files;&lt;br /&gt;
* use &amp;lt;code&amp;gt;pathlib&amp;lt;/code&amp;gt; for filesystem paths;&lt;br /&gt;
* read and write CSV and JSON files;&lt;br /&gt;
* use exceptions for error handling;&lt;br /&gt;
* define custom exceptions;&lt;br /&gt;
* use &amp;lt;code&amp;gt;try&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;except&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;else&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;finally&amp;lt;/code&amp;gt;;&lt;br /&gt;
* use context managers and the &amp;lt;code&amp;gt;with&amp;lt;/code&amp;gt; statement;&lt;br /&gt;
* write type hints for variables, functions and collections;&lt;br /&gt;
* use &amp;lt;code&amp;gt;Optional&amp;lt;/code&amp;gt;, unions and type aliases;&lt;br /&gt;
* understand the purpose of static type checkers;&lt;br /&gt;
* organize a small Python project using a &amp;lt;code&amp;gt;src/&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;tests/&amp;lt;/code&amp;gt; layout.&lt;br /&gt;
&lt;br /&gt;
Throughout this laboratory, Python concepts are compared with familiar C/C++ mechanisms whenever a direct comparison is useful.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== 1. From translation units to modules ==&lt;br /&gt;
&lt;br /&gt;
In C/C++, larger programs are usually split into header and source files.&lt;br /&gt;
&lt;br /&gt;
In Python, a source file is also a &amp;#039;&amp;#039;&amp;#039;module&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
project/&lt;br /&gt;
├── main.cpp&lt;br /&gt;
├── math_utils.cpp&lt;br /&gt;
└── math_utils.hpp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
project/&lt;br /&gt;
├── main.py&lt;br /&gt;
└── math_utils.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A Python file can contain:&lt;br /&gt;
&lt;br /&gt;
* functions;&lt;br /&gt;
* classes;&lt;br /&gt;
* constants;&lt;br /&gt;
* executable statements.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# math_utils.py&lt;br /&gt;
&lt;br /&gt;
def add(a, b):&lt;br /&gt;
    return a + b&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def multiply(a, b):&lt;br /&gt;
    return a * b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 2. Importing a module ==&lt;br /&gt;
&lt;br /&gt;
The Python &amp;lt;code&amp;gt;import&amp;lt;/code&amp;gt; statement plays a role similar to including declarations and linking implementation code in C/C++.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;math_utils.hpp&amp;quot;&lt;br /&gt;
&lt;br /&gt;
int result = add(2, 3);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import math_utils&lt;br /&gt;
&lt;br /&gt;
result = math_utils.add(2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Using the module name explicitly makes the origin of the function clear.&lt;br /&gt;
&lt;br /&gt;
== 3. Importing specific names ==&lt;br /&gt;
&lt;br /&gt;
Python can import individual names from a module.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;math_utils.hpp&amp;quot;&lt;br /&gt;
&lt;br /&gt;
int result = add(2, 3);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from math_utils import add&lt;br /&gt;
&lt;br /&gt;
result = add(2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Both styles are common:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import math_utils&lt;br /&gt;
&lt;br /&gt;
math_utils.add(2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from math_utils import add&lt;br /&gt;
&lt;br /&gt;
add(2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 4. Import aliases ==&lt;br /&gt;
&lt;br /&gt;
Modules or imported names can be given aliases.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
namespace fs = std::filesystem;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import numpy as np&lt;br /&gt;
import pandas as pd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This style is common when a library has a well-known conventional alias.&lt;br /&gt;
&lt;br /&gt;
Aliases should improve readability rather than obscure the origin of a name.&lt;br /&gt;
&lt;br /&gt;
== 5. Avoid wildcard imports ==&lt;br /&gt;
&lt;br /&gt;
Python allows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from math_utils import *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, this is generally discouraged.&lt;br /&gt;
&lt;br /&gt;
It makes it difficult to determine where a name originated.&lt;br /&gt;
&lt;br /&gt;
Compare:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Less explicit&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Preferred&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from math_utils import *&lt;br /&gt;
&lt;br /&gt;
result = add(2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from math_utils import add&lt;br /&gt;
&lt;br /&gt;
result = add(2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import math_utils&lt;br /&gt;
&lt;br /&gt;
result = math_utils.add(2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 6. Module execution ==&lt;br /&gt;
&lt;br /&gt;
A Python module may be:&lt;br /&gt;
&lt;br /&gt;
* executed directly;&lt;br /&gt;
* imported by another module.&lt;br /&gt;
&lt;br /&gt;
This distinction is visible through the special variable &amp;lt;code&amp;gt;__name__&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    run_program();&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
    run_program()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
When a file is executed directly:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
__name__ == &amp;quot;__main__&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When it is imported, &amp;lt;code&amp;gt;__name__&amp;lt;/code&amp;gt; contains the module name.&lt;br /&gt;
&lt;br /&gt;
== 7. Why the main guard matters ==&lt;br /&gt;
&lt;br /&gt;
Consider the following file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# tools.py&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;Initializing tools&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def calculate():&lt;br /&gt;
    return 42&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Importing it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import tools&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will immediately print:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Initializing tools&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Top-level statements execute when a module is imported.&lt;br /&gt;
&lt;br /&gt;
For code that should run only when the file is executed directly:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
    print(&amp;quot;Running application&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 8. Packages ==&lt;br /&gt;
&lt;br /&gt;
A package groups related modules into a directory.&lt;br /&gt;
&lt;br /&gt;
A simple package might look like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
project/&lt;br /&gt;
├── main.py&lt;br /&gt;
└── calculator/&lt;br /&gt;
    ├── __init__.py&lt;br /&gt;
    ├── arithmetic.py&lt;br /&gt;
    └── statistics.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The equivalent organizational idea in C++ might involve namespaces and multiple translation units.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
calculator/&lt;br /&gt;
├── arithmetic.cpp&lt;br /&gt;
├── arithmetic.hpp&lt;br /&gt;
├── statistics.cpp&lt;br /&gt;
└── statistics.hpp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
calculator/&lt;br /&gt;
├── __init__.py&lt;br /&gt;
├── arithmetic.py&lt;br /&gt;
└── statistics.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 9. __init__.py ==&lt;br /&gt;
&lt;br /&gt;
Traditionally, &amp;lt;code&amp;gt;__init__.py&amp;lt;/code&amp;gt; marks a directory as a Python package.&lt;br /&gt;
&lt;br /&gt;
It can be empty:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# calculator/__init__.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can also define which names are conveniently exposed by the package.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# calculator/__init__.py&lt;br /&gt;
&lt;br /&gt;
from .arithmetic import add&lt;br /&gt;
from .statistics import average&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then users can write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from calculator import add, average&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
instead of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from calculator.arithmetic import add&lt;br /&gt;
from calculator.statistics import average&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 10. Absolute imports ==&lt;br /&gt;
&lt;br /&gt;
An absolute import starts from the top-level package.&lt;br /&gt;
&lt;br /&gt;
Suppose the project is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
project/&lt;br /&gt;
└── app/&lt;br /&gt;
    ├── __init__.py&lt;br /&gt;
    ├── models.py&lt;br /&gt;
    └── services.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;lt;code&amp;gt;services.py&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from app.models import Student&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is an absolute import.&lt;br /&gt;
&lt;br /&gt;
== 11. Relative imports ==&lt;br /&gt;
&lt;br /&gt;
Modules inside a package may also use relative imports.&lt;br /&gt;
&lt;br /&gt;
Inside &amp;lt;code&amp;gt;services.py&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from .models import Student&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A single dot means:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
the current package&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Two dots mean the parent package:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from ..utils import load_config&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For student projects, absolute imports are often easier to read unless package-local relative imports clearly improve organization.&lt;br /&gt;
&lt;br /&gt;
== 12. Namespaces ==&lt;br /&gt;
&lt;br /&gt;
C++ namespaces and Python modules/packages solve related organizational problems.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
namespace math_utils {&lt;br /&gt;
&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    return a + b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int result =&lt;br /&gt;
    math_utils::add(2, 3);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# math_utils.py&lt;br /&gt;
&lt;br /&gt;
def add(a, b):&lt;br /&gt;
    return a + b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import math_utils&lt;br /&gt;
&lt;br /&gt;
result = math_utils.add(2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A module naturally provides a namespace.&lt;br /&gt;
&lt;br /&gt;
== 13. A first multi-file Python program ==&lt;br /&gt;
&lt;br /&gt;
Consider:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
student_app/&lt;br /&gt;
├── main.py&lt;br /&gt;
├── models.py&lt;br /&gt;
└── services.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;models.py&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from dataclasses import dataclass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@dataclass&lt;br /&gt;
class Student:&lt;br /&gt;
    name: str&lt;br /&gt;
    grade: float&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;services.py&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from models import Student&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def average(students: list[Student]) -&amp;gt; float:&lt;br /&gt;
    total = sum(student.grade for student in students)&lt;br /&gt;
    return total / len(students)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;main.py&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from models import Student&lt;br /&gt;
from services import average&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    students = [&lt;br /&gt;
        Student(&amp;quot;Alice&amp;quot;, 9.5),&lt;br /&gt;
        Student(&amp;quot;Bob&amp;quot;, 8.0),&lt;br /&gt;
    ]&lt;br /&gt;
&lt;br /&gt;
    print(average(students))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 14. Reading text files ==&lt;br /&gt;
&lt;br /&gt;
C++ commonly uses &amp;lt;code&amp;gt;std::ifstream&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python uses the built-in &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
&lt;br /&gt;
std::ifstream file(&amp;quot;data.txt&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
std::string line;&lt;br /&gt;
&lt;br /&gt;
while (std::getline(file, line)) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; line &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
with open(&amp;quot;data.txt&amp;quot;, &amp;quot;r&amp;quot;) as file:&lt;br /&gt;
    for line in file:&lt;br /&gt;
        print(line, end=&amp;quot;&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python &amp;lt;code&amp;gt;with&amp;lt;/code&amp;gt; statement automatically closes the file.&lt;br /&gt;
&lt;br /&gt;
== 15. Context managers ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;with&amp;lt;/code&amp;gt; statement is used with objects that manage resources.&lt;br /&gt;
&lt;br /&gt;
It is conceptually related to deterministic resource management in C++.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++ RAII&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python context manager&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    std::ifstream file(&amp;quot;data.txt&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    // Use file.&lt;br /&gt;
&lt;br /&gt;
} // file is closed here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
with open(&amp;quot;data.txt&amp;quot;) as file:&lt;br /&gt;
    # Use file.&lt;br /&gt;
    ...&lt;br /&gt;
# File is closed here.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The context manager guarantees cleanup even if an exception occurs inside the block.&lt;br /&gt;
&lt;br /&gt;
== 16. Reading an entire file ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::ifstream file(&amp;quot;data.txt&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
std::string content(&lt;br /&gt;
    (std::istreambuf_iterator&amp;lt;char&amp;gt;(file)),&lt;br /&gt;
    std::istreambuf_iterator&amp;lt;char&amp;gt;()&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
with open(&amp;quot;data.txt&amp;quot;, &amp;quot;r&amp;quot;) as file:&lt;br /&gt;
    content = file.read()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 17. Reading lines ==&lt;br /&gt;
&lt;br /&gt;
Python can read all lines into a list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
with open(&amp;quot;data.txt&amp;quot;, &amp;quot;r&amp;quot;) as file:&lt;br /&gt;
    lines = file.readlines()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Usually, if the entire list is not required, direct iteration is preferable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
with open(&amp;quot;data.txt&amp;quot;, &amp;quot;r&amp;quot;) as file:&lt;br /&gt;
    for line in file:&lt;br /&gt;
        ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This avoids loading the entire file into memory.&lt;br /&gt;
&lt;br /&gt;
== 18. Writing text files ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::ofstream file(&amp;quot;output.txt&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
file &amp;lt;&amp;lt; &amp;quot;Hello\n&amp;quot;;&lt;br /&gt;
file &amp;lt;&amp;lt; &amp;quot;Value: &amp;quot; &amp;lt;&amp;lt; value &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
with open(&amp;quot;output.txt&amp;quot;, &amp;quot;w&amp;quot;) as file:&lt;br /&gt;
    file.write(&amp;quot;Hello\n&amp;quot;)&lt;br /&gt;
    file.write(f&amp;quot;Value: {value}\n&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Common modes include:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Mode&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;r&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
| read&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;w&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
| write, replacing existing content&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;a&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
| append&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;x&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
| create, failing if the file already exists&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;rb&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
| binary read&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;wb&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
| binary write&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 19. Text encoding ==&lt;br /&gt;
&lt;br /&gt;
For text files, specifying an encoding explicitly is recommended.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
with open(&lt;br /&gt;
    &amp;quot;data.txt&amp;quot;,&lt;br /&gt;
    &amp;quot;r&amp;quot;,&lt;br /&gt;
    encoding=&amp;quot;utf-8&amp;quot;,&lt;br /&gt;
) as file:&lt;br /&gt;
    content = file.read()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This makes the program&amp;#039;s assumptions explicit and avoids relying on platform-specific defaults.&lt;br /&gt;
&lt;br /&gt;
== 20. pathlib ==&lt;br /&gt;
&lt;br /&gt;
Modern Python code commonly uses &amp;lt;code&amp;gt;pathlib.Path&amp;lt;/code&amp;gt; instead of manually manipulating path strings.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;filesystem&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace fs = std::filesystem;&lt;br /&gt;
&lt;br /&gt;
fs::path path =&lt;br /&gt;
    fs::path(&amp;quot;data&amp;quot;) / &amp;quot;students.txt&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from pathlib import Path&lt;br /&gt;
&lt;br /&gt;
path = Path(&amp;quot;data&amp;quot;) / &amp;quot;students.txt&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; operator joins path components.&lt;br /&gt;
&lt;br /&gt;
== 21. Common pathlib operations ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Operation&lt;br /&gt;
! Python&lt;br /&gt;
|-&lt;br /&gt;
| test whether path exists&lt;br /&gt;
| &amp;lt;code&amp;gt;path.exists()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| test whether path is a file&lt;br /&gt;
| &amp;lt;code&amp;gt;path.is_file()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| test whether path is a directory&lt;br /&gt;
| &amp;lt;code&amp;gt;path.is_dir()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| get filename&lt;br /&gt;
| &amp;lt;code&amp;gt;path.name&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| get extension&lt;br /&gt;
| &amp;lt;code&amp;gt;path.suffix&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| get parent&lt;br /&gt;
| &amp;lt;code&amp;gt;path.parent&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| create directory&lt;br /&gt;
| &amp;lt;code&amp;gt;path.mkdir()&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from pathlib import Path&lt;br /&gt;
&lt;br /&gt;
data_dir = Path(&amp;quot;data&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
data_dir.mkdir(&lt;br /&gt;
    parents=True,&lt;br /&gt;
    exist_ok=True,&lt;br /&gt;
)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 22. Reading and writing with pathlib ==&lt;br /&gt;
&lt;br /&gt;
For small text files, &amp;lt;code&amp;gt;Path&amp;lt;/code&amp;gt; provides convenience methods.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Traditional open()&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | pathlib&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
with open(&lt;br /&gt;
    &amp;quot;data.txt&amp;quot;,&lt;br /&gt;
    &amp;quot;r&amp;quot;,&lt;br /&gt;
    encoding=&amp;quot;utf-8&amp;quot;,&lt;br /&gt;
) as file:&lt;br /&gt;
    content = file.read()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from pathlib import Path&lt;br /&gt;
&lt;br /&gt;
path = Path(&amp;quot;data.txt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
content = path.read_text(&lt;br /&gt;
    encoding=&amp;quot;utf-8&amp;quot;&lt;br /&gt;
)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Writing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
path.write_text(&lt;br /&gt;
    &amp;quot;Hello\n&amp;quot;,&lt;br /&gt;
    encoding=&amp;quot;utf-8&amp;quot;,&lt;br /&gt;
)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 23. CSV files ==&lt;br /&gt;
&lt;br /&gt;
CSV data should generally be processed using Python&amp;#039;s &amp;lt;code&amp;gt;csv&amp;lt;/code&amp;gt; module rather than manually splitting lines.&lt;br /&gt;
&lt;br /&gt;
Suppose:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
name,grade&lt;br /&gt;
Alice,9.5&lt;br /&gt;
Bob,8.0&lt;br /&gt;
Carol,7.5&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Reading it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import csv&lt;br /&gt;
&lt;br /&gt;
with open(&lt;br /&gt;
    &amp;quot;students.csv&amp;quot;,&lt;br /&gt;
    &amp;quot;r&amp;quot;,&lt;br /&gt;
    encoding=&amp;quot;utf-8&amp;quot;,&lt;br /&gt;
    newline=&amp;quot;&amp;quot;,&lt;br /&gt;
) as file:&lt;br /&gt;
    reader = csv.reader(file)&lt;br /&gt;
&lt;br /&gt;
    for row in reader:&lt;br /&gt;
        print(row)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 24. CSV dictionaries ==&lt;br /&gt;
&lt;br /&gt;
When the first row contains column names, &amp;lt;code&amp;gt;csv.DictReader&amp;lt;/code&amp;gt; is often more readable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import csv&lt;br /&gt;
&lt;br /&gt;
with open(&lt;br /&gt;
    &amp;quot;students.csv&amp;quot;,&lt;br /&gt;
    &amp;quot;r&amp;quot;,&lt;br /&gt;
    encoding=&amp;quot;utf-8&amp;quot;,&lt;br /&gt;
    newline=&amp;quot;&amp;quot;,&lt;br /&gt;
) as file:&lt;br /&gt;
    reader = csv.DictReader(file)&lt;br /&gt;
&lt;br /&gt;
    for row in reader:&lt;br /&gt;
        print(row[&amp;quot;name&amp;quot;], row[&amp;quot;grade&amp;quot;])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each row behaves like a dictionary.&lt;br /&gt;
&lt;br /&gt;
== 25. Writing CSV files ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import csv&lt;br /&gt;
&lt;br /&gt;
students = [&lt;br /&gt;
    [&amp;quot;Alice&amp;quot;, 9.5],&lt;br /&gt;
    [&amp;quot;Bob&amp;quot;, 8.0],&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
with open(&lt;br /&gt;
    &amp;quot;students.csv&amp;quot;,&lt;br /&gt;
    &amp;quot;w&amp;quot;,&lt;br /&gt;
    encoding=&amp;quot;utf-8&amp;quot;,&lt;br /&gt;
    newline=&amp;quot;&amp;quot;,&lt;br /&gt;
) as file:&lt;br /&gt;
    writer = csv.writer(file)&lt;br /&gt;
&lt;br /&gt;
    writer.writerow([&amp;quot;name&amp;quot;, &amp;quot;grade&amp;quot;])&lt;br /&gt;
    writer.writerows(students)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 26. JSON files ==&lt;br /&gt;
&lt;br /&gt;
JSON is commonly used for configuration files, APIs and structured data.&lt;br /&gt;
&lt;br /&gt;
Example JSON:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;json&amp;quot;&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;br /&gt;
    &amp;quot;year&amp;quot;: 2,&lt;br /&gt;
    &amp;quot;active&amp;quot;: true&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s standard library provides the &amp;lt;code&amp;gt;json&amp;lt;/code&amp;gt; module.&lt;br /&gt;
&lt;br /&gt;
== 27. Reading JSON ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Conceptual C++ approach&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// Usually requires an external JSON library,&lt;br /&gt;
// for example nlohmann/json.&lt;br /&gt;
//&lt;br /&gt;
// std::ifstream file(&amp;quot;config.json&amp;quot;);&lt;br /&gt;
// json config = json::parse(file);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import json&lt;br /&gt;
&lt;br /&gt;
with open(&lt;br /&gt;
    &amp;quot;config.json&amp;quot;,&lt;br /&gt;
    &amp;quot;r&amp;quot;,&lt;br /&gt;
    encoding=&amp;quot;utf-8&amp;quot;,&lt;br /&gt;
) as file:&lt;br /&gt;
    config = json.load(file)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The resulting Python object normally contains dictionaries, lists, strings, numbers, booleans and &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 28. Writing JSON ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import json&lt;br /&gt;
&lt;br /&gt;
config = {&lt;br /&gt;
    &amp;quot;host&amp;quot;: &amp;quot;localhost&amp;quot;,&lt;br /&gt;
    &amp;quot;port&amp;quot;: 8000,&lt;br /&gt;
    &amp;quot;debug&amp;quot;: True,&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
with open(&lt;br /&gt;
    &amp;quot;config.json&amp;quot;,&lt;br /&gt;
    &amp;quot;w&amp;quot;,&lt;br /&gt;
    encoding=&amp;quot;utf-8&amp;quot;,&lt;br /&gt;
) as file:&lt;br /&gt;
    json.dump(&lt;br /&gt;
        config,&lt;br /&gt;
        file,&lt;br /&gt;
        indent=4,&lt;br /&gt;
    )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 29. Exceptions ==&lt;br /&gt;
&lt;br /&gt;
Python exceptions serve the same general purpose as C++ exceptions.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    int value = std::stoi(text);&lt;br /&gt;
}&lt;br /&gt;
catch (const std::invalid_argument&amp;amp; e) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Invalid input\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    value = int(text)&lt;br /&gt;
except ValueError:&lt;br /&gt;
    print(&amp;quot;Invalid input&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 30. Raising exceptions ==&lt;br /&gt;
&lt;br /&gt;
C++ uses &amp;lt;code&amp;gt;throw&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python uses &amp;lt;code&amp;gt;raise&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (value &amp;lt; 0) {&lt;br /&gt;
    throw std::invalid_argument(&lt;br /&gt;
        &amp;quot;value cannot be negative&amp;quot;&lt;br /&gt;
    );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if value &amp;lt; 0:&lt;br /&gt;
    raise ValueError(&lt;br /&gt;
        &amp;quot;value cannot be negative&amp;quot;&lt;br /&gt;
    )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 31. Catching multiple exception types ==&lt;br /&gt;
&lt;br /&gt;
Python can handle different exception types separately.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // ...&lt;br /&gt;
}&lt;br /&gt;
catch (const std::invalid_argument&amp;amp; e) {&lt;br /&gt;
    // ...&lt;br /&gt;
}&lt;br /&gt;
catch (const std::runtime_error&amp;amp; e) {&lt;br /&gt;
    // ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    ...&lt;br /&gt;
except ValueError:&lt;br /&gt;
    ...&lt;br /&gt;
except RuntimeError:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 32. Accessing the exception object ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
catch (const std::exception&amp;amp; e) {&lt;br /&gt;
    std::cerr &amp;lt;&amp;lt; e.what() &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
except ValueError as error:&lt;br /&gt;
    print(error)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 33. Avoid catching everything ==&lt;br /&gt;
&lt;br /&gt;
This is possible:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    ...&lt;br /&gt;
except Exception:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
but catching broad exceptions without a clear reason can hide programming errors.&lt;br /&gt;
&lt;br /&gt;
Prefer catching the specific errors that the code is expected to handle.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    value = int(text)&lt;br /&gt;
except ValueError:&lt;br /&gt;
    print(&amp;quot;Expected an integer&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 34. try / except / else ==&lt;br /&gt;
&lt;br /&gt;
Python provides an &amp;lt;code&amp;gt;else&amp;lt;/code&amp;gt; block that executes only if no exception was raised.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    value = int(text)&lt;br /&gt;
except ValueError:&lt;br /&gt;
    print(&amp;quot;Invalid integer&amp;quot;)&lt;br /&gt;
else:&lt;br /&gt;
    print(f&amp;quot;Parsed value: {value}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can keep the protected section of code small.&lt;br /&gt;
&lt;br /&gt;
== 35. finally ==&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;code&amp;gt;finally&amp;lt;/code&amp;gt; block executes whether or not an exception occurred.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++ idea&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Work.&lt;br /&gt;
}&lt;br /&gt;
catch (...) {&lt;br /&gt;
    // Handle error.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Cleanup must still happen,&lt;br /&gt;
// preferably through RAII.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    ...&lt;br /&gt;
except ValueError:&lt;br /&gt;
    ...&lt;br /&gt;
finally:&lt;br /&gt;
    print(&amp;quot;Always executed&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For resource cleanup, context managers are usually preferable to manually written &amp;lt;code&amp;gt;finally&amp;lt;/code&amp;gt; blocks.&lt;br /&gt;
&lt;br /&gt;
== 36. Custom exceptions ==&lt;br /&gt;
&lt;br /&gt;
Application-specific errors can be represented by custom exception classes.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class InvalidGrade&lt;br /&gt;
    : public std::runtime_error&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    InvalidGrade()&lt;br /&gt;
        : std::runtime_error(&lt;br /&gt;
            &amp;quot;invalid grade&amp;quot;&lt;br /&gt;
        )&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class InvalidGradeError(ValueError):&lt;br /&gt;
    pass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if not 1 &amp;lt;= grade &amp;lt;= 10:&lt;br /&gt;
    raise InvalidGradeError(&lt;br /&gt;
        &amp;quot;grade must be between 1 and 10&amp;quot;&lt;br /&gt;
    )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 37. Exception hierarchy ==&lt;br /&gt;
&lt;br /&gt;
Python exceptions form a class hierarchy.&lt;br /&gt;
&lt;br /&gt;
A simplified part of it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
BaseException&lt;br /&gt;
└── Exception&lt;br /&gt;
    ├── ValueError&lt;br /&gt;
    ├── TypeError&lt;br /&gt;
    ├── OSError&lt;br /&gt;
    │   └── FileNotFoundError&lt;br /&gt;
    ├── KeyError&lt;br /&gt;
    ├── IndexError&lt;br /&gt;
    └── RuntimeError&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Catching a base class also catches derived exception types.&lt;br /&gt;
&lt;br /&gt;
Therefore:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
except OSError:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also catches errors such as &amp;lt;code&amp;gt;FileNotFoundError&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 38. Common exception types ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Exception&lt;br /&gt;
! Typical meaning&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;ValueError&amp;lt;/code&amp;gt;&lt;br /&gt;
| correct type, invalid value&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;TypeError&amp;lt;/code&amp;gt;&lt;br /&gt;
| operation used with inappropriate type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;FileNotFoundError&amp;lt;/code&amp;gt;&lt;br /&gt;
| requested file does not exist&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;PermissionError&amp;lt;/code&amp;gt;&lt;br /&gt;
| insufficient filesystem permissions&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;KeyError&amp;lt;/code&amp;gt;&lt;br /&gt;
| dictionary key does not exist&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;IndexError&amp;lt;/code&amp;gt;&lt;br /&gt;
| sequence index outside valid range&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;ZeroDivisionError&amp;lt;/code&amp;gt;&lt;br /&gt;
| division by zero&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 39. EAFP versus LBYL ==&lt;br /&gt;
&lt;br /&gt;
Python often follows the principle:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Easier to Ask Forgiveness than Permission (EAFP).&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of checking everything before an operation, perform the operation and handle the expected exception.&lt;br /&gt;
&lt;br /&gt;
Compare:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Check first&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | EAFP style&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if &amp;quot;grade&amp;quot; in student:&lt;br /&gt;
    grade = student[&amp;quot;grade&amp;quot;]&lt;br /&gt;
else:&lt;br /&gt;
    grade = None&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    grade = student[&amp;quot;grade&amp;quot;]&lt;br /&gt;
except KeyError:&lt;br /&gt;
    grade = None&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This does not mean exceptions should be used for every branch. Use the style that expresses the intent clearly.&lt;br /&gt;
&lt;br /&gt;
== 40. Type hints ==&lt;br /&gt;
&lt;br /&gt;
Python is dynamically typed, but it supports optional static type annotations.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    return a + b;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def add(a: int, b: int) -&amp;gt; int:&lt;br /&gt;
    return a + b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not normally enforce these annotations at runtime.&lt;br /&gt;
&lt;br /&gt;
They are primarily used by:&lt;br /&gt;
&lt;br /&gt;
* IDEs;&lt;br /&gt;
* linters;&lt;br /&gt;
* static type checkers;&lt;br /&gt;
* documentation tools;&lt;br /&gt;
* programmers reading the code.&lt;br /&gt;
&lt;br /&gt;
== 41. Variable annotations ==&lt;br /&gt;
&lt;br /&gt;
Variables can also be annotated.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int count = 0;&lt;br /&gt;
double average = 0.0;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
count: int = 0&lt;br /&gt;
average: float = 0.0&lt;br /&gt;
name: str = &amp;quot;Alice&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Annotations are optional when the type is obvious.&lt;br /&gt;
&lt;br /&gt;
== 42. Collection type hints ==&lt;br /&gt;
&lt;br /&gt;
Modern Python allows built-in collection types to be parameterized.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values;&lt;br /&gt;
&lt;br /&gt;
std::unordered_map&amp;lt;&lt;br /&gt;
    std::string,&lt;br /&gt;
    double&lt;br /&gt;
&amp;gt; grades;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values: list[int] = []&lt;br /&gt;
&lt;br /&gt;
grades: dict[str, float] = {}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Other examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
names: set[str] = set()&lt;br /&gt;
&lt;br /&gt;
position: tuple[float, float] = (&lt;br /&gt;
    10.0,&lt;br /&gt;
    20.0,&lt;br /&gt;
)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 43. Function annotations with collections ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
double average(&lt;br /&gt;
    const std::vector&amp;lt;double&amp;gt;&amp;amp; values&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(&lt;br /&gt;
    values: list[float],&lt;br /&gt;
) -&amp;gt; float:&lt;br /&gt;
    return sum(values) / len(values)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 44. Optional values ==&lt;br /&gt;
&lt;br /&gt;
Sometimes a function may return either a value or no value.&lt;br /&gt;
&lt;br /&gt;
In C++, &amp;lt;code&amp;gt;std::optional&amp;lt;/code&amp;gt; can express this.&lt;br /&gt;
&lt;br /&gt;
Modern Python can use the union operator &amp;lt;code&amp;gt;|&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::optional&amp;lt;Student&amp;gt; findStudent(&lt;br /&gt;
    const std::string&amp;amp; name&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def find_student(&lt;br /&gt;
    name: str,&lt;br /&gt;
) -&amp;gt; Student | None:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Older Python code may use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from typing import Optional&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def find_student(&lt;br /&gt;
    name: str,&lt;br /&gt;
) -&amp;gt; Optional[Student]:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 45. Union types ==&lt;br /&gt;
&lt;br /&gt;
A value may have more than one accepted type.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++ idea&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::variant&amp;lt;int, std::string&amp;gt; value;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value: int | str&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Function example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def parse_identifier(&lt;br /&gt;
    value: int | str,&lt;br /&gt;
) -&amp;gt; str:&lt;br /&gt;
    return str(value)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 46. Type aliases ==&lt;br /&gt;
&lt;br /&gt;
Complex type annotations can be given descriptive names.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
using StudentGrades =&lt;br /&gt;
    std::unordered_map&amp;lt;&lt;br /&gt;
        std::string,&lt;br /&gt;
        std::vector&amp;lt;double&amp;gt;&lt;br /&gt;
    &amp;gt;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
StudentGrades = dict[&lt;br /&gt;
    str,&lt;br /&gt;
    list[float],&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Then:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average_grades(&lt;br /&gt;
    grades: StudentGrades,&lt;br /&gt;
) -&amp;gt; dict[str, float]:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 47. The Any type ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;Any&amp;lt;/code&amp;gt; means that static type checking should accept any type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from typing import Any&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
value: Any&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use it carefully.&lt;br /&gt;
&lt;br /&gt;
Overusing &amp;lt;code&amp;gt;Any&amp;lt;/code&amp;gt; removes much of the benefit of static type checking.&lt;br /&gt;
&lt;br /&gt;
== 48. Static type checking ==&lt;br /&gt;
&lt;br /&gt;
A static type checker can analyze annotations without executing the program.&lt;br /&gt;
&lt;br /&gt;
For example, with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def add(a: int, b: int) -&amp;gt; int:&lt;br /&gt;
    return a + b&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
result = add(&amp;quot;hello&amp;quot;, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python itself may only fail when that line executes.&lt;br /&gt;
&lt;br /&gt;
A static checker can report the mismatch before execution.&lt;br /&gt;
&lt;br /&gt;
A common checker is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mypy .&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another modern option is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pyright&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 49. Type hints are not runtime validation ==&lt;br /&gt;
&lt;br /&gt;
This function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def square(value: int) -&amp;gt; int:&lt;br /&gt;
    return value * value&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
can still be called at runtime with another compatible object:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(square(3.5))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Type hints do not automatically reject the call.&lt;br /&gt;
&lt;br /&gt;
If runtime validation is required, it must be implemented separately.&lt;br /&gt;
&lt;br /&gt;
== 50. Project structure ==&lt;br /&gt;
&lt;br /&gt;
For a larger project, avoid placing every file in one directory.&lt;br /&gt;
&lt;br /&gt;
A useful structure is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
student-project/&lt;br /&gt;
├── pyproject.toml&lt;br /&gt;
├── README.md&lt;br /&gt;
├── .gitignore&lt;br /&gt;
├── src/&lt;br /&gt;
│   └── student_project/&lt;br /&gt;
│       ├── __init__.py&lt;br /&gt;
│       ├── models.py&lt;br /&gt;
│       ├── services.py&lt;br /&gt;
│       ├── storage.py&lt;br /&gt;
│       └── main.py&lt;br /&gt;
└── tests/&lt;br /&gt;
    ├── test_services.py&lt;br /&gt;
    └── test_storage.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;src/&amp;lt;/code&amp;gt; layout clearly separates application code from project metadata and tests.&lt;br /&gt;
&lt;br /&gt;
== 51. C++ project layout versus Python project layout ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Typical C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Typical Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
project/&lt;br /&gt;
├── CMakeLists.txt&lt;br /&gt;
├── include/&lt;br /&gt;
│   └── project/&lt;br /&gt;
│       └── model.hpp&lt;br /&gt;
├── src/&lt;br /&gt;
│   ├── main.cpp&lt;br /&gt;
│   └── model.cpp&lt;br /&gt;
└── tests/&lt;br /&gt;
    └── test_model.cpp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
project/&lt;br /&gt;
├── pyproject.toml&lt;br /&gt;
├── src/&lt;br /&gt;
│   └── project/&lt;br /&gt;
│       ├── __init__.py&lt;br /&gt;
│       ├── main.py&lt;br /&gt;
│       └── model.py&lt;br /&gt;
└── tests/&lt;br /&gt;
    └── test_model.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 52. pyproject.toml ==&lt;br /&gt;
&lt;br /&gt;
Modern Python projects commonly use &amp;lt;code&amp;gt;pyproject.toml&amp;lt;/code&amp;gt; for project metadata and tool configuration.&lt;br /&gt;
&lt;br /&gt;
A minimal example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[project]&lt;br /&gt;
name = &amp;quot;student-project&amp;quot;&lt;br /&gt;
version = &amp;quot;0.1.0&amp;quot;&lt;br /&gt;
requires-python = &amp;quot;&amp;gt;=3.11&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dependencies can also be declared:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[project]&lt;br /&gt;
name = &amp;quot;student-project&amp;quot;&lt;br /&gt;
version = &amp;quot;0.1.0&amp;quot;&lt;br /&gt;
requires-python = &amp;quot;&amp;gt;=3.11&amp;quot;&lt;br /&gt;
&lt;br /&gt;
dependencies = [&lt;br /&gt;
    &amp;quot;requests&amp;gt;=2.32&amp;quot;,&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 53. Virtual environments ==&lt;br /&gt;
&lt;br /&gt;
Each project should use its own virtual environment.&lt;br /&gt;
&lt;br /&gt;
Create one:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 -m venv .venv&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Linux/macOS:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source .venv/bin/activate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Windows PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
.venv\Scripts\Activate.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Install dependencies:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m pip install requests&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 54. Why python -m pip? ==&lt;br /&gt;
&lt;br /&gt;
Using:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m pip install requests&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
makes it explicit which Python interpreter is being used to run &amp;lt;code&amp;gt;pip&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
This is useful when multiple Python installations or virtual environments exist.&lt;br /&gt;
&lt;br /&gt;
== 55. __pycache__ and .pyc files ==&lt;br /&gt;
&lt;br /&gt;
Python may compile modules to bytecode and store them in:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
__pycache__/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
__pycache__/&lt;br /&gt;
└── models.cpython-313.pyc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These files should normally not be committed to Git.&lt;br /&gt;
&lt;br /&gt;
A basic &amp;lt;code&amp;gt;.gitignore&amp;lt;/code&amp;gt; includes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.venv/&lt;br /&gt;
__pycache__/&lt;br /&gt;
*.pyc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 56. Example project — Student grade manager ==&lt;br /&gt;
&lt;br /&gt;
Consider the following project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
grade_manager/&lt;br /&gt;
├── pyproject.toml&lt;br /&gt;
├── src/&lt;br /&gt;
│   └── grade_manager/&lt;br /&gt;
│       ├── __init__.py&lt;br /&gt;
│       ├── models.py&lt;br /&gt;
│       ├── storage.py&lt;br /&gt;
│       ├── services.py&lt;br /&gt;
│       └── main.py&lt;br /&gt;
└── data/&lt;br /&gt;
    └── students.json&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;models.py&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from dataclasses import dataclass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@dataclass&lt;br /&gt;
class Student:&lt;br /&gt;
    name: str&lt;br /&gt;
    grade: float&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 57. Example project — storage module ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;storage.py&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import json&lt;br /&gt;
from pathlib import Path&lt;br /&gt;
&lt;br /&gt;
from .models import Student&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def load_students(&lt;br /&gt;
    path: Path,&lt;br /&gt;
) -&amp;gt; list[Student]:&lt;br /&gt;
    try:&lt;br /&gt;
        content = path.read_text(&lt;br /&gt;
            encoding=&amp;quot;utf-8&amp;quot;&lt;br /&gt;
        )&lt;br /&gt;
    except FileNotFoundError:&lt;br /&gt;
        return []&lt;br /&gt;
&lt;br /&gt;
    data = json.loads(content)&lt;br /&gt;
&lt;br /&gt;
    return [&lt;br /&gt;
        Student(&lt;br /&gt;
            name=item[&amp;quot;name&amp;quot;],&lt;br /&gt;
            grade=float(item[&amp;quot;grade&amp;quot;]),&lt;br /&gt;
        )&lt;br /&gt;
        for item in data&lt;br /&gt;
    ]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def save_students(&lt;br /&gt;
    path: Path,&lt;br /&gt;
    students: list[Student],&lt;br /&gt;
) -&amp;gt; None:&lt;br /&gt;
    data = [&lt;br /&gt;
        {&lt;br /&gt;
            &amp;quot;name&amp;quot;: student.name,&lt;br /&gt;
            &amp;quot;grade&amp;quot;: student.grade,&lt;br /&gt;
        }&lt;br /&gt;
        for student in students&lt;br /&gt;
    ]&lt;br /&gt;
&lt;br /&gt;
    path.write_text(&lt;br /&gt;
        json.dumps(data, indent=4),&lt;br /&gt;
        encoding=&amp;quot;utf-8&amp;quot;,&lt;br /&gt;
    )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 58. Example project — service module ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;services.py&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from .models import Student&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class EmptyStudentListError(ValueError):&lt;br /&gt;
    pass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def average(&lt;br /&gt;
    students: list[Student],&lt;br /&gt;
) -&amp;gt; float:&lt;br /&gt;
    if not students:&lt;br /&gt;
        raise EmptyStudentListError(&lt;br /&gt;
            &amp;quot;cannot calculate average &amp;quot;&lt;br /&gt;
            &amp;quot;for an empty list&amp;quot;&lt;br /&gt;
        )&lt;br /&gt;
&lt;br /&gt;
    return sum(&lt;br /&gt;
        student.grade&lt;br /&gt;
        for student in students&lt;br /&gt;
    ) / len(students)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 59. Example project — main module ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;main.py&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from pathlib import Path&lt;br /&gt;
&lt;br /&gt;
from .services import (&lt;br /&gt;
    EmptyStudentListError,&lt;br /&gt;
    average,&lt;br /&gt;
)&lt;br /&gt;
from .storage import load_students&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main() -&amp;gt; None:&lt;br /&gt;
    path = Path(&amp;quot;data/students.json&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    students = load_students(path)&lt;br /&gt;
&lt;br /&gt;
    try:&lt;br /&gt;
        result = average(students)&lt;br /&gt;
    except EmptyStudentListError as error:&lt;br /&gt;
        print(error)&lt;br /&gt;
        return&lt;br /&gt;
&lt;br /&gt;
    print(f&amp;quot;Average: {result:.2f}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example combines:&lt;br /&gt;
&lt;br /&gt;
* modules;&lt;br /&gt;
* packages;&lt;br /&gt;
* relative imports;&lt;br /&gt;
* dataclasses;&lt;br /&gt;
* file handling;&lt;br /&gt;
* JSON;&lt;br /&gt;
* exceptions;&lt;br /&gt;
* type hints;&lt;br /&gt;
* &amp;lt;code&amp;gt;pathlib&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 60. Common mistakes ==&lt;br /&gt;
&lt;br /&gt;
=== Running package files directly ===&lt;br /&gt;
&lt;br /&gt;
Suppose &amp;lt;code&amp;gt;main.py&amp;lt;/code&amp;gt; contains:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from .services import average&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Running:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python src/grade_manager/main.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
may fail because the file is being executed outside its package context.&lt;br /&gt;
&lt;br /&gt;
Instead, from an appropriate project location, run the module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m grade_manager.main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after installing the project or configuring the source path appropriately.&lt;br /&gt;
&lt;br /&gt;
=== Circular imports ===&lt;br /&gt;
&lt;br /&gt;
Avoid designs such as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
models.py imports services.py&lt;br /&gt;
services.py imports models.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This usually indicates that responsibilities should be reorganized.&lt;br /&gt;
&lt;br /&gt;
=== Too much code at module level ===&lt;br /&gt;
&lt;br /&gt;
Prefer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
instead of placing the entire application directly at module scope.&lt;br /&gt;
&lt;br /&gt;
== 61. C/C++ habits to reconsider ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ habit&lt;br /&gt;
! Typical Python approach&lt;br /&gt;
|-&lt;br /&gt;
| separate declaration and implementation files&lt;br /&gt;
| usually keep a class or function definition in one &amp;lt;code&amp;gt;.py&amp;lt;/code&amp;gt; module&lt;br /&gt;
|-&lt;br /&gt;
| include headers&lt;br /&gt;
| import modules or names&lt;br /&gt;
|-&lt;br /&gt;
| manually concatenate path strings&lt;br /&gt;
| use &amp;lt;code&amp;gt;pathlib.Path&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| explicitly close files everywhere&lt;br /&gt;
| use &amp;lt;code&amp;gt;with&amp;lt;/code&amp;gt; context managers&lt;br /&gt;
|-&lt;br /&gt;
| use error return codes for expected failures&lt;br /&gt;
| use exceptions when appropriate&lt;br /&gt;
|-&lt;br /&gt;
| create large monolithic source files&lt;br /&gt;
| split responsibilities into modules and packages&lt;br /&gt;
|-&lt;br /&gt;
| rely only on runtime type information&lt;br /&gt;
| use type hints and static analysis when useful&lt;br /&gt;
|-&lt;br /&gt;
| put all source files at project root&lt;br /&gt;
| use a clear project structure such as &amp;lt;code&amp;gt;src/&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;tests/&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 62. Exercise 1 — Split a program into modules ==&lt;br /&gt;
&lt;br /&gt;
Start from this single-file program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from dataclasses import dataclass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@dataclass&lt;br /&gt;
class Student:&lt;br /&gt;
    name: str&lt;br /&gt;
    grade: float&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def average(students):&lt;br /&gt;
    return sum(&lt;br /&gt;
        student.grade&lt;br /&gt;
        for student in students&lt;br /&gt;
    ) / len(students)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
students = [&lt;br /&gt;
    Student(&amp;quot;Alice&amp;quot;, 9.5),&lt;br /&gt;
    Student(&amp;quot;Bob&amp;quot;, 8.0),&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
print(average(students))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Split it into:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
main.py&lt;br /&gt;
models.py&lt;br /&gt;
services.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use imports rather than duplicating code.&lt;br /&gt;
&lt;br /&gt;
== 63. Exercise 2 — Build a package ==&lt;br /&gt;
&lt;br /&gt;
Transform the previous exercise into:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
student_app/&lt;br /&gt;
├── __init__.py&lt;br /&gt;
├── models.py&lt;br /&gt;
├── services.py&lt;br /&gt;
└── main.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Requirements:&lt;br /&gt;
&lt;br /&gt;
* use package imports;&lt;br /&gt;
* add a &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function;&lt;br /&gt;
* use &amp;lt;code&amp;gt;if __name__ == &amp;quot;__main__&amp;quot;&amp;lt;/code&amp;gt;;&lt;br /&gt;
* run the application as a module.&lt;br /&gt;
&lt;br /&gt;
== 64. Exercise 3 — Text file processing ==&lt;br /&gt;
&lt;br /&gt;
Create a program that reads:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
grades.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with one grade per line.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
9.5&lt;br /&gt;
8.0&lt;br /&gt;
7.5&lt;br /&gt;
10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The program must:&lt;br /&gt;
&lt;br /&gt;
# read all valid grades;&lt;br /&gt;
# ignore empty lines;&lt;br /&gt;
# report invalid values;&lt;br /&gt;
# calculate the average;&lt;br /&gt;
# print the highest and lowest grade.&lt;br /&gt;
&lt;br /&gt;
Use:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;with&amp;lt;/code&amp;gt;;&lt;br /&gt;
* exception handling;&lt;br /&gt;
* type hints.&lt;br /&gt;
&lt;br /&gt;
== 65. Exercise 4 — pathlib ==&lt;br /&gt;
&lt;br /&gt;
Write a function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def list_python_files(&lt;br /&gt;
    directory: Path,&lt;br /&gt;
) -&amp;gt; list[Path]:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
that returns all &amp;lt;code&amp;gt;.py&amp;lt;/code&amp;gt; files in a directory.&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;pathlib&amp;lt;/code&amp;gt;, not string concatenation.&lt;br /&gt;
&lt;br /&gt;
Extension: search recursively.&lt;br /&gt;
&lt;br /&gt;
== 66. Exercise 5 — CSV ==&lt;br /&gt;
&lt;br /&gt;
Create a CSV file containing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
name,grade&lt;br /&gt;
Alice,9.5&lt;br /&gt;
Bob,8.0&lt;br /&gt;
Carol,7.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Write functions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def load_students(path: Path) -&amp;gt; list[Student]:&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def save_students(&lt;br /&gt;
    path: Path,&lt;br /&gt;
    students: list[Student],&lt;br /&gt;
) -&amp;gt; None:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use the &amp;lt;code&amp;gt;csv&amp;lt;/code&amp;gt; module.&lt;br /&gt;
&lt;br /&gt;
== 67. Exercise 6 — JSON configuration ==&lt;br /&gt;
&lt;br /&gt;
Create:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;json&amp;quot;&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;minimum_grade&amp;quot;: 5.0,&lt;br /&gt;
    &amp;quot;maximum_grade&amp;quot;: 10.0,&lt;br /&gt;
    &amp;quot;output_file&amp;quot;: &amp;quot;results.txt&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Write a Python program that:&lt;br /&gt;
&lt;br /&gt;
# loads the configuration;&lt;br /&gt;
# validates the values;&lt;br /&gt;
# reports malformed or missing configuration;&lt;br /&gt;
# uses the configured output filename.&lt;br /&gt;
&lt;br /&gt;
Use specific exception types where possible.&lt;br /&gt;
&lt;br /&gt;
== 68. Exercise 7 — Custom exceptions ==&lt;br /&gt;
&lt;br /&gt;
Create:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class InvalidGradeError(ValueError):&lt;br /&gt;
    pass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def validate_grade(grade: float) -&amp;gt; None:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function should raise &amp;lt;code&amp;gt;InvalidGradeError&amp;lt;/code&amp;gt; when the grade is outside the accepted range.&lt;br /&gt;
&lt;br /&gt;
Use it when constructing or loading students.&lt;br /&gt;
&lt;br /&gt;
== 69. Exercise 8 — Type hints ==&lt;br /&gt;
&lt;br /&gt;
Add complete type annotations to the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def load_names(path):&lt;br /&gt;
    with open(path) as file:&lt;br /&gt;
        return [&lt;br /&gt;
            line.strip()&lt;br /&gt;
            for line in file&lt;br /&gt;
            if line.strip()&lt;br /&gt;
        ]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def find_name(names, target):&lt;br /&gt;
    for name in names:&lt;br /&gt;
        if name == target:&lt;br /&gt;
            return name&lt;br /&gt;
&lt;br /&gt;
    return None&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The final function should make it clear from its return type that the requested name may not exist.&lt;br /&gt;
&lt;br /&gt;
== 70. Exercise 9 — Refactor a monolithic application ==&lt;br /&gt;
&lt;br /&gt;
Consider an application that currently contains:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
main.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and mixes:&lt;br /&gt;
&lt;br /&gt;
* data classes;&lt;br /&gt;
* file reading;&lt;br /&gt;
* JSON parsing;&lt;br /&gt;
* calculations;&lt;br /&gt;
* user interaction.&lt;br /&gt;
&lt;br /&gt;
Refactor it into at least:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
models.py&lt;br /&gt;
storage.py&lt;br /&gt;
services.py&lt;br /&gt;
main.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For every module, write one sentence describing its responsibility.&lt;br /&gt;
&lt;br /&gt;
== 71. Exercise 10 — Mini-project structure ==&lt;br /&gt;
&lt;br /&gt;
Create the following project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
grade_manager/&lt;br /&gt;
├── pyproject.toml&lt;br /&gt;
├── README.md&lt;br /&gt;
├── .gitignore&lt;br /&gt;
├── src/&lt;br /&gt;
│   └── grade_manager/&lt;br /&gt;
│       ├── __init__.py&lt;br /&gt;
│       ├── models.py&lt;br /&gt;
│       ├── storage.py&lt;br /&gt;
│       ├── services.py&lt;br /&gt;
│       └── main.py&lt;br /&gt;
└── data/&lt;br /&gt;
    └── students.json&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Requirements:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;Student&amp;lt;/code&amp;gt; must be a dataclass;&lt;br /&gt;
* storage must use JSON;&lt;br /&gt;
* paths must use &amp;lt;code&amp;gt;pathlib&amp;lt;/code&amp;gt;;&lt;br /&gt;
* invalid grades must raise a custom exception;&lt;br /&gt;
* functions must use type hints;&lt;br /&gt;
* the program must calculate the class average;&lt;br /&gt;
* the main module must not contain storage implementation details.&lt;br /&gt;
&lt;br /&gt;
== 72. Summary ==&lt;br /&gt;
&lt;br /&gt;
The main mappings introduced in this laboratory are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
! Python&lt;br /&gt;
|-&lt;br /&gt;
| source/header organization&lt;br /&gt;
| modules and packages&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;#include&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;import&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| namespace&lt;br /&gt;
| module/package namespace&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;if __name__ == &amp;quot;__main__&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::ifstream&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;open(..., &amp;quot;r&amp;quot;)&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::ofstream&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;open(..., &amp;quot;w&amp;quot;)&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| RAII resource cleanup&lt;br /&gt;
| context manager / &amp;lt;code&amp;gt;with&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::filesystem::path&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;pathlib.Path&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;throw&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;raise&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;catch&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;except&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| custom exception class&lt;br /&gt;
| subclass of &amp;lt;code&amp;gt;Exception&amp;lt;/code&amp;gt; or a more specific exception&lt;br /&gt;
|-&lt;br /&gt;
| static type declaration&lt;br /&gt;
| optional type annotations&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::optional&amp;amp;lt;T&amp;amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;T | None&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::variant&amp;lt;/code&amp;gt;&lt;br /&gt;
| union type such as &amp;lt;code&amp;gt;int | str&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;using&amp;lt;/code&amp;gt; alias&lt;br /&gt;
| type alias&lt;br /&gt;
|-&lt;br /&gt;
| build/project metadata&lt;br /&gt;
| &amp;lt;code&amp;gt;pyproject.toml&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The central idea of this laboratory is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
A maintainable Python application is not a single large script. It is organized into modules with clear responsibilities, explicit dependencies, robust error handling and well-defined interfaces.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 73. Preparation for Lab 4 ==&lt;br /&gt;
&lt;br /&gt;
Before the next laboratory:&lt;br /&gt;
&lt;br /&gt;
# complete the exercises from this laboratory;&lt;br /&gt;
# organize the semester project into multiple modules;&lt;br /&gt;
# create a virtual environment for the project;&lt;br /&gt;
# add a &amp;lt;code&amp;gt;.gitignore&amp;lt;/code&amp;gt;;&lt;br /&gt;
# add basic type hints to public functions;&lt;br /&gt;
# separate data models, application logic and input/output code;&lt;br /&gt;
# ensure that expected errors are handled explicitly.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Lab 4&amp;#039;&amp;#039;&amp;#039;, the focus will move to testing, debugging and software quality using tools such as &amp;lt;code&amp;gt;pytest&amp;lt;/code&amp;gt;, assertions, fixtures, logging and code-quality checks.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_2&amp;diff=8371</id>
		<title>Lab 2</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_2&amp;diff=8371"/>
		<updated>2026-09-14T06:01:20Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: Pagină nouă: = Lab 2 — Object-Oriented Programming in Python =  == Objectives ==  This laboratory introduces object-oriented programming in Python to students who already know object-oriented...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 2 — Object-Oriented Programming in Python =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
This laboratory introduces object-oriented programming in Python to students who already know object-oriented programming in C++.&lt;br /&gt;
&lt;br /&gt;
The goal is not to relearn classes, inheritance, encapsulation or polymorphism, but to understand how these concepts are expressed in Python and how Python&amp;#039;s object model differs from C++.&lt;br /&gt;
&lt;br /&gt;
After completing this laboratory, you should be able to:&lt;br /&gt;
&lt;br /&gt;
* define and instantiate Python classes;&lt;br /&gt;
* understand the role of &amp;lt;code&amp;gt;self&amp;lt;/code&amp;gt;;&lt;br /&gt;
* distinguish between instance and class attributes;&lt;br /&gt;
* define constructors and methods;&lt;br /&gt;
* understand Python&amp;#039;s conventions for public, protected and private members;&lt;br /&gt;
* use properties;&lt;br /&gt;
* use class methods and static methods;&lt;br /&gt;
* use inheritance and &amp;lt;code&amp;gt;super()&amp;lt;/code&amp;gt;;&lt;br /&gt;
* understand method overriding and duck typing;&lt;br /&gt;
* prefer composition where appropriate;&lt;br /&gt;
* use &amp;lt;code&amp;gt;@dataclass&amp;lt;/code&amp;gt;;&lt;br /&gt;
* implement common special methods such as &amp;lt;code&amp;gt;__str__&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;__repr__&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;__eq__&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;__len__&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;__iter__&amp;lt;/code&amp;gt;;&lt;br /&gt;
* recognize common C++ habits that should not be translated directly into Python.&lt;br /&gt;
&lt;br /&gt;
Throughout this laboratory, familiar C++ constructs are presented next to their Python equivalents.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== 1. Defining a class ==&lt;br /&gt;
&lt;br /&gt;
A simple class declaration is similar conceptually, but Python uses &amp;lt;code&amp;gt;class&amp;lt;/code&amp;gt; followed by an indented block.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Student {&lt;br /&gt;
public:&lt;br /&gt;
    std::string name;&lt;br /&gt;
    int age;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Student:&lt;br /&gt;
    pass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python class above does not yet define any attributes. In Python, instance attributes are commonly created in the constructor.&lt;br /&gt;
&lt;br /&gt;
== 2. Creating objects ==&lt;br /&gt;
&lt;br /&gt;
Creating an object is straightforward in both languages.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Student student;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
student = Student()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In Python, the class itself is called like a function to create a new instance.&lt;br /&gt;
&lt;br /&gt;
== 3. Constructors ==&lt;br /&gt;
&lt;br /&gt;
In C++, constructors have the same name as the class.&lt;br /&gt;
&lt;br /&gt;
Python uses the special method &amp;lt;code&amp;gt;__init__()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Student {&lt;br /&gt;
public:&lt;br /&gt;
    Student(&lt;br /&gt;
        const std::string&amp;amp; name,&lt;br /&gt;
        int age&lt;br /&gt;
    )&lt;br /&gt;
        : name(name),&lt;br /&gt;
          age(age)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    std::string name;&lt;br /&gt;
    int age;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Student:&lt;br /&gt;
    def __init__(self, name, age):&lt;br /&gt;
        self.name = name&lt;br /&gt;
        self.age = age&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
An object can then be created as follows:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Student student(&amp;quot;Alice&amp;quot;, 21);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
student = Student(&amp;quot;Alice&amp;quot;, 21)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 4. The meaning of self ==&lt;br /&gt;
&lt;br /&gt;
The first parameter of an instance method is conventionally named &amp;lt;code&amp;gt;self&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
It refers to the current object.&lt;br /&gt;
&lt;br /&gt;
Conceptually, it plays a role similar to &amp;lt;code&amp;gt;this&amp;lt;/code&amp;gt; in C++.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Counter {&lt;br /&gt;
public:&lt;br /&gt;
    void increment()&lt;br /&gt;
    {&lt;br /&gt;
        this-&amp;gt;value++;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    int value = 0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Counter:&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        self.value = 0&lt;br /&gt;
&lt;br /&gt;
    def increment(self):&lt;br /&gt;
        self.value += 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
There is one important syntactic difference:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
! Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;this&amp;lt;/code&amp;gt; is implicit in the method parameter list&lt;br /&gt;
| &amp;lt;code&amp;gt;self&amp;lt;/code&amp;gt; is explicitly written as the first parameter&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;this-&amp;amp;gt;value&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;self.value&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
When calling the method, &amp;lt;code&amp;gt;self&amp;lt;/code&amp;gt; is not supplied explicitly:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
counter.increment();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
counter.increment()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python automatically passes the object as the first argument.&lt;br /&gt;
&lt;br /&gt;
== 5. Instance attributes ==&lt;br /&gt;
&lt;br /&gt;
Instance attributes belong to individual objects.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Student {&lt;br /&gt;
public:&lt;br /&gt;
    Student(std::string name)&lt;br /&gt;
        : name(name)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    std::string name;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Student:&lt;br /&gt;
    def __init__(self, name):&lt;br /&gt;
        self.name = name&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Each instance has its own value:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Student a(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
Student b(&amp;quot;Bob&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a.name &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; b.name &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = Student(&amp;quot;Alice&amp;quot;)&lt;br /&gt;
b = Student(&amp;quot;Bob&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
print(a.name)&lt;br /&gt;
print(b.name)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 6. Class attributes ==&lt;br /&gt;
&lt;br /&gt;
Python class attributes are shared by all instances of the class.&lt;br /&gt;
&lt;br /&gt;
They are similar to static data members in C++.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Student {&lt;br /&gt;
public:&lt;br /&gt;
    static int count;&lt;br /&gt;
&lt;br /&gt;
    Student()&lt;br /&gt;
    {&lt;br /&gt;
        count++;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
int Student::count = 0;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Student:&lt;br /&gt;
    count = 0&lt;br /&gt;
&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        Student.count += 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Usage:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Student a;&lt;br /&gt;
Student b;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; Student::count;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = Student()&lt;br /&gt;
b = Student()&lt;br /&gt;
&lt;br /&gt;
print(Student.count)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Class attributes should normally be accessed through the class when they represent class-wide state.&lt;br /&gt;
&lt;br /&gt;
== 7. Instance methods ==&lt;br /&gt;
&lt;br /&gt;
Instance methods operate on one particular object.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Rectangle {&lt;br /&gt;
public:&lt;br /&gt;
    Rectangle(double width, double height)&lt;br /&gt;
        : width(width), height(height)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    double area() const&lt;br /&gt;
    {&lt;br /&gt;
        return width * height;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Rectangle:&lt;br /&gt;
    def __init__(self, width, height):&lt;br /&gt;
        self.width = width&lt;br /&gt;
        self.height = height&lt;br /&gt;
&lt;br /&gt;
    def area(self):&lt;br /&gt;
        return self.width * self.height&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Calling the method:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Rectangle r(3.0, 4.0);&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; r.area();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
r = Rectangle(3.0, 4.0)&lt;br /&gt;
&lt;br /&gt;
print(r.area())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 8. Public, protected and private members ==&lt;br /&gt;
&lt;br /&gt;
C++ enforces access control through &amp;lt;code&amp;gt;public&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;protected&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python primarily uses naming conventions.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Meaning&lt;br /&gt;
! C++&lt;br /&gt;
! Python convention&lt;br /&gt;
|-&lt;br /&gt;
| public&lt;br /&gt;
| &amp;lt;code&amp;gt;public:&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| internal / protected-like&lt;br /&gt;
| &amp;lt;code&amp;gt;protected:&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;_name&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| private&lt;br /&gt;
| &amp;lt;code&amp;gt;private:&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;__name&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Account {&lt;br /&gt;
public:&lt;br /&gt;
    std::string owner;&lt;br /&gt;
&lt;br /&gt;
protected:&lt;br /&gt;
    int account_id;&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    double balance;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Account:&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        self.owner = &amp;quot;&amp;quot;&lt;br /&gt;
        self._account_id = 0&lt;br /&gt;
        self.__balance = 0.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A single leading underscore means:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
This member is intended for internal use.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is a convention, not an access restriction.&lt;br /&gt;
&lt;br /&gt;
== 9. Name mangling ==&lt;br /&gt;
&lt;br /&gt;
Names beginning with two underscores are transformed internally by Python.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Account {&lt;br /&gt;
private:&lt;br /&gt;
    double balance = 0.0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Account:&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        self.__balance = 0.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python internally transforms:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
__balance&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
into a name similar to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
_Account__balance&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mechanism is called &amp;#039;&amp;#039;&amp;#039;name mangling&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
It is primarily designed to avoid accidental name conflicts in subclasses. It is not a security mechanism.&lt;br /&gt;
&lt;br /&gt;
== 10. Getters and setters ==&lt;br /&gt;
&lt;br /&gt;
A direct C++ translation might use explicit getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python — direct style&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Person {&lt;br /&gt;
public:&lt;br /&gt;
    int getAge() const&lt;br /&gt;
    {&lt;br /&gt;
        return age;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    void setAge(int value)&lt;br /&gt;
    {&lt;br /&gt;
        if (value &amp;lt; 0) {&lt;br /&gt;
            throw std::invalid_argument(&lt;br /&gt;
                &amp;quot;age cannot be negative&amp;quot;&lt;br /&gt;
            );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        age = value;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    int age = 0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Person:&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        self._age = 0&lt;br /&gt;
&lt;br /&gt;
    def get_age(self):&lt;br /&gt;
        return self._age&lt;br /&gt;
&lt;br /&gt;
    def set_age(self, value):&lt;br /&gt;
        if value &amp;lt; 0:&lt;br /&gt;
            raise ValueError(&lt;br /&gt;
                &amp;quot;age cannot be negative&amp;quot;&lt;br /&gt;
            )&lt;br /&gt;
&lt;br /&gt;
        self._age = value&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python version works, but explicit getters and setters are usually not the preferred interface.&lt;br /&gt;
&lt;br /&gt;
== 11. Properties ==&lt;br /&gt;
&lt;br /&gt;
Python properties provide controlled attribute access while keeping normal attribute syntax.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Person {&lt;br /&gt;
public:&lt;br /&gt;
    int getAge() const&lt;br /&gt;
    {&lt;br /&gt;
        return age;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    void setAge(int value)&lt;br /&gt;
    {&lt;br /&gt;
        if (value &amp;lt; 0) {&lt;br /&gt;
            throw std::invalid_argument(&lt;br /&gt;
                &amp;quot;age cannot be negative&amp;quot;&lt;br /&gt;
            );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        age = value;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    int age = 0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Person:&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        self._age = 0&lt;br /&gt;
&lt;br /&gt;
    @property&lt;br /&gt;
    def age(self):&lt;br /&gt;
        return self._age&lt;br /&gt;
&lt;br /&gt;
    @age.setter&lt;br /&gt;
    def age(self, value):&lt;br /&gt;
        if value &amp;lt; 0:&lt;br /&gt;
            raise ValueError(&lt;br /&gt;
                &amp;quot;age cannot be negative&amp;quot;&lt;br /&gt;
            )&lt;br /&gt;
&lt;br /&gt;
        self._age = value&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Usage:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Person p;&lt;br /&gt;
&lt;br /&gt;
p.setAge(21);&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; p.getAge();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
p = Person()&lt;br /&gt;
&lt;br /&gt;
p.age = 21&lt;br /&gt;
&lt;br /&gt;
print(p.age)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python interface looks like direct attribute access even though validation is performed internally.&lt;br /&gt;
&lt;br /&gt;
== 12. Do not create properties unnecessarily ==&lt;br /&gt;
&lt;br /&gt;
In C++, private data members with getters and setters are common.&lt;br /&gt;
&lt;br /&gt;
In Python, plain public attributes are acceptable when no validation or additional behavior is required.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++ style&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python style&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Point {&lt;br /&gt;
public:&lt;br /&gt;
    double getX() const&lt;br /&gt;
    {&lt;br /&gt;
        return x;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    void setX(double value)&lt;br /&gt;
    {&lt;br /&gt;
        x = value;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    double x = 0.0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Point:&lt;br /&gt;
    def __init__(self, x=0.0):&lt;br /&gt;
        self.x = x&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A property can always be introduced later without changing how callers access the attribute.&lt;br /&gt;
&lt;br /&gt;
== 13. Static methods ==&lt;br /&gt;
&lt;br /&gt;
A static method does not operate on a particular instance.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Math {&lt;br /&gt;
public:&lt;br /&gt;
    static int square(int x)&lt;br /&gt;
    {&lt;br /&gt;
        return x * x;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Math:&lt;br /&gt;
    @staticmethod&lt;br /&gt;
    def square(x):&lt;br /&gt;
        return x * x&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Usage:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int result = Math::square(5);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
result = Math.square(5)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A static method receives neither &amp;lt;code&amp;gt;self&amp;lt;/code&amp;gt; nor &amp;lt;code&amp;gt;cls&amp;lt;/code&amp;gt; automatically.&lt;br /&gt;
&lt;br /&gt;
== 14. Class methods ==&lt;br /&gt;
&lt;br /&gt;
Python also provides class methods.&lt;br /&gt;
&lt;br /&gt;
A class method receives the class itself as its first argument, conventionally called &amp;lt;code&amp;gt;cls&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A common use is defining alternative constructors.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Point {&lt;br /&gt;
public:&lt;br /&gt;
    Point(double x, double y)&lt;br /&gt;
        : x(x), y(y)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    static Point origin()&lt;br /&gt;
    {&lt;br /&gt;
        return Point(0.0, 0.0);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    double x;&lt;br /&gt;
    double y;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Point:&lt;br /&gt;
    def __init__(self, x, y):&lt;br /&gt;
        self.x = x&lt;br /&gt;
        self.y = y&lt;br /&gt;
&lt;br /&gt;
    @classmethod&lt;br /&gt;
    def origin(cls):&lt;br /&gt;
        return cls(0.0, 0.0)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Usage:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Point p = Point::origin();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
p = Point.origin()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Using &amp;lt;code&amp;gt;cls&amp;lt;/code&amp;gt; rather than the explicit class name makes the method behave correctly when inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
== 15. Instance method vs class method vs static method ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Method type&lt;br /&gt;
! First automatic argument&lt;br /&gt;
! Typical purpose&lt;br /&gt;
|-&lt;br /&gt;
| instance method&lt;br /&gt;
| &amp;lt;code&amp;gt;self&amp;lt;/code&amp;gt;&lt;br /&gt;
| operate on an object&lt;br /&gt;
|-&lt;br /&gt;
| class method&lt;br /&gt;
| &amp;lt;code&amp;gt;cls&amp;lt;/code&amp;gt;&lt;br /&gt;
| operate on the class / alternative constructors&lt;br /&gt;
|-&lt;br /&gt;
| static method&lt;br /&gt;
| none&lt;br /&gt;
| utility logically associated with the class&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Example:&lt;br /&gt;
    def instance_method(self):&lt;br /&gt;
        ...&lt;br /&gt;
&lt;br /&gt;
    @classmethod&lt;br /&gt;
    def class_method(cls):&lt;br /&gt;
        ...&lt;br /&gt;
&lt;br /&gt;
    @staticmethod&lt;br /&gt;
    def static_method():&lt;br /&gt;
        ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 16. Inheritance ==&lt;br /&gt;
&lt;br /&gt;
Inheritance is declared by placing the base class in parentheses.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Animal {&lt;br /&gt;
public:&lt;br /&gt;
    void eat()&lt;br /&gt;
    {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; &amp;quot;Eating\n&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Dog : public Animal {&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Animal:&lt;br /&gt;
    def eat(self):&lt;br /&gt;
        print(&amp;quot;Eating&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Dog(Animal):&lt;br /&gt;
    pass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The subclass inherits members from the base class.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Dog dog;&lt;br /&gt;
dog.eat();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
dog = Dog()&lt;br /&gt;
dog.eat()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 17. Calling the base-class constructor ==&lt;br /&gt;
&lt;br /&gt;
C++ usually invokes a base-class constructor through the initializer list.&lt;br /&gt;
&lt;br /&gt;
Python commonly uses &amp;lt;code&amp;gt;super()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Person {&lt;br /&gt;
public:&lt;br /&gt;
    Person(std::string name)&lt;br /&gt;
        : name(name)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
protected:&lt;br /&gt;
    std::string name;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Student : public Person {&lt;br /&gt;
public:&lt;br /&gt;
    Student(&lt;br /&gt;
        std::string name,&lt;br /&gt;
        int year&lt;br /&gt;
    )&lt;br /&gt;
        : Person(name),&lt;br /&gt;
          year(year)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    int year;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Person:&lt;br /&gt;
    def __init__(self, name):&lt;br /&gt;
        self.name = name&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Student(Person):&lt;br /&gt;
    def __init__(self, name, year):&lt;br /&gt;
        super().__init__(name)&lt;br /&gt;
        self.year = year&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 18. Method overriding ==&lt;br /&gt;
&lt;br /&gt;
A subclass can provide its own implementation of an inherited method.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Animal {&lt;br /&gt;
public:&lt;br /&gt;
    virtual void speak() const&lt;br /&gt;
    {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; &amp;quot;Animal\n&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Dog : public Animal {&lt;br /&gt;
public:&lt;br /&gt;
    void speak() const override&lt;br /&gt;
    {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; &amp;quot;Woof\n&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Animal:&lt;br /&gt;
    def speak(self):&lt;br /&gt;
        print(&amp;quot;Animal&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Dog(Animal):&lt;br /&gt;
    def speak(self):&lt;br /&gt;
        print(&amp;quot;Woof&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not require &amp;lt;code&amp;gt;virtual&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;override&amp;lt;/code&amp;gt; keywords.&lt;br /&gt;
&lt;br /&gt;
Method lookup is dynamic.&lt;br /&gt;
&lt;br /&gt;
== 19. Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
The same interface can be used with objects of different classes.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void makeSpeak(const Animal&amp;amp; animal)&lt;br /&gt;
{&lt;br /&gt;
    animal.speak();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Dog dog;&lt;br /&gt;
makeSpeak(dog);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def make_speak(animal):&lt;br /&gt;
    animal.speak()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dog = Dog()&lt;br /&gt;
make_speak(dog)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In Python, the object does not necessarily need to inherit from a particular base class.&lt;br /&gt;
&lt;br /&gt;
== 20. Duck typing ==&lt;br /&gt;
&lt;br /&gt;
Python often relies on &amp;#039;&amp;#039;&amp;#039;duck typing&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
If an object provides the operations that are required, its exact class may not matter.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++ — inheritance-oriented&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python — duck typing&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Speaker {&lt;br /&gt;
public:&lt;br /&gt;
    virtual void speak() const = 0;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void makeSpeak(const Speaker&amp;amp; s)&lt;br /&gt;
{&lt;br /&gt;
    s.speak();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def make_speak(obj):&lt;br /&gt;
    obj.speak()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Dog:&lt;br /&gt;
    def speak(self):&lt;br /&gt;
        print(&amp;quot;Woof&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Robot:&lt;br /&gt;
    def speak(self):&lt;br /&gt;
        print(&amp;quot;Beep&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
make_speak(Dog())&lt;br /&gt;
make_speak(Robot())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;Dog&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Robot&amp;lt;/code&amp;gt; do not need a common Python base class for this function to work.&lt;br /&gt;
&lt;br /&gt;
== 21. isinstance() ==&lt;br /&gt;
&lt;br /&gt;
When an explicit runtime type check is actually required, Python provides &amp;lt;code&amp;gt;isinstance()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Animal* animal = ...;&lt;br /&gt;
&lt;br /&gt;
if (dynamic_cast&amp;lt;Dog*&amp;gt;(animal) != nullptr) {&lt;br /&gt;
    // It is a Dog.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if isinstance(animal, Dog):&lt;br /&gt;
    # It is a Dog.&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
However, frequent explicit type checks can indicate that polymorphism or another design should be used instead.&lt;br /&gt;
&lt;br /&gt;
== 22. Composition ==&lt;br /&gt;
&lt;br /&gt;
Composition means constructing a class from other objects rather than inheriting behavior.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Engine {&lt;br /&gt;
public:&lt;br /&gt;
    void start()&lt;br /&gt;
    {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; &amp;quot;Engine started\n&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Car {&lt;br /&gt;
public:&lt;br /&gt;
    void start()&lt;br /&gt;
    {&lt;br /&gt;
        engine.start();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    Engine engine;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Engine:&lt;br /&gt;
    def start(self):&lt;br /&gt;
        print(&amp;quot;Engine started&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Car:&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        self.engine = Engine()&lt;br /&gt;
&lt;br /&gt;
    def start(self):&lt;br /&gt;
        self.engine.start()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Composition is often preferable when the relationship is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
has-a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
rather than:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
is-a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 23. Dataclasses ==&lt;br /&gt;
&lt;br /&gt;
Many classes exist primarily to store data.&lt;br /&gt;
&lt;br /&gt;
In C++, such classes or structures often require explicit constructors and comparison code.&lt;br /&gt;
&lt;br /&gt;
Python provides &amp;lt;code&amp;gt;@dataclass&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
struct Student {&lt;br /&gt;
    std::string name;&lt;br /&gt;
    int age;&lt;br /&gt;
&lt;br /&gt;
    Student(&lt;br /&gt;
        std::string name,&lt;br /&gt;
        int age&lt;br /&gt;
    )&lt;br /&gt;
        : name(name),&lt;br /&gt;
          age(age)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from dataclasses import dataclass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@dataclass&lt;br /&gt;
class Student:&lt;br /&gt;
    name: str&lt;br /&gt;
    age: int&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Creation is the same as for a normal class:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Student s(&amp;quot;Alice&amp;quot;, 21);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
s = Student(&amp;quot;Alice&amp;quot;, 21)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The dataclass automatically generates useful methods such as &amp;lt;code&amp;gt;__init__()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;__repr__()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 24. Dataclass equality ==&lt;br /&gt;
&lt;br /&gt;
Dataclasses also provide value-based equality by default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
struct Point {&lt;br /&gt;
    int x;&lt;br /&gt;
    int y;&lt;br /&gt;
&lt;br /&gt;
    bool operator==(const Point&amp;amp; other) const&lt;br /&gt;
    {&lt;br /&gt;
        return x == other.x &amp;amp;&amp;amp;&lt;br /&gt;
               y == other.y;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from dataclasses import dataclass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@dataclass&lt;br /&gt;
class Point:&lt;br /&gt;
    x: int&lt;br /&gt;
    y: int&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Now:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = Point(1, 2)&lt;br /&gt;
b = Point(1, 2)&lt;br /&gt;
&lt;br /&gt;
print(a == b)  # True&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 25. Special methods ==&lt;br /&gt;
&lt;br /&gt;
Python classes can implement special methods, often called &amp;#039;&amp;#039;&amp;#039;dunder methods&amp;#039;&amp;#039;&amp;#039; because their names begin and end with two underscores.&lt;br /&gt;
&lt;br /&gt;
They allow user-defined objects to participate in normal Python syntax.&lt;br /&gt;
&lt;br /&gt;
Examples include:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Python method&lt;br /&gt;
! Purpose&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;__init__&amp;lt;/code&amp;gt;&lt;br /&gt;
| initialize an object&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;__str__&amp;lt;/code&amp;gt;&lt;br /&gt;
| user-friendly string representation&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;__repr__&amp;lt;/code&amp;gt;&lt;br /&gt;
| developer-oriented representation&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;__eq__&amp;lt;/code&amp;gt;&lt;br /&gt;
| equality comparison&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;__len__&amp;lt;/code&amp;gt;&lt;br /&gt;
| support &amp;lt;code&amp;gt;len(obj)&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;__iter__&amp;lt;/code&amp;gt;&lt;br /&gt;
| allow iteration&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;__contains__&amp;lt;/code&amp;gt;&lt;br /&gt;
| support &amp;lt;code&amp;gt;x in obj&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 26. __str__() ==&lt;br /&gt;
&lt;br /&gt;
C++ commonly overloads &amp;lt;code&amp;gt;operator&amp;amp;lt;&amp;amp;lt;&amp;lt;/code&amp;gt; for printable objects.&lt;br /&gt;
&lt;br /&gt;
Python commonly implements &amp;lt;code&amp;gt;__str__()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Point {&lt;br /&gt;
public:&lt;br /&gt;
    Point(int x, int y)&lt;br /&gt;
        : x(x), y(y)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    friend std::ostream&amp;amp; operator&amp;lt;&amp;lt;(&lt;br /&gt;
        std::ostream&amp;amp; os,&lt;br /&gt;
        const Point&amp;amp; p&lt;br /&gt;
    )&lt;br /&gt;
    {&lt;br /&gt;
        return os&lt;br /&gt;
            &amp;lt;&amp;lt; &amp;quot;(&amp;quot;&lt;br /&gt;
            &amp;lt;&amp;lt; p.x&lt;br /&gt;
            &amp;lt;&amp;lt; &amp;quot;, &amp;quot;&lt;br /&gt;
            &amp;lt;&amp;lt; p.y&lt;br /&gt;
            &amp;lt;&amp;lt; &amp;quot;)&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    int x;&lt;br /&gt;
    int y;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Point:&lt;br /&gt;
    def __init__(self, x, y):&lt;br /&gt;
        self.x = x&lt;br /&gt;
        self.y = y&lt;br /&gt;
&lt;br /&gt;
    def __str__(self):&lt;br /&gt;
        return f&amp;quot;({self.x}, {self.y})&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Usage:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Point p(2, 3);&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; p;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
p = Point(2, 3)&lt;br /&gt;
&lt;br /&gt;
print(p)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 27. __repr__() ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;__repr__()&amp;lt;/code&amp;gt; should normally return a representation useful to a programmer.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// No exact language-level equivalent.&lt;br /&gt;
// Usually implemented through&lt;br /&gt;
// debug output or operator&amp;lt;&amp;lt;.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Point:&lt;br /&gt;
    def __init__(self, x, y):&lt;br /&gt;
        self.x = x&lt;br /&gt;
        self.y = y&lt;br /&gt;
&lt;br /&gt;
    def __repr__(self):&lt;br /&gt;
        return (&lt;br /&gt;
            f&amp;quot;Point(x={self.x}, &amp;quot;&lt;br /&gt;
            f&amp;quot;y={self.y})&amp;quot;&lt;br /&gt;
        )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
p = Point(2, 3)&lt;br /&gt;
&lt;br /&gt;
print(repr(p))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Point(x=2, y=3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 28. __eq__() ==&lt;br /&gt;
&lt;br /&gt;
Python uses &amp;lt;code&amp;gt;__eq__()&amp;lt;/code&amp;gt; to define the behavior of &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Point {&lt;br /&gt;
public:&lt;br /&gt;
    bool operator==(const Point&amp;amp; other) const&lt;br /&gt;
    {&lt;br /&gt;
        return x == other.x &amp;amp;&amp;amp;&lt;br /&gt;
               y == other.y;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    int x;&lt;br /&gt;
    int y;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Point:&lt;br /&gt;
    def __init__(self, x, y):&lt;br /&gt;
        self.x = x&lt;br /&gt;
        self.y = y&lt;br /&gt;
&lt;br /&gt;
    def __eq__(self, other):&lt;br /&gt;
        if not isinstance(other, Point):&lt;br /&gt;
            return NotImplemented&lt;br /&gt;
&lt;br /&gt;
        return (&lt;br /&gt;
            self.x == other.x&lt;br /&gt;
            and self.y == other.y&lt;br /&gt;
        )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In many simple data-holding classes, using &amp;lt;code&amp;gt;@dataclass&amp;lt;/code&amp;gt; avoids writing this manually.&lt;br /&gt;
&lt;br /&gt;
== 29. __len__() ==&lt;br /&gt;
&lt;br /&gt;
A custom class can define what &amp;lt;code&amp;gt;len(obj)&amp;lt;/code&amp;gt; means.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Course {&lt;br /&gt;
public:&lt;br /&gt;
    std::size_t size() const&lt;br /&gt;
    {&lt;br /&gt;
        return students.size();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    std::vector&amp;lt;std::string&amp;gt; students;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Course:&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        self.students = []&lt;br /&gt;
&lt;br /&gt;
    def __len__(self):&lt;br /&gt;
        return len(self.students)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Usage:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; course.size();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(len(course))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 30. __iter__() ==&lt;br /&gt;
&lt;br /&gt;
Implementing &amp;lt;code&amp;gt;__iter__()&amp;lt;/code&amp;gt; allows an object to be used directly in a &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; loop.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Course {&lt;br /&gt;
public:&lt;br /&gt;
    auto begin()&lt;br /&gt;
    {&lt;br /&gt;
        return students.begin();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    auto end()&lt;br /&gt;
    {&lt;br /&gt;
        return students.end();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    std::vector&amp;lt;std::string&amp;gt; students;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Course:&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        self.students = []&lt;br /&gt;
&lt;br /&gt;
    def __iter__(self):&lt;br /&gt;
        return iter(self.students)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Now the class can be used naturally:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (const auto&amp;amp; student : course) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; student &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for student in course:&lt;br /&gt;
    print(student)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 31. Operator overloading ==&lt;br /&gt;
&lt;br /&gt;
Python maps operators to special methods.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Operation&lt;br /&gt;
! C++&lt;br /&gt;
! Python&lt;br /&gt;
|-&lt;br /&gt;
| addition&lt;br /&gt;
| &amp;lt;code&amp;gt;operator+&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;__add__&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| subtraction&lt;br /&gt;
| &amp;lt;code&amp;gt;operator-&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;__sub__&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| multiplication&lt;br /&gt;
| &amp;lt;code&amp;gt;operator*&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;__mul__&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| equality&lt;br /&gt;
| &amp;lt;code&amp;gt;operator==&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;__eq__&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| less than&lt;br /&gt;
| &amp;lt;code&amp;gt;operator&amp;amp;lt;&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;__lt__&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Vector2D {&lt;br /&gt;
public:&lt;br /&gt;
    Vector2D(double x, double y)&lt;br /&gt;
        : x(x), y(y)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    Vector2D operator+(&lt;br /&gt;
        const Vector2D&amp;amp; other&lt;br /&gt;
    ) const&lt;br /&gt;
    {&lt;br /&gt;
        return Vector2D(&lt;br /&gt;
            x + other.x,&lt;br /&gt;
            y + other.y&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    double x;&lt;br /&gt;
    double y;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Vector2D:&lt;br /&gt;
    def __init__(self, x, y):&lt;br /&gt;
        self.x = x&lt;br /&gt;
        self.y = y&lt;br /&gt;
&lt;br /&gt;
    def __add__(self, other):&lt;br /&gt;
        return Vector2D(&lt;br /&gt;
            self.x + other.x,&lt;br /&gt;
            self.y + other.y,&lt;br /&gt;
        )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = Vector2D(1, 2)&lt;br /&gt;
b = Vector2D(3, 4)&lt;br /&gt;
&lt;br /&gt;
c = a + b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 32. Attributes can be added dynamically ==&lt;br /&gt;
&lt;br /&gt;
Python objects are usually more dynamic than C++ objects.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Student {&lt;br /&gt;
public:&lt;br /&gt;
    std::string name;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
Student s;&lt;br /&gt;
&lt;br /&gt;
// Not allowed unless &amp;quot;grade&amp;quot;&lt;br /&gt;
// was declared in Student.&lt;br /&gt;
// s.grade = 10;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Student:&lt;br /&gt;
    pass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
s = Student()&lt;br /&gt;
&lt;br /&gt;
s.name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
s.grade = 10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Although Python permits this, creating expected instance attributes in &amp;lt;code&amp;gt;__init__()&amp;lt;/code&amp;gt; is generally clearer.&lt;br /&gt;
&lt;br /&gt;
== 33. Type hints in classes ==&lt;br /&gt;
&lt;br /&gt;
Python allows type hints for attributes and methods.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Student {&lt;br /&gt;
public:&lt;br /&gt;
    Student(&lt;br /&gt;
        std::string name,&lt;br /&gt;
        int year&lt;br /&gt;
    )&lt;br /&gt;
        : name(name),&lt;br /&gt;
          year(year)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    std::string description() const&lt;br /&gt;
    {&lt;br /&gt;
        return name;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    std::string name;&lt;br /&gt;
    int year;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Student:&lt;br /&gt;
    def __init__(&lt;br /&gt;
        self,&lt;br /&gt;
        name: str,&lt;br /&gt;
        year: int,&lt;br /&gt;
    ) -&amp;gt; None:&lt;br /&gt;
        self.name: str = name&lt;br /&gt;
        self.year: int = year&lt;br /&gt;
&lt;br /&gt;
    def description(self) -&amp;gt; str:&lt;br /&gt;
        return self.name&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Type hints improve readability and allow static analysis tools to detect some errors before runtime.&lt;br /&gt;
&lt;br /&gt;
== 34. Example — Course and Student classes ==&lt;br /&gt;
&lt;br /&gt;
Consider a small system containing students and a course.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Student {&lt;br /&gt;
public:&lt;br /&gt;
    Student(&lt;br /&gt;
        std::string name,&lt;br /&gt;
        double grade&lt;br /&gt;
    )&lt;br /&gt;
        : name(name),&lt;br /&gt;
          grade(grade)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    std::string getName() const&lt;br /&gt;
    {&lt;br /&gt;
        return name;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    double getGrade() const&lt;br /&gt;
    {&lt;br /&gt;
        return grade;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    std::string name;&lt;br /&gt;
    double grade;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Course {&lt;br /&gt;
public:&lt;br /&gt;
    void addStudent(const Student&amp;amp; student)&lt;br /&gt;
    {&lt;br /&gt;
        students.push_back(student);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    double average() const&lt;br /&gt;
    {&lt;br /&gt;
        double sum = 0.0;&lt;br /&gt;
&lt;br /&gt;
        for (const auto&amp;amp; student : students) {&lt;br /&gt;
            sum += student.getGrade();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return sum / students.size();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    std::vector&amp;lt;Student&amp;gt; students;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from dataclasses import dataclass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@dataclass&lt;br /&gt;
class Student:&lt;br /&gt;
    name: str&lt;br /&gt;
    grade: float&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Course:&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        self.students = []&lt;br /&gt;
&lt;br /&gt;
    def add_student(self, student):&lt;br /&gt;
        self.students.append(student)&lt;br /&gt;
&lt;br /&gt;
    def average(self):&lt;br /&gt;
        total = sum(&lt;br /&gt;
            student.grade&lt;br /&gt;
            for student in self.students&lt;br /&gt;
        )&lt;br /&gt;
&lt;br /&gt;
        return total / len(self.students)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python implementation uses:&lt;br /&gt;
&lt;br /&gt;
* a dataclass for the simple data-holding &amp;lt;code&amp;gt;Student&amp;lt;/code&amp;gt; class;&lt;br /&gt;
* direct attribute access;&lt;br /&gt;
* a normal class for &amp;lt;code&amp;gt;Course&amp;lt;/code&amp;gt;;&lt;br /&gt;
* a generator expression for calculating the total.&lt;br /&gt;
&lt;br /&gt;
== 35. Example — Adding Python protocol behavior ==&lt;br /&gt;
&lt;br /&gt;
We can make the &amp;lt;code&amp;gt;Course&amp;lt;/code&amp;gt; class behave more naturally in Python.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Conventional explicit API&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Pythonic API&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Course:&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        self.students = []&lt;br /&gt;
&lt;br /&gt;
    def get_student_count(self):&lt;br /&gt;
        return len(self.students)&lt;br /&gt;
&lt;br /&gt;
    def get_students(self):&lt;br /&gt;
        return self.students&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Course:&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        self.students = []&lt;br /&gt;
&lt;br /&gt;
    def __len__(self):&lt;br /&gt;
        return len(self.students)&lt;br /&gt;
&lt;br /&gt;
    def __iter__(self):&lt;br /&gt;
        return iter(self.students)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The second version allows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(len(course))&lt;br /&gt;
&lt;br /&gt;
for student in course:&lt;br /&gt;
    print(student)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a recurring Python design principle:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Prefer implementing the protocol expected by Python instead of inventing unnecessary custom method names.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 36. C++ habits to reconsider ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C++ habit&lt;br /&gt;
! Typical Python approach&lt;br /&gt;
|-&lt;br /&gt;
| create getters and setters for every field&lt;br /&gt;
| use public attributes unless behavior or validation is required&lt;br /&gt;
|-&lt;br /&gt;
| enforce access through &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;&lt;br /&gt;
| use naming conventions and properties&lt;br /&gt;
|-&lt;br /&gt;
| create a class for every small data structure&lt;br /&gt;
| consider &amp;lt;code&amp;gt;@dataclass&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| require a common base class for polymorphism&lt;br /&gt;
| consider duck typing&lt;br /&gt;
|-&lt;br /&gt;
| create methods such as &amp;lt;code&amp;gt;getSize()&amp;lt;/code&amp;gt;&lt;br /&gt;
| implement &amp;lt;code&amp;gt;__len__()&amp;lt;/code&amp;gt; when appropriate&lt;br /&gt;
|-&lt;br /&gt;
| create methods such as &amp;lt;code&amp;gt;toString()&amp;lt;/code&amp;gt;&lt;br /&gt;
| implement &amp;lt;code&amp;gt;__str__()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| create explicit collection access methods&lt;br /&gt;
| consider &amp;lt;code&amp;gt;__iter__()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| use inheritance to reuse implementation&lt;br /&gt;
| consider composition&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 37. Exercise 1 — Rectangle ==&lt;br /&gt;
&lt;br /&gt;
Implement a &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
The equivalent C++ design is:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Rectangle {&lt;br /&gt;
public:&lt;br /&gt;
    Rectangle(double width, double height)&lt;br /&gt;
        : width(width),&lt;br /&gt;
          height(height)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    double area() const&lt;br /&gt;
    {&lt;br /&gt;
        return width * height;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    double perimeter() const&lt;br /&gt;
    {&lt;br /&gt;
        return 2 * (width + height);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Your implementation:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# TODO&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Requirements:&lt;br /&gt;
&lt;br /&gt;
* store width and height;&lt;br /&gt;
* implement &amp;lt;code&amp;gt;area()&amp;lt;/code&amp;gt;;&lt;br /&gt;
* implement &amp;lt;code&amp;gt;perimeter()&amp;lt;/code&amp;gt;;&lt;br /&gt;
* implement &amp;lt;code&amp;gt;__str__()&amp;lt;/code&amp;gt;;&lt;br /&gt;
* reject negative dimensions using properties.&lt;br /&gt;
&lt;br /&gt;
== 38. Exercise 2 — Bank account ==&lt;br /&gt;
&lt;br /&gt;
Implement a &amp;lt;code&amp;gt;BankAccount&amp;lt;/code&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
The class should contain:&lt;br /&gt;
&lt;br /&gt;
* owner name;&lt;br /&gt;
* account number;&lt;br /&gt;
* current balance.&lt;br /&gt;
&lt;br /&gt;
Implement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
deposit(amount)&lt;br /&gt;
withdraw(amount)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rules:&lt;br /&gt;
&lt;br /&gt;
* deposits must be positive;&lt;br /&gt;
* withdrawals must be positive;&lt;br /&gt;
* a withdrawal cannot exceed the current balance;&lt;br /&gt;
* invalid operations should raise an exception;&lt;br /&gt;
* the balance should not be modified directly from outside the class.&lt;br /&gt;
&lt;br /&gt;
Suggested interface:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++ idea&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python idea&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
public:&lt;br /&gt;
    void deposit(double amount);&lt;br /&gt;
    void withdraw(double amount);&lt;br /&gt;
&lt;br /&gt;
    double getBalance() const;&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    double balance = 0.0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class BankAccount:&lt;br /&gt;
    def deposit(self, amount):&lt;br /&gt;
        ...&lt;br /&gt;
&lt;br /&gt;
    def withdraw(self, amount):&lt;br /&gt;
        ...&lt;br /&gt;
&lt;br /&gt;
    @property&lt;br /&gt;
    def balance(self):&lt;br /&gt;
        ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 39. Exercise 3 — Inheritance ==&lt;br /&gt;
&lt;br /&gt;
Create the following hierarchy:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Shape&lt;br /&gt;
├── Rectangle&lt;br /&gt;
└── Circle&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each shape must provide:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
area()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then write a function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def print_area(shape):&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
that works with either a rectangle or a circle.&lt;br /&gt;
&lt;br /&gt;
Compare the design mentally with the equivalent C++ solution based on a virtual function.&lt;br /&gt;
&lt;br /&gt;
== 40. Exercise 4 — Composition ==&lt;br /&gt;
&lt;br /&gt;
Create:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Engine&lt;br /&gt;
Car&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Engine&amp;lt;/code&amp;gt; class should provide:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
start()&lt;br /&gt;
stop()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; class should &amp;#039;&amp;#039;&amp;#039;contain&amp;#039;&amp;#039;&amp;#039; an &amp;lt;code&amp;gt;Engine&amp;lt;/code&amp;gt; object rather than inherit from &amp;lt;code&amp;gt;Engine&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Implement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
car.start()&lt;br /&gt;
car.stop()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
so that these methods delegate to the contained engine.&lt;br /&gt;
&lt;br /&gt;
== 41. Exercise 5 — Dataclass ==&lt;br /&gt;
&lt;br /&gt;
Create a &amp;lt;code&amp;gt;Student&amp;lt;/code&amp;gt; dataclass containing:&lt;br /&gt;
&lt;br /&gt;
* name;&lt;br /&gt;
* student ID;&lt;br /&gt;
* year;&lt;br /&gt;
* grade.&lt;br /&gt;
&lt;br /&gt;
Then:&lt;br /&gt;
&lt;br /&gt;
# create at least three students;&lt;br /&gt;
# place them in a list;&lt;br /&gt;
# print them;&lt;br /&gt;
# compare two students using &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;;&lt;br /&gt;
# determine the student with the highest grade.&lt;br /&gt;
&lt;br /&gt;
Start from:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from dataclasses import dataclass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@dataclass&lt;br /&gt;
class Student:&lt;br /&gt;
    # TODO&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 42. Exercise 6 — Custom collection ==&lt;br /&gt;
&lt;br /&gt;
Implement a &amp;lt;code&amp;gt;Course&amp;lt;/code&amp;gt; class that stores &amp;lt;code&amp;gt;Student&amp;lt;/code&amp;gt; objects.&lt;br /&gt;
&lt;br /&gt;
The class should support:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
course.add(student)&lt;br /&gt;
&lt;br /&gt;
len(course)&lt;br /&gt;
&lt;br /&gt;
for student in course:&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
print(course)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To achieve this, implement:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;add()&amp;lt;/code&amp;gt;;&lt;br /&gt;
* &amp;lt;code&amp;gt;__len__()&amp;lt;/code&amp;gt;;&lt;br /&gt;
* &amp;lt;code&amp;gt;__iter__()&amp;lt;/code&amp;gt;;&lt;br /&gt;
* &amp;lt;code&amp;gt;__str__()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 43. Exercise 7 — Vector2D ==&lt;br /&gt;
&lt;br /&gt;
Implement a two-dimensional vector class.&lt;br /&gt;
&lt;br /&gt;
The following operations should be possible:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = Vector2D(1, 2)&lt;br /&gt;
b = Vector2D(3, 4)&lt;br /&gt;
&lt;br /&gt;
c = a + b&lt;br /&gt;
&lt;br /&gt;
print(c)&lt;br /&gt;
print(a == b)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Implement:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;__init__()&amp;lt;/code&amp;gt;;&lt;br /&gt;
* &amp;lt;code&amp;gt;__add__()&amp;lt;/code&amp;gt;;&lt;br /&gt;
* &amp;lt;code&amp;gt;__eq__()&amp;lt;/code&amp;gt;;&lt;br /&gt;
* &amp;lt;code&amp;gt;__str__()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The equivalent C++ concepts are constructor definition and operator overloading.&lt;br /&gt;
&lt;br /&gt;
== 44. Exercise 8 — Refactoring C++ style into Python style ==&lt;br /&gt;
&lt;br /&gt;
Consider the following Python class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Student:&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        self._name = &amp;quot;&amp;quot;&lt;br /&gt;
        self._grade = 0.0&lt;br /&gt;
&lt;br /&gt;
    def get_name(self):&lt;br /&gt;
        return self._name&lt;br /&gt;
&lt;br /&gt;
    def set_name(self, name):&lt;br /&gt;
        self._name = name&lt;br /&gt;
&lt;br /&gt;
    def get_grade(self):&lt;br /&gt;
        return self._grade&lt;br /&gt;
&lt;br /&gt;
    def set_grade(self, grade):&lt;br /&gt;
        self._grade = grade&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Refactor it into a more idiomatic Python design.&lt;br /&gt;
&lt;br /&gt;
Questions to consider:&lt;br /&gt;
&lt;br /&gt;
* Are explicit getters necessary?&lt;br /&gt;
* Are explicit setters necessary?&lt;br /&gt;
* Would a dataclass be appropriate?&lt;br /&gt;
* Is validation required?&lt;br /&gt;
* Which attributes should be public?&lt;br /&gt;
&lt;br /&gt;
== 45. Summary ==&lt;br /&gt;
&lt;br /&gt;
The main mappings introduced in this laboratory are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
! Python&lt;br /&gt;
|-&lt;br /&gt;
| constructor&lt;br /&gt;
| &amp;lt;code&amp;gt;__init__()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;this&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;self&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| static data member&lt;br /&gt;
| class attribute&lt;br /&gt;
|-&lt;br /&gt;
| static member function&lt;br /&gt;
| &amp;lt;code&amp;gt;@staticmethod&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| no direct equivalent&lt;br /&gt;
| &amp;lt;code&amp;gt;@classmethod&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| getter / setter&lt;br /&gt;
| often direct attribute access or &amp;lt;code&amp;gt;@property&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;&lt;br /&gt;
| naming convention / name mangling&lt;br /&gt;
|-&lt;br /&gt;
| base-class constructor&lt;br /&gt;
| &amp;lt;code&amp;gt;super().__init__()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| virtual method&lt;br /&gt;
| normal dynamically dispatched method&lt;br /&gt;
|-&lt;br /&gt;
| value-oriented struct&lt;br /&gt;
| &amp;lt;code&amp;gt;@dataclass&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;operator&amp;amp;lt;&amp;amp;lt;&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;__str__()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;operator==&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;__eq__()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;size()&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;__len__()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;begin()&amp;lt;/code&amp;gt; / &amp;lt;code&amp;gt;end()&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;__iter__()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| operator overloading&lt;br /&gt;
| special methods such as &amp;lt;code&amp;gt;__add__()&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The central idea of this laboratory is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Python supports object-oriented programming, but idiomatic Python does not attempt to reproduce C++ class design exactly. Use Python&amp;#039;s object model, conventions and protocols rather than mechanically translating C++ patterns.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 46. Preparation for Lab 3 ==&lt;br /&gt;
&lt;br /&gt;
Before the next laboratory:&lt;br /&gt;
&lt;br /&gt;
# complete the exercises from this laboratory;&lt;br /&gt;
# review Python lists, dictionaries and comprehensions;&lt;br /&gt;
# be comfortable creating classes and methods;&lt;br /&gt;
# understand the difference between instance and class attributes;&lt;br /&gt;
# understand inheritance and composition;&lt;br /&gt;
# review exception handling from Lab 1.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Lab 3&amp;#039;&amp;#039;&amp;#039;, the focus will move from individual classes to the structure of complete Python applications: modules, packages, files, exceptions, type hints and project organization.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Python_Project&amp;diff=8370</id>
		<title>Python Project</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Python_Project&amp;diff=8370"/>
		<updated>2026-09-14T05:54:59Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Python Project */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Python Project =&lt;br /&gt;
&lt;br /&gt;
This course introduces Python programming to students who already have experience with procedural programming in C and object-oriented programming in C++.&lt;br /&gt;
&lt;br /&gt;
The laboratories focus on the differences between C/C++ and Python, Python-specific programming concepts, and the development of a complete software project.&lt;br /&gt;
&lt;br /&gt;
== Laboratories ==&lt;br /&gt;
&lt;br /&gt;
* [[Lab 1|Lab 1 — Python for C/C++ Programmers]]&lt;br /&gt;
* [[Lab 2|Lab 2 — Object-Oriented Programming in Python]]&lt;br /&gt;
* [[Lab 3|Lab 3 — Modules, Packages, Files and Type Hints]]&lt;br /&gt;
* [[Lab 4|Lab 4 — Testing and Software Quality]]&lt;br /&gt;
* [[Lab 5|Lab 5 — Project Development I]]&lt;br /&gt;
* [[Lab 6|Lab 6 — Project Development II]]&lt;br /&gt;
* [[Lab 7|Lab 7 — Final Integration and Presentation]]&lt;br /&gt;
&lt;br /&gt;
== Course structure ==&lt;br /&gt;
&lt;br /&gt;
The course consists of &amp;#039;&amp;#039;&amp;#039;7 laboratory meetings&amp;#039;&amp;#039;&amp;#039;, each with a duration of &amp;#039;&amp;#039;&amp;#039;2 hours&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The first laboratories introduce the Python language and its software ecosystem, while the later meetings are primarily dedicated to the semester project.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Laboratory&lt;br /&gt;
! Main topic&lt;br /&gt;
! Type&lt;br /&gt;
|-&lt;br /&gt;
| [[Lab 1]]&lt;br /&gt;
| Python for C/C++ programmers&lt;br /&gt;
| Theory + exercises&lt;br /&gt;
|-&lt;br /&gt;
| [[Lab 2]]&lt;br /&gt;
| Object-oriented programming and the Python object model&lt;br /&gt;
| Theory + exercises&lt;br /&gt;
|-&lt;br /&gt;
| [[Lab 3]]&lt;br /&gt;
| Modules, packages, files, exceptions and type hints&lt;br /&gt;
| Theory + exercises&lt;br /&gt;
|-&lt;br /&gt;
| [[Lab 4]]&lt;br /&gt;
| Testing, debugging and software quality&lt;br /&gt;
| Theory + project&lt;br /&gt;
|-&lt;br /&gt;
| [[Lab 5]]&lt;br /&gt;
| Project implementation&lt;br /&gt;
| Project work&lt;br /&gt;
|-&lt;br /&gt;
| [[Lab 6]]&lt;br /&gt;
| Testing, refactoring and documentation&lt;br /&gt;
| Project work&lt;br /&gt;
|-&lt;br /&gt;
| [[Lab 7]]&lt;br /&gt;
| Final integration, demonstration and code review&lt;br /&gt;
| Project work + presentation&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Semester project ==&lt;br /&gt;
&lt;br /&gt;
During the course, students will develop a Python software project individually or in teams.&lt;br /&gt;
&lt;br /&gt;
The project should demonstrate the ability to:&lt;br /&gt;
&lt;br /&gt;
* organize a Python project into multiple modules;&lt;br /&gt;
* use appropriate Python data structures and language features;&lt;br /&gt;
* use external libraries where appropriate;&lt;br /&gt;
* handle invalid input and exceptional situations;&lt;br /&gt;
* write readable and maintainable code;&lt;br /&gt;
* use Git for version control;&lt;br /&gt;
* document the application;&lt;br /&gt;
* test the main functionality of the application.&lt;br /&gt;
&lt;br /&gt;
The objective is not simply to translate C/C++ programs into Python, but to learn how to design and implement software using Python&amp;#039;s own abstractions, conventions and ecosystem.&lt;br /&gt;
&lt;br /&gt;
== Recommended software ==&lt;br /&gt;
&lt;br /&gt;
Students should have the following software available:&lt;br /&gt;
&lt;br /&gt;
* Python 3;&lt;br /&gt;
* Git;&lt;br /&gt;
* a text editor or IDE;&lt;br /&gt;
* a terminal;&lt;br /&gt;
* access to the course Git repository.&lt;br /&gt;
&lt;br /&gt;
Recommended development environments include:&lt;br /&gt;
&lt;br /&gt;
* Visual Studio Code;&lt;br /&gt;
* PyCharm;&lt;br /&gt;
* any editor with Python and Git support.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Python_Project&amp;diff=8369</id>
		<title>Python Project</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Python_Project&amp;diff=8369"/>
		<updated>2026-09-14T05:54:24Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Python Project =&lt;br /&gt;
&lt;br /&gt;
This course introduces Python programming to students who already have experience with procedural programming in C and object-oriented programming in C++.&lt;br /&gt;
&lt;br /&gt;
The laboratories focus on the differences between C/C++ and Python, Python-specific programming concepts, and the development of a complete software project.&lt;br /&gt;
&lt;br /&gt;
Throughout the course, familiar C/C++ constructs are compared directly with their Python equivalents.&lt;br /&gt;
&lt;br /&gt;
== Laboratories ==&lt;br /&gt;
&lt;br /&gt;
* [[Lab 1|Lab 1 — Python for C/C++ Programmers]]&lt;br /&gt;
* [[Lab 2|Lab 2 — Object-Oriented Programming in Python]]&lt;br /&gt;
* [[Lab 3|Lab 3 — Modules, Packages, Files and Type Hints]]&lt;br /&gt;
* [[Lab 4|Lab 4 — Testing and Software Quality]]&lt;br /&gt;
* [[Lab 5|Lab 5 — Project Development I]]&lt;br /&gt;
* [[Lab 6|Lab 6 — Project Development II]]&lt;br /&gt;
* [[Lab 7|Lab 7 — Final Integration and Presentation]]&lt;br /&gt;
&lt;br /&gt;
== Course structure ==&lt;br /&gt;
&lt;br /&gt;
The course consists of &amp;#039;&amp;#039;&amp;#039;7 laboratory meetings&amp;#039;&amp;#039;&amp;#039;, each with a duration of &amp;#039;&amp;#039;&amp;#039;2 hours&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The first laboratories introduce the Python language and its software ecosystem, while the later meetings are primarily dedicated to the semester project.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Laboratory&lt;br /&gt;
! Main topic&lt;br /&gt;
! Type&lt;br /&gt;
|-&lt;br /&gt;
| [[Lab 1]]&lt;br /&gt;
| Python for C/C++ programmers&lt;br /&gt;
| Theory + exercises&lt;br /&gt;
|-&lt;br /&gt;
| [[Lab 2]]&lt;br /&gt;
| Object-oriented programming and the Python object model&lt;br /&gt;
| Theory + exercises&lt;br /&gt;
|-&lt;br /&gt;
| [[Lab 3]]&lt;br /&gt;
| Modules, packages, files, exceptions and type hints&lt;br /&gt;
| Theory + exercises&lt;br /&gt;
|-&lt;br /&gt;
| [[Lab 4]]&lt;br /&gt;
| Testing, debugging and software quality&lt;br /&gt;
| Theory + project&lt;br /&gt;
|-&lt;br /&gt;
| [[Lab 5]]&lt;br /&gt;
| Project implementation&lt;br /&gt;
| Project work&lt;br /&gt;
|-&lt;br /&gt;
| [[Lab 6]]&lt;br /&gt;
| Testing, refactoring and documentation&lt;br /&gt;
| Project work&lt;br /&gt;
|-&lt;br /&gt;
| [[Lab 7]]&lt;br /&gt;
| Final integration, demonstration and code review&lt;br /&gt;
| Project work + presentation&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Semester project ==&lt;br /&gt;
&lt;br /&gt;
During the course, students will develop a Python software project individually or in teams.&lt;br /&gt;
&lt;br /&gt;
The project should demonstrate the ability to:&lt;br /&gt;
&lt;br /&gt;
* organize a Python project into multiple modules;&lt;br /&gt;
* use appropriate Python data structures and language features;&lt;br /&gt;
* use external libraries where appropriate;&lt;br /&gt;
* handle invalid input and exceptional situations;&lt;br /&gt;
* write readable and maintainable code;&lt;br /&gt;
* use Git for version control;&lt;br /&gt;
* document the application;&lt;br /&gt;
* test the main functionality of the application.&lt;br /&gt;
&lt;br /&gt;
The objective is not simply to translate C/C++ programs into Python, but to learn how to design and implement software using Python&amp;#039;s own abstractions, conventions and ecosystem.&lt;br /&gt;
&lt;br /&gt;
== Recommended software ==&lt;br /&gt;
&lt;br /&gt;
Students should have the following software available:&lt;br /&gt;
&lt;br /&gt;
* Python 3;&lt;br /&gt;
* Git;&lt;br /&gt;
* a text editor or IDE;&lt;br /&gt;
* a terminal;&lt;br /&gt;
* access to the course Git repository.&lt;br /&gt;
&lt;br /&gt;
Recommended development environments include:&lt;br /&gt;
&lt;br /&gt;
* Visual Studio Code;&lt;br /&gt;
* PyCharm;&lt;br /&gt;
* any editor with Python and Git support.&lt;br /&gt;
&lt;br /&gt;
== Navigation ==&lt;br /&gt;
&lt;br /&gt;
Start with:&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;[[Lab 1|Lab 1 — Python for C/C++ Programmers]]&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8368</id>
		<title>Lab 1</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8368"/>
		<updated>2026-09-14T05:51:19Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Lab 1 — Python for C/C++ Programmers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 1 — Python for C/C++ Programmers =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
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++.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
After completing this laboratory, you should be able to:&lt;br /&gt;
&lt;br /&gt;
* understand the basic syntax of Python;&lt;br /&gt;
* recognize the main differences between Python and C/C++;&lt;br /&gt;
* work with Python&amp;#039;s fundamental data types;&lt;br /&gt;
* use conditional statements and loops;&lt;br /&gt;
* work with lists, dictionaries, sets and tuples;&lt;br /&gt;
* define and call functions;&lt;br /&gt;
* understand basic Python object/reference semantics;&lt;br /&gt;
* recognize several common Python idioms;&lt;br /&gt;
* create and execute a simple Python project.&lt;br /&gt;
&lt;br /&gt;
Throughout this laboratory, equivalent or similar C/C++ and Python code will be presented side by side.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== 1. Running a Python program ==&lt;br /&gt;
&lt;br /&gt;
A C/C++ program normally has to be compiled before it is executed. Python programs are usually executed directly by the Python interpreter.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Hello, world!\n&amp;quot;;&lt;br /&gt;
return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(&amp;quot;Hello, world!&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A Python source file normally uses the extension:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be executed using:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unlike C/C++, Python does not require a special &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
However, larger Python programs commonly use:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Program started\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
    print(&amp;quot;Program started&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python version is not mandatory, but this pattern will become useful when programs are split into multiple modules.&lt;br /&gt;
&lt;br /&gt;
== 2. Blocks and indentation ==&lt;br /&gt;
&lt;br /&gt;
In C and C++, braces delimit blocks of code.&lt;br /&gt;
&lt;br /&gt;
Python uses &amp;#039;&amp;#039;&amp;#039;indentation&amp;#039;&amp;#039;&amp;#039; instead.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (x &amp;gt; 10) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Large\n&amp;quot;;&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Value: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
    print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
    print(&amp;quot;Value:&amp;quot;, x)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Indentation in Python is part of the language syntax.&lt;br /&gt;
&lt;br /&gt;
The standard convention is &amp;#039;&amp;#039;&amp;#039;4 spaces per indentation level&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
For example, the following code is incorrect:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
    print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python will report an &amp;lt;code&amp;gt;IndentationError&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 3. Variables and types ==&lt;br /&gt;
&lt;br /&gt;
C/C++ variables have a declared static type.&lt;br /&gt;
&lt;br /&gt;
Python variables do not require explicit type declarations.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int count = 10;&lt;br /&gt;
double temperature = 23.5;&lt;br /&gt;
bool enabled = true;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
count = 10&lt;br /&gt;
temperature = 23.5&lt;br /&gt;
enabled = True&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python is &amp;#039;&amp;#039;&amp;#039;dynamically typed&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
This means that a variable name can later refer to an object of another type.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
// Not allowed:&lt;br /&gt;
// value = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
value = &amp;quot;hello&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This does &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; mean that Python has no types.&lt;br /&gt;
&lt;br /&gt;
Objects still have types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;typeinfo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; typeid(value).name();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
&lt;br /&gt;
print(type(value))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python will produce:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common basic types ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ type&lt;br /&gt;
! Python equivalent&lt;br /&gt;
! Example&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;42&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt; / &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;3.14&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt;&lt;br /&gt;
| No separate character type&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;A&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;str&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;Hello&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 4. Constants ==&lt;br /&gt;
&lt;br /&gt;
C++ can enforce that a value cannot be modified using &amp;lt;code&amp;gt;const&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python does not enforce constants at language level.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
const double PI = 3.1415926535;&lt;br /&gt;
const int MAX_USERS = 100;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
PI = 3.1415926535&lt;br /&gt;
MAX_USERS = 100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
By convention, Python names that should be treated as constants are written using uppercase letters.&lt;br /&gt;
&lt;br /&gt;
Nothing prevents the programmer from changing them.&lt;br /&gt;
&lt;br /&gt;
== 5. Arithmetic operators ==&lt;br /&gt;
&lt;br /&gt;
Most arithmetic operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
int sum  = a + b;&lt;br /&gt;
int diff = a - b;&lt;br /&gt;
int prod = a * b;&lt;br /&gt;
int div  = a / b;&lt;br /&gt;
int rem  = a % b;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
sum_ = a + b&lt;br /&gt;
diff = a - b&lt;br /&gt;
prod = a * b&lt;br /&gt;
div = a // b&lt;br /&gt;
rem = a % b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
There is an important difference between &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a / b;   // 3&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
print(a / b)   # 3.3333333333333335&lt;br /&gt;
print(a // b)  # 3&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In C/C++, dividing two integers performs integer division.&lt;br /&gt;
&lt;br /&gt;
In Python, the &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; operator always performs floating-point division, while &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt; performs floor division.&lt;br /&gt;
&lt;br /&gt;
Python also provides an exponentiation operator:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;cmath&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double result = std::pow(2, 8);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
result = 2 ** 8&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 6. Printing values ==&lt;br /&gt;
&lt;br /&gt;
Output using &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt; is replaced by the built-in &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
int age = 21;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; is &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; age&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; years old\n&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
age = 21&lt;br /&gt;
&lt;br /&gt;
print(name, &amp;quot;is&amp;quot;, age, &amp;quot;years old&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python also provides formatted string literals, commonly called &amp;#039;&amp;#039;&amp;#039;f-strings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Temperature: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; temperature&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; C\n&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Temperature: {temperature} C&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Expressions can appear directly inside an f-string:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Result: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; a + b&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Result: {a + b}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 7. Reading input ==&lt;br /&gt;
&lt;br /&gt;
Console input is performed using &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An important difference is that &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt; &amp;#039;&amp;#039;&amp;#039;always returns a string&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int age;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Age: &amp;quot;;&lt;br /&gt;
std::cin &amp;gt;&amp;gt; age;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = int(input(&amp;quot;Age: &amp;quot;))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Without the conversion:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = input(&amp;quot;Age: &amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; is a string.&lt;br /&gt;
&lt;br /&gt;
Conversions are explicit:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = std::stoi(text);&lt;br /&gt;
double b = std::stod(text);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = int(text)&lt;br /&gt;
b = float(text)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 8. Conditional statements ==&lt;br /&gt;
&lt;br /&gt;
The overall structure is similar, but Python does not use parentheses or braces.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (temperature &amp;gt; 30) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Hot\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else if (temperature &amp;gt; 20) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Warm\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Cold\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if temperature &amp;gt; 30:&lt;br /&gt;
    print(&amp;quot;Hot&amp;quot;)&lt;br /&gt;
elif temperature &amp;gt; 20:&lt;br /&gt;
    print(&amp;quot;Warm&amp;quot;)&lt;br /&gt;
else:&lt;br /&gt;
    print(&amp;quot;Cold&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python uses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
elif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
instead of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
else if&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 9. Boolean operators ==&lt;br /&gt;
&lt;br /&gt;
Python uses words instead of the C/C++ symbolic logical operators.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;amp;&amp;amp;amp;&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;#124;&amp;amp;#124;&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (age &amp;gt;= 18 &amp;amp;&amp;amp; enabled) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if (!enabled) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if age &amp;gt;= 18 and enabled:&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
if not enabled:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 10. Comparison operators ==&lt;br /&gt;
&lt;br /&gt;
Most comparison operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Operation&lt;br /&gt;
! C/C++&lt;br /&gt;
! Python&lt;br /&gt;
|-&lt;br /&gt;
| equality&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| inequality&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| less than&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;lt;&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;lt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| greater than&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| less than or equal&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;lt;=&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;lt;=&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| greater than or equal&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;gt;=&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;gt;=&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python also allows chained comparisons.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (0 &amp;lt;= x &amp;amp;&amp;amp; x &amp;lt; 100) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 0 &amp;lt;= x &amp;lt; 100:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 11. The ternary operator ==&lt;br /&gt;
&lt;br /&gt;
Both languages support conditional expressions.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int maximum = (a &amp;gt; b) ? a : b;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
maximum = a if a &amp;gt; b else b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python&amp;#039;s order is different:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
value_if_true if condition else value_if_false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 12. While loops ==&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;code&amp;gt;while&amp;lt;/code&amp;gt; loop is very similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int i = 0;&lt;br /&gt;
&lt;br /&gt;
while (i &amp;lt; 5) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
i++;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
&lt;br /&gt;
while i &amp;lt; 5:&lt;br /&gt;
    print(i)&lt;br /&gt;
    i += 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not have &amp;lt;code&amp;gt;++&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;--&amp;lt;/code&amp;gt; operators.&lt;br /&gt;
&lt;br /&gt;
Therefore:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i += 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is used instead.&lt;br /&gt;
&lt;br /&gt;
== 13. For loops ==&lt;br /&gt;
&lt;br /&gt;
This is one of the first important conceptual differences.&lt;br /&gt;
&lt;br /&gt;
In C/C++, a &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement commonly controls a numeric index.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement iterates over objects.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 5; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;range(5)&amp;lt;/code&amp;gt; generates values conceptually equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
0, 1, 2, 3, 4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A different starting value can be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 2; i &amp;lt; 10; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(2, 10):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A step can also be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i += 2) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(0, 10, 2):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 14. Iterating over containers ==&lt;br /&gt;
&lt;br /&gt;
Suppose we want to print every element of a vector/list.&lt;br /&gt;
&lt;br /&gt;
An index-based implementation is possible:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; values.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[i] &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
    print(values[i])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
However, this is generally &amp;#039;&amp;#039;&amp;#039;not the preferred Python style&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
If the index is not required, iterate directly over the elements.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; value : values) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; value &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for value in values:&lt;br /&gt;
    print(value)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The second Python version should normally be preferred.&lt;br /&gt;
&lt;br /&gt;
== 15. When both index and value are required ==&lt;br /&gt;
&lt;br /&gt;
C/C++ programmers frequently use an explicit index.&lt;br /&gt;
&lt;br /&gt;
Python provides &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;std::string&amp;gt; names = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; names.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; names[i]&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
names = [&amp;quot;Ana&amp;quot;, &amp;quot;Bob&amp;quot;, &amp;quot;Carol&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
for i, name in enumerate(names):&lt;br /&gt;
    print(i, &amp;quot;:&amp;quot;, name)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This introduces another frequently used Python feature: &amp;#039;&amp;#039;&amp;#039;unpacking&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== 16. Lists ==&lt;br /&gt;
&lt;br /&gt;
The closest commonly used Python equivalent to &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.push_back(40);&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[0];&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values.size();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
values.append(40)&lt;br /&gt;
&lt;br /&gt;
print(values[0])&lt;br /&gt;
print(len(values))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python lists can contain objects of different types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// A normal std::vector cannot&lt;br /&gt;
// contain unrelated types.&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [&lt;br /&gt;
10,&lt;br /&gt;
3.14,&lt;br /&gt;
&amp;quot;hello&amp;quot;,&lt;br /&gt;
True&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Although Python allows this, homogeneous collections are generally easier to understand and maintain when the elements conceptually represent the same kind of data.&lt;br /&gt;
&lt;br /&gt;
== 17. Negative indexing ==&lt;br /&gt;
&lt;br /&gt;
Python allows indexing from the end of a sequence.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {10, 20, 30, 40};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 1]; // 40&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 2]; // 30&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40]&lt;br /&gt;
&lt;br /&gt;
print(v[-1])  # 40&lt;br /&gt;
print(v[-2])  # 30&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 18. Slicing ==&lt;br /&gt;
&lt;br /&gt;
Python sequences support slicing directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {&lt;br /&gt;
    10, 20, 30, 40, 50&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; part(&lt;br /&gt;
v.begin() + 1,&lt;br /&gt;
v.begin() + 4&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40, 50]&lt;br /&gt;
&lt;br /&gt;
part = v[1:4]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The result is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[20, 30, 40]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
General syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
sequence[start:stop:step]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// Requires an explicit loop or&lt;br /&gt;
// another algorithm.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [0, 1, 2, 3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
print(v[1:4])   # [1, 2, 3]&lt;br /&gt;
print(v[:3])    # [0, 1, 2]&lt;br /&gt;
print(v[3:])    # [3, 4, 5]&lt;br /&gt;
print(v[::2])   # [0, 2, 4]&lt;br /&gt;
print(v[::-1])  # [5, 4, 3, 2, 1, 0]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 19. Membership testing ==&lt;br /&gt;
&lt;br /&gt;
Checking whether an element exists in a container is commonly performed using &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
if (std::find(&lt;br /&gt;
values.begin(),&lt;br /&gt;
values.end(),&lt;br /&gt;
20&lt;br /&gt;
) != values.end()) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
    print(&amp;quot;Found&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The opposite test uses &amp;lt;code&amp;gt;not in&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 100 not in values:&lt;br /&gt;
    print(&amp;quot;Not found&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 20. Dictionaries ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt; is similar to a C++ associative container such as &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades;&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10;&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; grades[&amp;quot;Ana&amp;quot;];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {}&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8&lt;br /&gt;
&lt;br /&gt;
print(grades[&amp;quot;Ana&amp;quot;])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A dictionary can also be initialized directly:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades = {&lt;br /&gt;
    {&amp;quot;Ana&amp;quot;, 10},&lt;br /&gt;
    {&amp;quot;Bob&amp;quot;, 8},&lt;br /&gt;
    {&amp;quot;Carol&amp;quot;, 9}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 10,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 8,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 9,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Iterating over keys and values:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; grade&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
    print(name, &amp;quot;:&amp;quot;, grade)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 21. Sets ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; represents a collection containing unique values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.insert(40);&lt;br /&gt;
&lt;br /&gt;
if (20 != values.end()) {&lt;br /&gt;
// conceptually test membership&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = {10, 20, 30}&lt;br /&gt;
&lt;br /&gt;
values.add(40)&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
    print(&amp;quot;Found&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A more correct C++ membership test would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (values.find(20) != values.end()) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets are particularly useful when:&lt;br /&gt;
&lt;br /&gt;
* duplicate values should be removed;&lt;br /&gt;
* fast membership tests are required;&lt;br /&gt;
* mathematical set operations are useful.&lt;br /&gt;
&lt;br /&gt;
== 22. Tuples ==&lt;br /&gt;
&lt;br /&gt;
Tuples are similar to lists, but they are immutable.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::tuple&amp;lt;int, double, std::string&amp;gt; data {&lt;br /&gt;
    10,&lt;br /&gt;
    3.14,&lt;br /&gt;
    &amp;quot;test&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
data = (10, 3.14, &amp;quot;test&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python uses tuples extensively for returning or unpacking multiple values.&lt;br /&gt;
&lt;br /&gt;
== 23. Functions ==&lt;br /&gt;
&lt;br /&gt;
Functions are declared using &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    return a + b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int result = add(2, 3);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def add(a, b):&lt;br /&gt;
    return a + b&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
result = add(2, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not require parameter or return types to be declared.&lt;br /&gt;
&lt;br /&gt;
However, Python supports optional &amp;#039;&amp;#039;&amp;#039;type hints&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
double average(&lt;br /&gt;
    double a,&lt;br /&gt;
    double b&lt;br /&gt;
) {&lt;br /&gt;
    return (a + b) / 2.0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(&lt;br /&gt;
    a: float,&lt;br /&gt;
    b: float&lt;br /&gt;
) -&amp;gt; float:&lt;br /&gt;
    return (a + b) / 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Type hints do not normally enforce types at runtime.&lt;br /&gt;
&lt;br /&gt;
They provide information for:&lt;br /&gt;
&lt;br /&gt;
* the programmer;&lt;br /&gt;
* IDEs;&lt;br /&gt;
* static analysis tools;&lt;br /&gt;
* documentation.&lt;br /&gt;
&lt;br /&gt;
== 24. Default parameters ==&lt;br /&gt;
&lt;br /&gt;
Both C++ and Python support default parameter values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void greet(&lt;br /&gt;
    const std::string&amp;amp; name = &amp;quot;World&amp;quot;&lt;br /&gt;
) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Hello, &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
greet();&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def greet(name=&amp;quot;World&amp;quot;):&lt;br /&gt;
    print(f&amp;quot;Hello, {name}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
greet()&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 25. Named arguments ==&lt;br /&gt;
&lt;br /&gt;
Python allows function arguments to be specified by name.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void connect(&lt;br /&gt;
    std::string host,&lt;br /&gt;
    int port,&lt;br /&gt;
    bool secure&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
connect(&amp;quot;server.local&amp;quot;, 443, true);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def connect(host, port, secure):&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
connect(&lt;br /&gt;
    host=&amp;quot;server.local&amp;quot;,&lt;br /&gt;
    port=443,&lt;br /&gt;
    secure=True&lt;br /&gt;
)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This can make calls with several parameters easier to understand.&lt;br /&gt;
&lt;br /&gt;
== 26. Returning multiple values ==&lt;br /&gt;
&lt;br /&gt;
In C++, multiple return values typically require a structure, tuple, pair or output parameters.&lt;br /&gt;
&lt;br /&gt;
Python commonly uses tuples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::pair&amp;lt;int, int&amp;gt; minmax(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    if (a &amp;lt; b)&lt;br /&gt;
        return {a, b};&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
return {b, a};&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
auto [minimum, maximum] = minmax(10, 3);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def minmax(a, b):&lt;br /&gt;
    if a &amp;lt; b:&lt;br /&gt;
        return a, b&lt;br /&gt;
&lt;br /&gt;
    return b, a&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
minimum, maximum = minmax(10, 3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is another example of tuple unpacking.&lt;br /&gt;
&lt;br /&gt;
== 27. Swapping variables ==&lt;br /&gt;
&lt;br /&gt;
A temporary variable is normally used in C.&lt;br /&gt;
&lt;br /&gt;
Python supports unpacking directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int temp = a;&lt;br /&gt;
a = b;&lt;br /&gt;
b = temp;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a, b = b, a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 28. Strings ==&lt;br /&gt;
&lt;br /&gt;
Python strings behave more like C++ &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt; objects than traditional C character arrays.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text.size();&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text[0];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(len(text))&lt;br /&gt;
print(text[0])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
String concatenation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string first = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
std::string second = &amp;quot;World&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::string result =&lt;br /&gt;
first + &amp;quot; &amp;quot; + second;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
first = &amp;quot;Hello&amp;quot;&lt;br /&gt;
second = &amp;quot;World&amp;quot;&lt;br /&gt;
&lt;br /&gt;
result = first + &amp;quot; &amp;quot; + second&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python strings provide many built-in operations:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Usually requires algorithms,&lt;br /&gt;
// explicit transformations,&lt;br /&gt;
// or utility functions.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(text.upper())&lt;br /&gt;
print(text.lower())&lt;br /&gt;
print(text.startswith(&amp;quot;he&amp;quot;))&lt;br /&gt;
print(text.endswith(&amp;quot;lo&amp;quot;))&lt;br /&gt;
print(text.replace(&amp;quot;l&amp;quot;, &amp;quot;x&amp;quot;))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 29. Lists versus arrays: important semantic difference ==&lt;br /&gt;
&lt;br /&gt;
Consider assigning one collection to another.&lt;br /&gt;
&lt;br /&gt;
With normal C++ value semantics, copying a vector produces another vector.&lt;br /&gt;
&lt;br /&gt;
Python assignment behaves differently.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&lt;br /&gt;
b[0] = 100;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a[0];  // 1&lt;br /&gt;
std::cout &amp;lt;&amp;lt; b[0];  // 100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
b = a&lt;br /&gt;
&lt;br /&gt;
b[0] = 100&lt;br /&gt;
&lt;br /&gt;
print(a[0])  # 100&lt;br /&gt;
print(b[0])  # 100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This happens because:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
does not copy the list.&lt;br /&gt;
&lt;br /&gt;
Both names refer to the &amp;#039;&amp;#039;&amp;#039;same list object&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Conceptually:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
       +-------------+&lt;br /&gt;
a ---&amp;gt; | [1, 2, 3]   |&lt;br /&gt;
       +-------------+&lt;br /&gt;
          ^&lt;br /&gt;
          |&lt;br /&gt;
b --------+&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To explicitly create a shallow copy:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a.copy()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = list(a)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This distinction is extremely important when using Python.&lt;br /&gt;
&lt;br /&gt;
== 30. Equality versus identity ==&lt;br /&gt;
&lt;br /&gt;
Python distinguishes between:&lt;br /&gt;
&lt;br /&gt;
* whether two objects contain equal values;&lt;br /&gt;
* whether two names refer to the exact same object.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; (a == b);  // true&lt;br /&gt;
&lt;br /&gt;
// Address comparison would be needed&lt;br /&gt;
// to determine object identity.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
b = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
print(a == b)  # True&lt;br /&gt;
print(a is b)  # False&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In Python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
==  tests value equality&lt;br /&gt;
is  tests object identity&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not use &amp;lt;code&amp;gt;is&amp;lt;/code&amp;gt; when you intend to compare values.&lt;br /&gt;
&lt;br /&gt;
A common correct use is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if value is None:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 31. None and nullptr ==&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; represents the absence of a value.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Object* ptr = nullptr;&lt;br /&gt;
&lt;br /&gt;
if (ptr == nullptr) {&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = None&lt;br /&gt;
&lt;br /&gt;
if value is None:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 32. Exceptions ==&lt;br /&gt;
&lt;br /&gt;
Exception handling is conceptually similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    int value = std::stoi(text);&lt;br /&gt;
}&lt;br /&gt;
catch (const std::exception&amp;amp; e) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Invalid value\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    value = int(text)&lt;br /&gt;
except ValueError:&lt;br /&gt;
    print(&amp;quot;Invalid value&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python generally uses exceptions rather than special error return values for many failure conditions.&lt;br /&gt;
&lt;br /&gt;
== 33. A first Pythonic transformation ==&lt;br /&gt;
&lt;br /&gt;
Consider creating a vector/list containing the squares of the numbers from 0 to 9.&lt;br /&gt;
&lt;br /&gt;
A direct translation from C++ could look like this:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python — direct translation&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = []&lt;br /&gt;
&lt;br /&gt;
for i in range(10):&lt;br /&gt;
    squares.append(i * i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python code is correct.&lt;br /&gt;
&lt;br /&gt;
However, Python provides a concise construct called a &amp;#039;&amp;#039;&amp;#039;list comprehension&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python — idiomatic&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A condition can also be included.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
if (i % 2 == 0) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
if i % 2 == 0&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
An important objective of this course is not only to write programs that work in Python, but to learn to write programs in a style appropriate for Python.&lt;br /&gt;
&lt;br /&gt;
== 34. Example: processing student grades ==&lt;br /&gt;
&lt;br /&gt;
Consider the following problem:&lt;br /&gt;
&lt;br /&gt;
* store the grades of several students;&lt;br /&gt;
* compute the average;&lt;br /&gt;
* print the students whose grade is greater than or equal to the average.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;unordered_map&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt; grades = {&lt;br /&gt;
{&amp;quot;Ana&amp;quot;, 9.5},&lt;br /&gt;
{&amp;quot;Bob&amp;quot;, 7.0},&lt;br /&gt;
{&amp;quot;Carol&amp;quot;, 8.5},&lt;br /&gt;
{&amp;quot;Dan&amp;quot;, 6.0}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double sum = 0.0;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    sum += grade;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
double average = sum / grades.size();&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Average: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; average&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    if (grade &amp;gt;= average) {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
                  &amp;lt;&amp;lt; grade&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
    &amp;quot;Dan&amp;quot;: 6.0,&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
average = sum(grades.values()) / len(grades)&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;Average: {average:.2f}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
    if grade &amp;gt;= average:&lt;br /&gt;
        print(f&amp;quot;{name}: {grade}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that the Python version uses existing language functionality rather than manually reproducing every operation.&lt;br /&gt;
&lt;br /&gt;
In particular:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sum(grades.values())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
expresses the intention directly.&lt;br /&gt;
&lt;br /&gt;
== 35. Exercise 1 — Translation ==&lt;br /&gt;
&lt;br /&gt;
Translate the following C++ program into Python.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
12, 7, 18, 3, 24, 11&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int sum = 0;&lt;br /&gt;
int count = 0;&lt;br /&gt;
&lt;br /&gt;
for (int value : values) {&lt;br /&gt;
    if (value % 2 == 0) {&lt;br /&gt;
        sum += value;&lt;br /&gt;
        count++;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Count: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; count&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Sum: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; sum&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Your implementation:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# TODO&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
After implementing a direct translation, try to simplify the Python implementation.&lt;br /&gt;
&lt;br /&gt;
== 36. Exercise 2 — Statistics ==&lt;br /&gt;
&lt;br /&gt;
Write a Python program that:&lt;br /&gt;
&lt;br /&gt;
# reads a sequence of integer values;&lt;br /&gt;
# stores them in a list;&lt;br /&gt;
# prints the minimum value;&lt;br /&gt;
# prints the maximum value;&lt;br /&gt;
# prints the average;&lt;br /&gt;
# prints how many values are above the average.&lt;br /&gt;
&lt;br /&gt;
The equivalent C++ structure might begin as follows:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values;&lt;br /&gt;
&lt;br /&gt;
// Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
int minimum = ...;&lt;br /&gt;
int maximum = ...;&lt;br /&gt;
double average = ...;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = []&lt;br /&gt;
&lt;br /&gt;
# Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
minimum = ...&lt;br /&gt;
maximum = ...&lt;br /&gt;
average = ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Useful Python functions include:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
len(...)&lt;br /&gt;
sum(...)&lt;br /&gt;
min(...)&lt;br /&gt;
max(...)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 37. Exercise 3 — Word frequency ==&lt;br /&gt;
&lt;br /&gt;
Given a sentence, count how many times each word occurs.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python is simple and python is powerful&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
should produce information equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python -&amp;gt; 2&lt;br /&gt;
is -&amp;gt; 2&lt;br /&gt;
simple -&amp;gt; 1&lt;br /&gt;
and -&amp;gt; 1&lt;br /&gt;
powerful -&amp;gt; 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A possible C++ data structure and its Python equivalent are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt;&lt;br /&gt;
    frequencies;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
frequencies = {}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sentence.split()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to separate the sentence into words.&lt;br /&gt;
&lt;br /&gt;
== 38. Exercise 4 — Remove duplicates ==&lt;br /&gt;
&lt;br /&gt;
Given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [4, 2, 7, 4, 2, 9, 7, 1]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
create a collection containing every distinct value.&lt;br /&gt;
&lt;br /&gt;
Think about the following C++ and Python data structures:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; unique_values;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
unique_values = set()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Try to solve the problem both:&lt;br /&gt;
&lt;br /&gt;
# using an explicit loop;&lt;br /&gt;
# using Python&amp;#039;s built-in functionality.&lt;br /&gt;
&lt;br /&gt;
== 39. Exercise 5 — Small problem ==&lt;br /&gt;
&lt;br /&gt;
Write a program that receives a list of students and their grades and determines:&lt;br /&gt;
&lt;br /&gt;
* the class average;&lt;br /&gt;
* the student with the highest grade;&lt;br /&gt;
* all students who passed;&lt;br /&gt;
* all students whose grade is above the class average.&lt;br /&gt;
&lt;br /&gt;
Suggested representation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt;&lt;br /&gt;
    grades;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
    &amp;quot;Dan&amp;quot;: 4.0,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Avoid using numeric indices unless an index is actually required.&lt;br /&gt;
&lt;br /&gt;
== 40. Creating a project environment ==&lt;br /&gt;
&lt;br /&gt;
Python applications commonly use a &amp;#039;&amp;#039;&amp;#039;virtual environment&amp;#039;&amp;#039;&amp;#039; to isolate project dependencies.&lt;br /&gt;
&lt;br /&gt;
Create a directory for the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir python-project&lt;br /&gt;
cd python-project&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a virtual environment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 -m venv .venv&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Linux/macOS:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source .venv/bin/activate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Windows PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
.venv\Scripts\Activate.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command prompt will normally indicate that the environment is active.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
(.venv) student@computer:~/python-project$&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Packages installed using &amp;lt;code&amp;gt;pip&amp;lt;/code&amp;gt; will now be installed inside this environment.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip install requests&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To inspect installed packages:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip list&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 41. Recommended initial project structure ==&lt;br /&gt;
&lt;br /&gt;
For the first laboratories, the following structure is sufficient:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python-project/&lt;br /&gt;
├── .venv/&lt;br /&gt;
├── main.py&lt;br /&gt;
├── README.md&lt;br /&gt;
└── .gitignore&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A basic &amp;lt;code&amp;gt;.gitignore&amp;lt;/code&amp;gt; should contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.venv/&lt;br /&gt;
__pycache__/&lt;br /&gt;
*.pyc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; commit the virtual environment to Git.&lt;br /&gt;
&lt;br /&gt;
== 42. C/C++ habits to reconsider in Python ==&lt;br /&gt;
&lt;br /&gt;
When transitioning from C/C++ to Python, the following patterns should generally be reconsidered.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++ habit&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Typical Python approach&lt;br /&gt;
|-&lt;br /&gt;
| Iterate using an integer index&lt;br /&gt;
| Iterate directly over the objects&lt;br /&gt;
|-&lt;br /&gt;
| Manually maintain counters&lt;br /&gt;
| Consider &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Search manually through a container&lt;br /&gt;
| Use &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Manually calculate container size&lt;br /&gt;
| Use &amp;lt;code&amp;gt;len()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Explicit temporary variable for swapping&lt;br /&gt;
| Use tuple unpacking&lt;br /&gt;
|-&lt;br /&gt;
| Explicit loop for simple transformations&lt;br /&gt;
| Consider a comprehension&lt;br /&gt;
|-&lt;br /&gt;
| Long output expressions&lt;br /&gt;
| Use f-strings&lt;br /&gt;
|-&lt;br /&gt;
| Copy assumed after assignment&lt;br /&gt;
| Remember that names refer to objects&lt;br /&gt;
|-&lt;br /&gt;
| Check null using equality&lt;br /&gt;
| Use &amp;lt;code&amp;gt;is None&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The objective is not simply to translate C/C++ syntax into Python syntax.&lt;br /&gt;
&lt;br /&gt;
Consider:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C++ style translated literally&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | More idiomatic Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
    print(values[i])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for value in values:&lt;br /&gt;
    print(value)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Both programs are correct.&lt;br /&gt;
&lt;br /&gt;
The second expresses the programmer&amp;#039;s intention more directly.&lt;br /&gt;
&lt;br /&gt;
== 43. Summary ==&lt;br /&gt;
&lt;br /&gt;
The main mappings introduced in this laboratory are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;{ ... }&amp;lt;/code&amp;gt;&lt;br /&gt;
| indentation&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;amp;&amp;amp;amp;&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;#124;&amp;amp;#124;&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_set&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::tuple&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;tuple&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cin&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| indexed iteration&lt;br /&gt;
| direct iteration / &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| manual search&lt;br /&gt;
| &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::pow(a, b)&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;a ** b&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| integer division&lt;br /&gt;
| &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The central idea to retain from this laboratory is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Python should not be treated merely as C++ with different syntax. Python provides its own abstractions and idioms, and using them generally produces simpler and more readable programs.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 44. Preparation for Lab 2 ==&lt;br /&gt;
&lt;br /&gt;
Before the next laboratory:&lt;br /&gt;
&lt;br /&gt;
# complete all exercises from this laboratory;&lt;br /&gt;
# ensure Python 3 is installed;&lt;br /&gt;
# create a virtual environment successfully;&lt;br /&gt;
# create a Git repository for the semester project;&lt;br /&gt;
# become familiar with lists, dictionaries, tuples and sets;&lt;br /&gt;
# review the concepts of classes, objects, inheritance and composition from C++.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Lab 2&amp;#039;&amp;#039;&amp;#039;, these concepts will be revisited from the perspective of Python&amp;#039;s object model and Pythonic object-oriented programming.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8367</id>
		<title>Lab 1</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8367"/>
		<updated>2026-09-14T05:46:51Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* 10. Comparison operators */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 1 — Python for C/C++ Programmers =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
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++.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
After completing this laboratory, you should be able to:&lt;br /&gt;
&lt;br /&gt;
* understand the basic syntax of Python;&lt;br /&gt;
* recognize the main differences between Python and C/C++;&lt;br /&gt;
* work with Python&amp;#039;s fundamental data types;&lt;br /&gt;
* use conditional statements and loops;&lt;br /&gt;
* work with lists, dictionaries, sets and tuples;&lt;br /&gt;
* define and call functions;&lt;br /&gt;
* understand basic Python object/reference semantics;&lt;br /&gt;
* recognize several common Python idioms;&lt;br /&gt;
* create and execute a simple Python project.&lt;br /&gt;
&lt;br /&gt;
Throughout this laboratory, equivalent or similar C/C++ and Python code will be presented side by side.&lt;br /&gt;
&lt;br /&gt;
**TOC**&lt;br /&gt;
&lt;br /&gt;
== 1. Running a Python program ==&lt;br /&gt;
&lt;br /&gt;
A C/C++ program normally has to be compiled before it is executed. Python programs are usually executed directly by the Python interpreter.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Hello, world!\n&amp;quot;;&lt;br /&gt;
return 0;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(&amp;quot;Hello, world!&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A Python source file normally uses the extension:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be executed using:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unlike C/C++, Python does not require a special &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
However, larger Python programs commonly use:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Program started\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
print(&amp;quot;Program started&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
if **name** == &amp;quot;**main**&amp;quot;:&lt;br /&gt;
main() &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python version is not mandatory, but this pattern will become useful when programs are split into multiple modules.&lt;br /&gt;
&lt;br /&gt;
== 2. Blocks and indentation ==&lt;br /&gt;
&lt;br /&gt;
In C and C++, braces delimit blocks of code.&lt;br /&gt;
&lt;br /&gt;
Python uses &amp;#039;&amp;#039;&amp;#039;indentation&amp;#039;&amp;#039;&amp;#039; instead.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (x &amp;gt; 10) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Large\n&amp;quot;;&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Value: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
    print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
    print(&amp;quot;Value:&amp;quot;, x)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Indentation in Python is part of the language syntax.&lt;br /&gt;
&lt;br /&gt;
The standard convention is &amp;#039;&amp;#039;&amp;#039;4 spaces per indentation level&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
For example, the following code is incorrect:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python will report an &amp;lt;code&amp;gt;IndentationError&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 3. Variables and types ==&lt;br /&gt;
&lt;br /&gt;
C/C++ variables have a declared static type.&lt;br /&gt;
&lt;br /&gt;
Python variables do not require explicit type declarations.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int count = 10;&lt;br /&gt;
double temperature = 23.5;&lt;br /&gt;
bool enabled = true;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
count = 10&lt;br /&gt;
temperature = 23.5&lt;br /&gt;
enabled = True&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python is &amp;#039;&amp;#039;&amp;#039;dynamically typed&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
This means that a variable name can later refer to an object of another type.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
// Not allowed:&lt;br /&gt;
// value = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
value = &amp;quot;hello&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This does &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; mean that Python has no types.&lt;br /&gt;
&lt;br /&gt;
Objects still have types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;typeinfo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; typeid(value).name();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
&lt;br /&gt;
print(type(value))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python will produce:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common basic types ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ type&lt;br /&gt;
! Python equivalent&lt;br /&gt;
! Example&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;42&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt; / &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;3.14&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt;&lt;br /&gt;
| No separate character type&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;A&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;str&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;Hello&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 4. Constants ==&lt;br /&gt;
&lt;br /&gt;
C++ can enforce that a value cannot be modified using &amp;lt;code&amp;gt;const&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python does not enforce constants at language level.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
const double PI = 3.1415926535;&lt;br /&gt;
const int MAX_USERS = 100; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
PI = 3.1415926535&lt;br /&gt;
MAX_USERS = 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
By convention, Python names that should be treated as constants are written using uppercase letters.&lt;br /&gt;
&lt;br /&gt;
Nothing prevents the programmer from changing them.&lt;br /&gt;
&lt;br /&gt;
== 5. Arithmetic operators ==&lt;br /&gt;
&lt;br /&gt;
Most arithmetic operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
int sum  = a + b;&lt;br /&gt;
int diff = a - b;&lt;br /&gt;
int prod = a * b;&lt;br /&gt;
int div  = a / b;&lt;br /&gt;
int rem  = a % b; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
sum_ = a + b&lt;br /&gt;
diff = a - b&lt;br /&gt;
prod = a * b&lt;br /&gt;
div = a // b&lt;br /&gt;
rem = a % b &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
There is an important difference between &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a / b;   // 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
print(a / b)   # 3.3333333333333335&lt;br /&gt;
print(a // b)  # 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In C/C++, dividing two integers performs integer division.&lt;br /&gt;
&lt;br /&gt;
In Python, the &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; operator always performs floating-point division, while &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt; performs floor division.&lt;br /&gt;
&lt;br /&gt;
Python also provides an exponentiation operator:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;cmath&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double result = std::pow(2, 8); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
result = 2 ** 8 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 6. Printing values ==&lt;br /&gt;
&lt;br /&gt;
Output using &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt; is replaced by the built-in &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
int age = 21;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; is &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; age&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; years old\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
age = 21&lt;br /&gt;
&lt;br /&gt;
print(name, &amp;quot;is&amp;quot;, age, &amp;quot;years old&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python also provides formatted string literals, commonly called &amp;#039;&amp;#039;&amp;#039;f-strings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Temperature: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; temperature&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; C\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Temperature: {temperature} C&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Expressions can appear directly inside an f-string:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Result: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; a + b&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Result: {a + b}&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 7. Reading input ==&lt;br /&gt;
&lt;br /&gt;
Console input is performed using &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An important difference is that &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt; &amp;#039;&amp;#039;&amp;#039;always returns a string&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int age;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Age: &amp;quot;;&lt;br /&gt;
std::cin &amp;gt;&amp;gt; age; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = int(input(&amp;quot;Age: &amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Without the conversion:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = input(&amp;quot;Age: &amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; is a string.&lt;br /&gt;
&lt;br /&gt;
Conversions are explicit:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = std::stoi(text);&lt;br /&gt;
double b = std::stod(text); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = int(text)&lt;br /&gt;
b = float(text) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 8. Conditional statements ==&lt;br /&gt;
&lt;br /&gt;
The overall structure is similar, but Python does not use parentheses or braces.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (temperature &amp;gt; 30) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Hot\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else if (temperature &amp;gt; 20) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Warm\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Cold\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if temperature &amp;gt; 30:&lt;br /&gt;
print(&amp;quot;Hot&amp;quot;)&lt;br /&gt;
elif temperature &amp;gt; 20:&lt;br /&gt;
print(&amp;quot;Warm&amp;quot;)&lt;br /&gt;
else:&lt;br /&gt;
print(&amp;quot;Cold&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python uses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
elif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
instead of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
else if&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 9. Boolean operators ==&lt;br /&gt;
&lt;br /&gt;
Python uses words instead of the C/C++ symbolic logical operators.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;amp;&amp;amp;amp;&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;#124;&amp;amp;#124;&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (age &amp;gt;= 18 &amp;amp;&amp;amp; enabled) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if (!enabled) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if age &amp;gt;= 18 and enabled:&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
if not enabled:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 10. Comparison operators ==&lt;br /&gt;
&lt;br /&gt;
Most comparison operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Operation&lt;br /&gt;
! C/C++&lt;br /&gt;
! Python&lt;br /&gt;
|-&lt;br /&gt;
| equality&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| inequality&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| less than&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;lt;&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;lt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| greater than&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| less than or equal&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;lt;=&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;lt;=&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| greater than or equal&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;gt;=&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;gt;=&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python also allows chained comparisons.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (0 &amp;lt;= x &amp;amp;&amp;amp; x &amp;lt; 100) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 0 &amp;lt;= x &amp;lt; 100:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 11. The ternary operator ==&lt;br /&gt;
&lt;br /&gt;
Both languages support conditional expressions.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int maximum = (a &amp;gt; b) ? a : b;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
maximum = a if a &amp;gt; b else b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python&amp;#039;s order is different:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
value_if_true if condition else value_if_false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 12. While loops ==&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;code&amp;gt;while&amp;lt;/code&amp;gt; loop is very similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int i = 0;&lt;br /&gt;
&lt;br /&gt;
while (i &amp;lt; 5) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
i++;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
&lt;br /&gt;
while i &amp;lt; 5:&lt;br /&gt;
print(i)&lt;br /&gt;
i += 1 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not have &amp;lt;code&amp;gt;++&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;--&amp;lt;/code&amp;gt; operators.&lt;br /&gt;
&lt;br /&gt;
Therefore:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i += 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is used instead.&lt;br /&gt;
&lt;br /&gt;
== 13. For loops ==&lt;br /&gt;
&lt;br /&gt;
This is one of the first important conceptual differences.&lt;br /&gt;
&lt;br /&gt;
In C/C++, a &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement commonly controls a numeric index.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement iterates over objects.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 5; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;range(5)&amp;lt;/code&amp;gt; generates values conceptually equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
0, 1, 2, 3, 4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A different starting value can be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 2; i &amp;lt; 10; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(2, 10):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A step can also be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i += 2) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(0, 10, 2):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 14. Iterating over containers ==&lt;br /&gt;
&lt;br /&gt;
Suppose we want to print every element of a vector/list.&lt;br /&gt;
&lt;br /&gt;
An index-based implementation is possible:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; values.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[i] &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
print(values[i]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
However, this is generally &amp;#039;&amp;#039;&amp;#039;not the preferred Python style&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
If the index is not required, iterate directly over the elements.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; value : values) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; value &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for value in values:&lt;br /&gt;
print(value) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The second Python version should normally be preferred.&lt;br /&gt;
&lt;br /&gt;
== 15. When both index and value are required ==&lt;br /&gt;
&lt;br /&gt;
C/C++ programmers frequently use an explicit index.&lt;br /&gt;
&lt;br /&gt;
Python provides &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;std::string&amp;gt; names = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; names.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; names[i]&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
names = [&amp;quot;Ana&amp;quot;, &amp;quot;Bob&amp;quot;, &amp;quot;Carol&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
for i, name in enumerate(names):&lt;br /&gt;
print(i, &amp;quot;:&amp;quot;, name) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This introduces another frequently used Python feature: &amp;#039;&amp;#039;&amp;#039;unpacking&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== 16. Lists ==&lt;br /&gt;
&lt;br /&gt;
The closest commonly used Python equivalent to &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.push_back(40);&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[0];&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values.size(); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
values.append(40)&lt;br /&gt;
&lt;br /&gt;
print(values[0])&lt;br /&gt;
print(len(values)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python lists can contain objects of different types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// A normal std::vector cannot&lt;br /&gt;
// contain unrelated types.&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
10, 20, 30&lt;br /&gt;
}; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [&lt;br /&gt;
10,&lt;br /&gt;
3.14,&lt;br /&gt;
&amp;quot;hello&amp;quot;,&lt;br /&gt;
True&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Although Python allows this, homogeneous collections are generally easier to understand and maintain when the elements conceptually represent the same kind of data.&lt;br /&gt;
&lt;br /&gt;
== 17. Negative indexing ==&lt;br /&gt;
&lt;br /&gt;
Python allows indexing from the end of a sequence.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {10, 20, 30, 40};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 1]; // 40&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 2]; // 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40]&lt;br /&gt;
&lt;br /&gt;
print(v[-1])  # 40&lt;br /&gt;
print(v[-2])  # 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 18. Slicing ==&lt;br /&gt;
&lt;br /&gt;
Python sequences support slicing directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {&lt;br /&gt;
    10, 20, 30, 40, 50&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; part(&lt;br /&gt;
v.begin() + 1,&lt;br /&gt;
v.begin() + 4&lt;br /&gt;
); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40, 50]&lt;br /&gt;
&lt;br /&gt;
part = v[1:4] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The result is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[20, 30, 40]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
General syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
sequence[start:stop:step]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// Requires an explicit loop or&lt;br /&gt;
// another algorithm.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [0, 1, 2, 3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
print(v[1:4])   # [1, 2, 3]&lt;br /&gt;
print(v[:3])    # [0, 1, 2]&lt;br /&gt;
print(v[3:])    # [3, 4, 5]&lt;br /&gt;
print(v[::2])   # [0, 2, 4]&lt;br /&gt;
print(v[::-1])  # [5, 4, 3, 2, 1, 0] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 19. Membership testing ==&lt;br /&gt;
&lt;br /&gt;
Checking whether an element exists in a container is commonly performed using &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
if (std::find(&lt;br /&gt;
values.begin(),&lt;br /&gt;
values.end(),&lt;br /&gt;
20&lt;br /&gt;
) != values.end()) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The opposite test uses &amp;lt;code&amp;gt;not in&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 100 not in values:&lt;br /&gt;
    print(&amp;quot;Not found&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 20. Dictionaries ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt; is similar to a C++ associative container such as &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades;&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10;&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; grades[&amp;quot;Ana&amp;quot;]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {}&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8&lt;br /&gt;
&lt;br /&gt;
print(grades[&amp;quot;Ana&amp;quot;]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A dictionary can also be initialized directly:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades = {&lt;br /&gt;
    {&amp;quot;Ana&amp;quot;, 10},&lt;br /&gt;
    {&amp;quot;Bob&amp;quot;, 8},&lt;br /&gt;
    {&amp;quot;Carol&amp;quot;, 9}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 10,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 8,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 9,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Iterating over keys and values:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; grade&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
    print(name, &amp;quot;:&amp;quot;, grade)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 21. Sets ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; represents a collection containing unique values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.insert(40);&lt;br /&gt;
&lt;br /&gt;
if (20 != values.end()) {&lt;br /&gt;
// conceptually test membership&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = {10, 20, 30}&lt;br /&gt;
&lt;br /&gt;
values.add(40)&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A more correct C++ membership test would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (values.find(20) != values.end()) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets are particularly useful when:&lt;br /&gt;
&lt;br /&gt;
* duplicate values should be removed;&lt;br /&gt;
* fast membership tests are required;&lt;br /&gt;
* mathematical set operations are useful.&lt;br /&gt;
&lt;br /&gt;
== 22. Tuples ==&lt;br /&gt;
&lt;br /&gt;
Tuples are similar to lists, but they are immutable.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::tuple&amp;lt;int, double, std::string&amp;gt; data {&lt;br /&gt;
    10,&lt;br /&gt;
    3.14,&lt;br /&gt;
    &amp;quot;test&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
data = (10, 3.14, &amp;quot;test&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python uses tuples extensively for returning or unpacking multiple values.&lt;br /&gt;
&lt;br /&gt;
== 23. Functions ==&lt;br /&gt;
&lt;br /&gt;
Functions are declared using &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    return a + b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int result = add(2, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def add(a, b):&lt;br /&gt;
return a + b&lt;br /&gt;
&lt;br /&gt;
result = add(2, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not require parameter or return types to be declared.&lt;br /&gt;
&lt;br /&gt;
However, Python supports optional &amp;#039;&amp;#039;&amp;#039;type hints&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
double average(&lt;br /&gt;
    double a,&lt;br /&gt;
    double b&lt;br /&gt;
) {&lt;br /&gt;
    return (a + b) / 2.0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(&lt;br /&gt;
    a: float,&lt;br /&gt;
    b: float&lt;br /&gt;
) -&amp;gt; float:&lt;br /&gt;
    return (a + b) / 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Type hints do not normally enforce types at runtime.&lt;br /&gt;
&lt;br /&gt;
They provide information for:&lt;br /&gt;
&lt;br /&gt;
* the programmer;&lt;br /&gt;
* IDEs;&lt;br /&gt;
* static analysis tools;&lt;br /&gt;
* documentation.&lt;br /&gt;
&lt;br /&gt;
== 24. Default parameters ==&lt;br /&gt;
&lt;br /&gt;
Both C++ and Python support default parameter values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void greet(&lt;br /&gt;
    const std::string&amp;amp; name = &amp;quot;World&amp;quot;&lt;br /&gt;
) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Hello, &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
greet();&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def greet(name=&amp;quot;World&amp;quot;):&lt;br /&gt;
print(f&amp;quot;Hello, {name}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
greet()&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 25. Named arguments ==&lt;br /&gt;
&lt;br /&gt;
Python allows function arguments to be specified by name.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void connect(&lt;br /&gt;
    std::string host,&lt;br /&gt;
    int port,&lt;br /&gt;
    bool secure&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
connect(&amp;quot;server.local&amp;quot;, 443, true); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def connect(host, port, secure):&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
connect(&lt;br /&gt;
host=&amp;quot;server.local&amp;quot;,&lt;br /&gt;
port=443,&lt;br /&gt;
secure=True&lt;br /&gt;
) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This can make calls with several parameters easier to understand.&lt;br /&gt;
&lt;br /&gt;
== 26. Returning multiple values ==&lt;br /&gt;
&lt;br /&gt;
In C++, multiple return values typically require a structure, tuple, pair or output parameters.&lt;br /&gt;
&lt;br /&gt;
Python commonly uses tuples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::pair&amp;lt;int, int&amp;gt; minmax(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    if (a &amp;lt; b)&lt;br /&gt;
        return {a, b};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return {b, a};&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
auto [minimum, maximum] = minmax(10, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def minmax(a, b):&lt;br /&gt;
if a &amp;lt; b:&lt;br /&gt;
return a, b&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return b, a&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
minimum, maximum = minmax(10, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is another example of tuple unpacking.&lt;br /&gt;
&lt;br /&gt;
== 27. Swapping variables ==&lt;br /&gt;
&lt;br /&gt;
A temporary variable is normally used in C.&lt;br /&gt;
&lt;br /&gt;
Python supports unpacking directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int temp = a;&lt;br /&gt;
a = b;&lt;br /&gt;
b = temp;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a, b = b, a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 28. Strings ==&lt;br /&gt;
&lt;br /&gt;
Python strings behave more like C++ &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt; objects than traditional C character arrays.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text.size();&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text[0]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(len(text))&lt;br /&gt;
print(text[0]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
String concatenation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string first = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
std::string second = &amp;quot;World&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::string result =&lt;br /&gt;
first + &amp;quot; &amp;quot; + second; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
first = &amp;quot;Hello&amp;quot;&lt;br /&gt;
second = &amp;quot;World&amp;quot;&lt;br /&gt;
&lt;br /&gt;
result = first + &amp;quot; &amp;quot; + second &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python strings provide many built-in operations:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Usually requires algorithms,&lt;br /&gt;
// explicit transformations,&lt;br /&gt;
// or utility functions. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(text.upper())&lt;br /&gt;
print(text.lower())&lt;br /&gt;
print(text.startswith(&amp;quot;he&amp;quot;))&lt;br /&gt;
print(text.endswith(&amp;quot;lo&amp;quot;))&lt;br /&gt;
print(text.replace(&amp;quot;l&amp;quot;, &amp;quot;x&amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 29. Lists versus arrays: important semantic difference ==&lt;br /&gt;
&lt;br /&gt;
Consider assigning one collection to another.&lt;br /&gt;
&lt;br /&gt;
With normal C++ value semantics, copying a vector produces another vector.&lt;br /&gt;
&lt;br /&gt;
Python assignment behaves differently.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&lt;br /&gt;
b[0] = 100;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a[0];  // 1&lt;br /&gt;
std::cout &amp;lt;&amp;lt; b[0];  // 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
b = a&lt;br /&gt;
&lt;br /&gt;
b[0] = 100&lt;br /&gt;
&lt;br /&gt;
print(a[0])  # 100&lt;br /&gt;
print(b[0])  # 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This happens because:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
does not copy the list.&lt;br /&gt;
&lt;br /&gt;
Both names refer to the &amp;#039;&amp;#039;&amp;#039;same list object&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Conceptually:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
       +-------------+&lt;br /&gt;
a ---&amp;gt; | [1, 2, 3]   |&lt;br /&gt;
       +-------------+&lt;br /&gt;
          ^&lt;br /&gt;
          |&lt;br /&gt;
b --------+&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To explicitly create a shallow copy:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a.copy()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = list(a)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This distinction is extremely important when using Python.&lt;br /&gt;
&lt;br /&gt;
== 30. Equality versus identity ==&lt;br /&gt;
&lt;br /&gt;
Python distinguishes between:&lt;br /&gt;
&lt;br /&gt;
* whether two objects contain equal values;&lt;br /&gt;
* whether two names refer to the exact same object.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; (a == b);  // true&lt;br /&gt;
&lt;br /&gt;
// Address comparison would be needed&lt;br /&gt;
// to determine object identity. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
b = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
print(a == b)  # True&lt;br /&gt;
print(a is b)  # False &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In Python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
==  tests value equality&lt;br /&gt;
is  tests object identity&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not use &amp;lt;code&amp;gt;is&amp;lt;/code&amp;gt; when you intend to compare values.&lt;br /&gt;
&lt;br /&gt;
A common correct use is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if value is None:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 31. None and nullptr ==&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; represents the absence of a value.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Object* ptr = nullptr;&lt;br /&gt;
&lt;br /&gt;
if (ptr == nullptr) {&lt;br /&gt;
...&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = None&lt;br /&gt;
&lt;br /&gt;
if value is None:&lt;br /&gt;
... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 32. Exceptions ==&lt;br /&gt;
&lt;br /&gt;
Exception handling is conceptually similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    int value = std::stoi(text);&lt;br /&gt;
}&lt;br /&gt;
catch (const std::exception&amp;amp; e) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Invalid value\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    value = int(text)&lt;br /&gt;
except ValueError:&lt;br /&gt;
    print(&amp;quot;Invalid value&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python generally uses exceptions rather than special error return values for many failure conditions.&lt;br /&gt;
&lt;br /&gt;
== 33. A first Pythonic transformation ==&lt;br /&gt;
&lt;br /&gt;
Consider creating a vector/list containing the squares of the numbers from 0 to 9.&lt;br /&gt;
&lt;br /&gt;
A direct translation from C++ could look like this:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — direct translation |&lt;br /&gt;
| ----------------------------- |&lt;br /&gt;
|                               |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = []&lt;br /&gt;
&lt;br /&gt;
for i in range(10):&lt;br /&gt;
squares.append(i * i) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python code is correct.&lt;br /&gt;
&lt;br /&gt;
However, Python provides a concise construct called a &amp;#039;&amp;#039;&amp;#039;list comprehension&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — idiomatic |&lt;br /&gt;
| -------------------- |&lt;br /&gt;
|                      |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A condition can also be included.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
if (i % 2 == 0) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
}&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
if i % 2 == 0&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
An important objective of this course is not only to write programs that work in Python, but to learn to write programs in a style appropriate for Python.&lt;br /&gt;
&lt;br /&gt;
== 34. Example: processing student grades ==&lt;br /&gt;
&lt;br /&gt;
Consider the following problem:&lt;br /&gt;
&lt;br /&gt;
* store the grades of several students;&lt;br /&gt;
* compute the average;&lt;br /&gt;
* print the students whose grade is greater than or equal to the average.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;unordered_map&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt; grades = {&lt;br /&gt;
{&amp;quot;Ana&amp;quot;, 9.5},&lt;br /&gt;
{&amp;quot;Bob&amp;quot;, 7.0},&lt;br /&gt;
{&amp;quot;Carol&amp;quot;, 8.5},&lt;br /&gt;
{&amp;quot;Dan&amp;quot;, 6.0}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
double sum = 0.0;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    sum += grade;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
double average = sum / grades.size();&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Average: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; average&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    if (grade &amp;gt;= average) {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
                  &amp;lt;&amp;lt; grade&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
&amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
&amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
&amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
&amp;quot;Dan&amp;quot;: 6.0,&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
average = sum(grades.values()) / len(grades)&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;Average: {average:.2f}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
if grade &amp;gt;= average:&lt;br /&gt;
print(f&amp;quot;{name}: {grade}&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that the Python version uses existing language functionality rather than manually reproducing every operation.&lt;br /&gt;
&lt;br /&gt;
In particular:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sum(grades.values())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
expresses the intention directly.&lt;br /&gt;
&lt;br /&gt;
== 35. Exercise 1 — Translation ==&lt;br /&gt;
&lt;br /&gt;
Translate the following C++ program into Python.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
12, 7, 18, 3, 24, 11&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
int sum = 0;&lt;br /&gt;
int count = 0;&lt;br /&gt;
&lt;br /&gt;
for (int value : values) {&lt;br /&gt;
    if (value % 2 == 0) {&lt;br /&gt;
        sum += value;&lt;br /&gt;
        count++;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Count: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; count&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Sum: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; sum&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Your implementation:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# TODO&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
After implementing a direct translation, try to simplify the Python implementation.&lt;br /&gt;
&lt;br /&gt;
== 36. Exercise 2 — Statistics ==&lt;br /&gt;
&lt;br /&gt;
Write a Python program that:&lt;br /&gt;
&lt;br /&gt;
# reads a sequence of integer values;&lt;br /&gt;
&lt;br /&gt;
# stores them in a list;&lt;br /&gt;
&lt;br /&gt;
# prints the minimum value;&lt;br /&gt;
&lt;br /&gt;
# prints the maximum value;&lt;br /&gt;
&lt;br /&gt;
# prints the average;&lt;br /&gt;
&lt;br /&gt;
# prints how many values are above the average.&lt;br /&gt;
&lt;br /&gt;
The equivalent C++ structure might begin as follows:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values;&lt;br /&gt;
&lt;br /&gt;
// Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
int minimum = ...;&lt;br /&gt;
int maximum = ...;&lt;br /&gt;
double average = ...; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = []&lt;br /&gt;
&lt;br /&gt;
# Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
minimum = ...&lt;br /&gt;
maximum = ...&lt;br /&gt;
average = ... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Useful Python functions include:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
len(...)&lt;br /&gt;
sum(...)&lt;br /&gt;
min(...)&lt;br /&gt;
max(...)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 37. Exercise 3 — Word frequency ==&lt;br /&gt;
&lt;br /&gt;
Given a sentence, count how many times each word occurs.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python is simple and python is powerful&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
should produce information equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python -&amp;gt; 2&lt;br /&gt;
is -&amp;gt; 2&lt;br /&gt;
simple -&amp;gt; 1&lt;br /&gt;
and -&amp;gt; 1&lt;br /&gt;
powerful -&amp;gt; 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A possible C++ data structure and its Python equivalent are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt;&lt;br /&gt;
    frequencies;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
frequencies = {}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sentence.split()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to separate the sentence into words.&lt;br /&gt;
&lt;br /&gt;
== 38. Exercise 4 — Remove duplicates ==&lt;br /&gt;
&lt;br /&gt;
Given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [4, 2, 7, 4, 2, 9, 7, 1]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
create a collection containing every distinct value.&lt;br /&gt;
&lt;br /&gt;
Think about the following C++ and Python data structures:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; unique_values;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
unique_values = set()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Try to solve the problem both:&lt;br /&gt;
&lt;br /&gt;
# using an explicit loop;&lt;br /&gt;
&lt;br /&gt;
# using Python&amp;#039;s built-in functionality.&lt;br /&gt;
&lt;br /&gt;
== 39. Exercise 5 — Small problem ==&lt;br /&gt;
&lt;br /&gt;
Write a program that receives a list of students and their grades and determines:&lt;br /&gt;
&lt;br /&gt;
* the class average;&lt;br /&gt;
* the student with the highest grade;&lt;br /&gt;
* all students who passed;&lt;br /&gt;
* all students whose grade is above the class average.&lt;br /&gt;
&lt;br /&gt;
Suggested representation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt;&lt;br /&gt;
    grades;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
    &amp;quot;Dan&amp;quot;: 4.0,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Avoid using numeric indices unless an index is actually required.&lt;br /&gt;
&lt;br /&gt;
== 40. Creating a project environment ==&lt;br /&gt;
&lt;br /&gt;
Python applications commonly use a &amp;#039;&amp;#039;&amp;#039;virtual environment&amp;#039;&amp;#039;&amp;#039; to isolate project dependencies.&lt;br /&gt;
&lt;br /&gt;
Create a directory for the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir python-project&lt;br /&gt;
cd python-project&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a virtual environment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 -m venv .venv&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Linux/macOS:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source .venv/bin/activate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Windows PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
.venv\Scripts\Activate.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command prompt will normally indicate that the environment is active.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
(.venv) student@computer:~/python-project$&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Packages installed using &amp;lt;code&amp;gt;pip&amp;lt;/code&amp;gt; will now be installed inside this environment.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip install requests&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To inspect installed packages:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip list&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 41. Recommended initial project structure ==&lt;br /&gt;
&lt;br /&gt;
For the first laboratories, the following structure is sufficient:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python-project/&lt;br /&gt;
├── .venv/&lt;br /&gt;
├── main.py&lt;br /&gt;
├── README.md&lt;br /&gt;
└── .gitignore&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A basic &amp;lt;code&amp;gt;.gitignore&amp;lt;/code&amp;gt; should contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.venv/&lt;br /&gt;
__pycache__/&lt;br /&gt;
*.pyc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; commit the virtual environment to Git.&lt;br /&gt;
&lt;br /&gt;
== 42. C/C++ habits to reconsider in Python ==&lt;br /&gt;
&lt;br /&gt;
When transitioning from C/C++ to Python, the following patterns should generally be reconsidered.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ habit&lt;br /&gt;
&lt;br /&gt;
| ! Typical Python approach                |&lt;br /&gt;
| ---------------------------------------- |&lt;br /&gt;
| Iterate using an integer index           |&lt;br /&gt;
| Iterate directly over the objects        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually maintain counters               |&lt;br /&gt;
| Consider &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Search manually through a container      |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually calculate container size        |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;len()&amp;lt;/code&amp;gt;                   |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit temporary variable for swapping |&lt;br /&gt;
| Use tuple unpacking                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit loop for simple transformations |&lt;br /&gt;
| Consider a comprehension                 |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Long output expressions                  |&lt;br /&gt;
| Use f-strings                            |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Copy assumed after assignment            |&lt;br /&gt;
| Remember that names refer to objects     |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Check null using equality                |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;is None&amp;lt;/code&amp;gt;                 |&lt;br /&gt;
| }                                        |&lt;br /&gt;
&lt;br /&gt;
The objective is not simply to translate C/C++ syntax into Python syntax.&lt;br /&gt;
&lt;br /&gt;
Consider:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++ style translated literally&lt;br /&gt;
&lt;br /&gt;
| ! More idiomatic Python |&lt;br /&gt;
| ----------------------- |&lt;br /&gt;
|                         |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
    print(values[i])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for value in values:&lt;br /&gt;
    print(value)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Both programs are correct.&lt;br /&gt;
&lt;br /&gt;
The second expresses the programmer&amp;#039;s intention more directly.&lt;br /&gt;
&lt;br /&gt;
== 43. Summary ==&lt;br /&gt;
&lt;br /&gt;
The main mappings introduced in this laboratory are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python                                    |   |         |&lt;br /&gt;
| ------------------------------------------- | - | ------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;{ ... }&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| indentation                                 |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;                                      |   | &amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;                              |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt;                    |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_set&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::tuple&amp;lt;/code&amp;gt;                     |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;tuple&amp;lt;/code&amp;gt;                          |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt;                      |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cin&amp;lt;/code&amp;gt;                       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| indexed iteration                           |   |         |&lt;br /&gt;
| direct iteration / &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| manual search                               |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::pow(a, b)&amp;lt;/code&amp;gt;                 |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;a ** b&amp;lt;/code&amp;gt;                         |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| integer division                            |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| }                                           |   |         |&lt;br /&gt;
&lt;br /&gt;
The central idea to retain from this laboratory is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Python should not be treated merely as C++ with different syntax. Python provides its own abstractions and idioms, and using them generally produces simpler and more readable programs.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 44. Preparation for Lab 2 ==&lt;br /&gt;
&lt;br /&gt;
Before the next laboratory:&lt;br /&gt;
&lt;br /&gt;
# complete all exercises from this laboratory;&lt;br /&gt;
&lt;br /&gt;
# ensure Python 3 is installed;&lt;br /&gt;
&lt;br /&gt;
# create a virtual environment successfully;&lt;br /&gt;
&lt;br /&gt;
# create a Git repository for the semester project;&lt;br /&gt;
&lt;br /&gt;
# become familiar with lists, dictionaries, tuples and sets;&lt;br /&gt;
&lt;br /&gt;
# review the concepts of classes, objects, inheritance and composition from C++.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Lab 2&amp;#039;&amp;#039;&amp;#039;, these concepts will be revisited from the perspective of Python&amp;#039;s object model and Pythonic object-oriented programming.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8366</id>
		<title>Lab 1</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8366"/>
		<updated>2026-09-14T05:40:55Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* 9. Boolean operators */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 1 — Python for C/C++ Programmers =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
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++.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
After completing this laboratory, you should be able to:&lt;br /&gt;
&lt;br /&gt;
* understand the basic syntax of Python;&lt;br /&gt;
* recognize the main differences between Python and C/C++;&lt;br /&gt;
* work with Python&amp;#039;s fundamental data types;&lt;br /&gt;
* use conditional statements and loops;&lt;br /&gt;
* work with lists, dictionaries, sets and tuples;&lt;br /&gt;
* define and call functions;&lt;br /&gt;
* understand basic Python object/reference semantics;&lt;br /&gt;
* recognize several common Python idioms;&lt;br /&gt;
* create and execute a simple Python project.&lt;br /&gt;
&lt;br /&gt;
Throughout this laboratory, equivalent or similar C/C++ and Python code will be presented side by side.&lt;br /&gt;
&lt;br /&gt;
**TOC**&lt;br /&gt;
&lt;br /&gt;
== 1. Running a Python program ==&lt;br /&gt;
&lt;br /&gt;
A C/C++ program normally has to be compiled before it is executed. Python programs are usually executed directly by the Python interpreter.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Hello, world!\n&amp;quot;;&lt;br /&gt;
return 0;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(&amp;quot;Hello, world!&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A Python source file normally uses the extension:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be executed using:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unlike C/C++, Python does not require a special &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
However, larger Python programs commonly use:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Program started\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
print(&amp;quot;Program started&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
if **name** == &amp;quot;**main**&amp;quot;:&lt;br /&gt;
main() &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python version is not mandatory, but this pattern will become useful when programs are split into multiple modules.&lt;br /&gt;
&lt;br /&gt;
== 2. Blocks and indentation ==&lt;br /&gt;
&lt;br /&gt;
In C and C++, braces delimit blocks of code.&lt;br /&gt;
&lt;br /&gt;
Python uses &amp;#039;&amp;#039;&amp;#039;indentation&amp;#039;&amp;#039;&amp;#039; instead.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (x &amp;gt; 10) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Large\n&amp;quot;;&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Value: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
    print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
    print(&amp;quot;Value:&amp;quot;, x)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Indentation in Python is part of the language syntax.&lt;br /&gt;
&lt;br /&gt;
The standard convention is &amp;#039;&amp;#039;&amp;#039;4 spaces per indentation level&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
For example, the following code is incorrect:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python will report an &amp;lt;code&amp;gt;IndentationError&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 3. Variables and types ==&lt;br /&gt;
&lt;br /&gt;
C/C++ variables have a declared static type.&lt;br /&gt;
&lt;br /&gt;
Python variables do not require explicit type declarations.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int count = 10;&lt;br /&gt;
double temperature = 23.5;&lt;br /&gt;
bool enabled = true;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
count = 10&lt;br /&gt;
temperature = 23.5&lt;br /&gt;
enabled = True&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python is &amp;#039;&amp;#039;&amp;#039;dynamically typed&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
This means that a variable name can later refer to an object of another type.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
// Not allowed:&lt;br /&gt;
// value = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
value = &amp;quot;hello&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This does &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; mean that Python has no types.&lt;br /&gt;
&lt;br /&gt;
Objects still have types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;typeinfo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; typeid(value).name();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
&lt;br /&gt;
print(type(value))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python will produce:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common basic types ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ type&lt;br /&gt;
! Python equivalent&lt;br /&gt;
! Example&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;42&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt; / &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;3.14&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt;&lt;br /&gt;
| No separate character type&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;A&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;str&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;Hello&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 4. Constants ==&lt;br /&gt;
&lt;br /&gt;
C++ can enforce that a value cannot be modified using &amp;lt;code&amp;gt;const&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python does not enforce constants at language level.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
const double PI = 3.1415926535;&lt;br /&gt;
const int MAX_USERS = 100; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
PI = 3.1415926535&lt;br /&gt;
MAX_USERS = 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
By convention, Python names that should be treated as constants are written using uppercase letters.&lt;br /&gt;
&lt;br /&gt;
Nothing prevents the programmer from changing them.&lt;br /&gt;
&lt;br /&gt;
== 5. Arithmetic operators ==&lt;br /&gt;
&lt;br /&gt;
Most arithmetic operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
int sum  = a + b;&lt;br /&gt;
int diff = a - b;&lt;br /&gt;
int prod = a * b;&lt;br /&gt;
int div  = a / b;&lt;br /&gt;
int rem  = a % b; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
sum_ = a + b&lt;br /&gt;
diff = a - b&lt;br /&gt;
prod = a * b&lt;br /&gt;
div = a // b&lt;br /&gt;
rem = a % b &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
There is an important difference between &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a / b;   // 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
print(a / b)   # 3.3333333333333335&lt;br /&gt;
print(a // b)  # 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In C/C++, dividing two integers performs integer division.&lt;br /&gt;
&lt;br /&gt;
In Python, the &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; operator always performs floating-point division, while &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt; performs floor division.&lt;br /&gt;
&lt;br /&gt;
Python also provides an exponentiation operator:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;cmath&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double result = std::pow(2, 8); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
result = 2 ** 8 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 6. Printing values ==&lt;br /&gt;
&lt;br /&gt;
Output using &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt; is replaced by the built-in &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
int age = 21;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; is &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; age&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; years old\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
age = 21&lt;br /&gt;
&lt;br /&gt;
print(name, &amp;quot;is&amp;quot;, age, &amp;quot;years old&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python also provides formatted string literals, commonly called &amp;#039;&amp;#039;&amp;#039;f-strings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Temperature: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; temperature&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; C\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Temperature: {temperature} C&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Expressions can appear directly inside an f-string:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Result: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; a + b&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Result: {a + b}&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 7. Reading input ==&lt;br /&gt;
&lt;br /&gt;
Console input is performed using &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An important difference is that &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt; &amp;#039;&amp;#039;&amp;#039;always returns a string&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int age;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Age: &amp;quot;;&lt;br /&gt;
std::cin &amp;gt;&amp;gt; age; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = int(input(&amp;quot;Age: &amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Without the conversion:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = input(&amp;quot;Age: &amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; is a string.&lt;br /&gt;
&lt;br /&gt;
Conversions are explicit:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = std::stoi(text);&lt;br /&gt;
double b = std::stod(text); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = int(text)&lt;br /&gt;
b = float(text) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 8. Conditional statements ==&lt;br /&gt;
&lt;br /&gt;
The overall structure is similar, but Python does not use parentheses or braces.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (temperature &amp;gt; 30) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Hot\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else if (temperature &amp;gt; 20) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Warm\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Cold\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if temperature &amp;gt; 30:&lt;br /&gt;
print(&amp;quot;Hot&amp;quot;)&lt;br /&gt;
elif temperature &amp;gt; 20:&lt;br /&gt;
print(&amp;quot;Warm&amp;quot;)&lt;br /&gt;
else:&lt;br /&gt;
print(&amp;quot;Cold&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python uses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
elif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
instead of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
else if&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 9. Boolean operators ==&lt;br /&gt;
&lt;br /&gt;
Python uses words instead of the C/C++ symbolic logical operators.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;amp;&amp;amp;amp;&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;#124;&amp;amp;#124;&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (age &amp;gt;= 18 &amp;amp;&amp;amp; enabled) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if (!enabled) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if age &amp;gt;= 18 and enabled:&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
if not enabled:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 10. Comparison operators ==&lt;br /&gt;
&lt;br /&gt;
Most comparison operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Operation&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python              |&lt;br /&gt;
| --------------------- |&lt;br /&gt;
| equality              |&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| inequality            |&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| less than             |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                     |&lt;br /&gt;
| greater than          |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                     |&lt;br /&gt;
| less than or equal    |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| greater than or equal |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| }                     |&lt;br /&gt;
&lt;br /&gt;
Python also allows chained comparisons.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (0 &amp;lt;= x &amp;amp;&amp;amp; x &amp;lt; 100) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 0 &amp;lt;= x &amp;lt; 100:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 11. The ternary operator ==&lt;br /&gt;
&lt;br /&gt;
Both languages support conditional expressions.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int maximum = (a &amp;gt; b) ? a : b;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
maximum = a if a &amp;gt; b else b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python&amp;#039;s order is different:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
value_if_true if condition else value_if_false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 12. While loops ==&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;code&amp;gt;while&amp;lt;/code&amp;gt; loop is very similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int i = 0;&lt;br /&gt;
&lt;br /&gt;
while (i &amp;lt; 5) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
i++;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
&lt;br /&gt;
while i &amp;lt; 5:&lt;br /&gt;
print(i)&lt;br /&gt;
i += 1 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not have &amp;lt;code&amp;gt;++&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;--&amp;lt;/code&amp;gt; operators.&lt;br /&gt;
&lt;br /&gt;
Therefore:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i += 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is used instead.&lt;br /&gt;
&lt;br /&gt;
== 13. For loops ==&lt;br /&gt;
&lt;br /&gt;
This is one of the first important conceptual differences.&lt;br /&gt;
&lt;br /&gt;
In C/C++, a &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement commonly controls a numeric index.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement iterates over objects.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 5; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;range(5)&amp;lt;/code&amp;gt; generates values conceptually equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
0, 1, 2, 3, 4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A different starting value can be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 2; i &amp;lt; 10; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(2, 10):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A step can also be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i += 2) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(0, 10, 2):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 14. Iterating over containers ==&lt;br /&gt;
&lt;br /&gt;
Suppose we want to print every element of a vector/list.&lt;br /&gt;
&lt;br /&gt;
An index-based implementation is possible:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; values.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[i] &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
print(values[i]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
However, this is generally &amp;#039;&amp;#039;&amp;#039;not the preferred Python style&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
If the index is not required, iterate directly over the elements.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; value : values) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; value &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for value in values:&lt;br /&gt;
print(value) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The second Python version should normally be preferred.&lt;br /&gt;
&lt;br /&gt;
== 15. When both index and value are required ==&lt;br /&gt;
&lt;br /&gt;
C/C++ programmers frequently use an explicit index.&lt;br /&gt;
&lt;br /&gt;
Python provides &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;std::string&amp;gt; names = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; names.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; names[i]&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
names = [&amp;quot;Ana&amp;quot;, &amp;quot;Bob&amp;quot;, &amp;quot;Carol&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
for i, name in enumerate(names):&lt;br /&gt;
print(i, &amp;quot;:&amp;quot;, name) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This introduces another frequently used Python feature: &amp;#039;&amp;#039;&amp;#039;unpacking&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== 16. Lists ==&lt;br /&gt;
&lt;br /&gt;
The closest commonly used Python equivalent to &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.push_back(40);&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[0];&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values.size(); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
values.append(40)&lt;br /&gt;
&lt;br /&gt;
print(values[0])&lt;br /&gt;
print(len(values)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python lists can contain objects of different types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// A normal std::vector cannot&lt;br /&gt;
// contain unrelated types.&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
10, 20, 30&lt;br /&gt;
}; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [&lt;br /&gt;
10,&lt;br /&gt;
3.14,&lt;br /&gt;
&amp;quot;hello&amp;quot;,&lt;br /&gt;
True&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Although Python allows this, homogeneous collections are generally easier to understand and maintain when the elements conceptually represent the same kind of data.&lt;br /&gt;
&lt;br /&gt;
== 17. Negative indexing ==&lt;br /&gt;
&lt;br /&gt;
Python allows indexing from the end of a sequence.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {10, 20, 30, 40};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 1]; // 40&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 2]; // 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40]&lt;br /&gt;
&lt;br /&gt;
print(v[-1])  # 40&lt;br /&gt;
print(v[-2])  # 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 18. Slicing ==&lt;br /&gt;
&lt;br /&gt;
Python sequences support slicing directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {&lt;br /&gt;
    10, 20, 30, 40, 50&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; part(&lt;br /&gt;
v.begin() + 1,&lt;br /&gt;
v.begin() + 4&lt;br /&gt;
); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40, 50]&lt;br /&gt;
&lt;br /&gt;
part = v[1:4] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The result is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[20, 30, 40]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
General syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
sequence[start:stop:step]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// Requires an explicit loop or&lt;br /&gt;
// another algorithm.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [0, 1, 2, 3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
print(v[1:4])   # [1, 2, 3]&lt;br /&gt;
print(v[:3])    # [0, 1, 2]&lt;br /&gt;
print(v[3:])    # [3, 4, 5]&lt;br /&gt;
print(v[::2])   # [0, 2, 4]&lt;br /&gt;
print(v[::-1])  # [5, 4, 3, 2, 1, 0] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 19. Membership testing ==&lt;br /&gt;
&lt;br /&gt;
Checking whether an element exists in a container is commonly performed using &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
if (std::find(&lt;br /&gt;
values.begin(),&lt;br /&gt;
values.end(),&lt;br /&gt;
20&lt;br /&gt;
) != values.end()) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The opposite test uses &amp;lt;code&amp;gt;not in&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 100 not in values:&lt;br /&gt;
    print(&amp;quot;Not found&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 20. Dictionaries ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt; is similar to a C++ associative container such as &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades;&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10;&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; grades[&amp;quot;Ana&amp;quot;]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {}&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8&lt;br /&gt;
&lt;br /&gt;
print(grades[&amp;quot;Ana&amp;quot;]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A dictionary can also be initialized directly:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades = {&lt;br /&gt;
    {&amp;quot;Ana&amp;quot;, 10},&lt;br /&gt;
    {&amp;quot;Bob&amp;quot;, 8},&lt;br /&gt;
    {&amp;quot;Carol&amp;quot;, 9}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 10,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 8,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 9,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Iterating over keys and values:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; grade&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
    print(name, &amp;quot;:&amp;quot;, grade)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 21. Sets ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; represents a collection containing unique values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.insert(40);&lt;br /&gt;
&lt;br /&gt;
if (20 != values.end()) {&lt;br /&gt;
// conceptually test membership&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = {10, 20, 30}&lt;br /&gt;
&lt;br /&gt;
values.add(40)&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A more correct C++ membership test would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (values.find(20) != values.end()) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets are particularly useful when:&lt;br /&gt;
&lt;br /&gt;
* duplicate values should be removed;&lt;br /&gt;
* fast membership tests are required;&lt;br /&gt;
* mathematical set operations are useful.&lt;br /&gt;
&lt;br /&gt;
== 22. Tuples ==&lt;br /&gt;
&lt;br /&gt;
Tuples are similar to lists, but they are immutable.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::tuple&amp;lt;int, double, std::string&amp;gt; data {&lt;br /&gt;
    10,&lt;br /&gt;
    3.14,&lt;br /&gt;
    &amp;quot;test&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
data = (10, 3.14, &amp;quot;test&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python uses tuples extensively for returning or unpacking multiple values.&lt;br /&gt;
&lt;br /&gt;
== 23. Functions ==&lt;br /&gt;
&lt;br /&gt;
Functions are declared using &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    return a + b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int result = add(2, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def add(a, b):&lt;br /&gt;
return a + b&lt;br /&gt;
&lt;br /&gt;
result = add(2, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not require parameter or return types to be declared.&lt;br /&gt;
&lt;br /&gt;
However, Python supports optional &amp;#039;&amp;#039;&amp;#039;type hints&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
double average(&lt;br /&gt;
    double a,&lt;br /&gt;
    double b&lt;br /&gt;
) {&lt;br /&gt;
    return (a + b) / 2.0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(&lt;br /&gt;
    a: float,&lt;br /&gt;
    b: float&lt;br /&gt;
) -&amp;gt; float:&lt;br /&gt;
    return (a + b) / 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Type hints do not normally enforce types at runtime.&lt;br /&gt;
&lt;br /&gt;
They provide information for:&lt;br /&gt;
&lt;br /&gt;
* the programmer;&lt;br /&gt;
* IDEs;&lt;br /&gt;
* static analysis tools;&lt;br /&gt;
* documentation.&lt;br /&gt;
&lt;br /&gt;
== 24. Default parameters ==&lt;br /&gt;
&lt;br /&gt;
Both C++ and Python support default parameter values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void greet(&lt;br /&gt;
    const std::string&amp;amp; name = &amp;quot;World&amp;quot;&lt;br /&gt;
) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Hello, &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
greet();&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def greet(name=&amp;quot;World&amp;quot;):&lt;br /&gt;
print(f&amp;quot;Hello, {name}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
greet()&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 25. Named arguments ==&lt;br /&gt;
&lt;br /&gt;
Python allows function arguments to be specified by name.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void connect(&lt;br /&gt;
    std::string host,&lt;br /&gt;
    int port,&lt;br /&gt;
    bool secure&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
connect(&amp;quot;server.local&amp;quot;, 443, true); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def connect(host, port, secure):&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
connect(&lt;br /&gt;
host=&amp;quot;server.local&amp;quot;,&lt;br /&gt;
port=443,&lt;br /&gt;
secure=True&lt;br /&gt;
) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This can make calls with several parameters easier to understand.&lt;br /&gt;
&lt;br /&gt;
== 26. Returning multiple values ==&lt;br /&gt;
&lt;br /&gt;
In C++, multiple return values typically require a structure, tuple, pair or output parameters.&lt;br /&gt;
&lt;br /&gt;
Python commonly uses tuples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::pair&amp;lt;int, int&amp;gt; minmax(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    if (a &amp;lt; b)&lt;br /&gt;
        return {a, b};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return {b, a};&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
auto [minimum, maximum] = minmax(10, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def minmax(a, b):&lt;br /&gt;
if a &amp;lt; b:&lt;br /&gt;
return a, b&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return b, a&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
minimum, maximum = minmax(10, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is another example of tuple unpacking.&lt;br /&gt;
&lt;br /&gt;
== 27. Swapping variables ==&lt;br /&gt;
&lt;br /&gt;
A temporary variable is normally used in C.&lt;br /&gt;
&lt;br /&gt;
Python supports unpacking directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int temp = a;&lt;br /&gt;
a = b;&lt;br /&gt;
b = temp;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a, b = b, a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 28. Strings ==&lt;br /&gt;
&lt;br /&gt;
Python strings behave more like C++ &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt; objects than traditional C character arrays.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text.size();&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text[0]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(len(text))&lt;br /&gt;
print(text[0]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
String concatenation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string first = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
std::string second = &amp;quot;World&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::string result =&lt;br /&gt;
first + &amp;quot; &amp;quot; + second; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
first = &amp;quot;Hello&amp;quot;&lt;br /&gt;
second = &amp;quot;World&amp;quot;&lt;br /&gt;
&lt;br /&gt;
result = first + &amp;quot; &amp;quot; + second &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python strings provide many built-in operations:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Usually requires algorithms,&lt;br /&gt;
// explicit transformations,&lt;br /&gt;
// or utility functions. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(text.upper())&lt;br /&gt;
print(text.lower())&lt;br /&gt;
print(text.startswith(&amp;quot;he&amp;quot;))&lt;br /&gt;
print(text.endswith(&amp;quot;lo&amp;quot;))&lt;br /&gt;
print(text.replace(&amp;quot;l&amp;quot;, &amp;quot;x&amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 29. Lists versus arrays: important semantic difference ==&lt;br /&gt;
&lt;br /&gt;
Consider assigning one collection to another.&lt;br /&gt;
&lt;br /&gt;
With normal C++ value semantics, copying a vector produces another vector.&lt;br /&gt;
&lt;br /&gt;
Python assignment behaves differently.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&lt;br /&gt;
b[0] = 100;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a[0];  // 1&lt;br /&gt;
std::cout &amp;lt;&amp;lt; b[0];  // 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
b = a&lt;br /&gt;
&lt;br /&gt;
b[0] = 100&lt;br /&gt;
&lt;br /&gt;
print(a[0])  # 100&lt;br /&gt;
print(b[0])  # 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This happens because:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
does not copy the list.&lt;br /&gt;
&lt;br /&gt;
Both names refer to the &amp;#039;&amp;#039;&amp;#039;same list object&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Conceptually:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
       +-------------+&lt;br /&gt;
a ---&amp;gt; | [1, 2, 3]   |&lt;br /&gt;
       +-------------+&lt;br /&gt;
          ^&lt;br /&gt;
          |&lt;br /&gt;
b --------+&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To explicitly create a shallow copy:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a.copy()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = list(a)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This distinction is extremely important when using Python.&lt;br /&gt;
&lt;br /&gt;
== 30. Equality versus identity ==&lt;br /&gt;
&lt;br /&gt;
Python distinguishes between:&lt;br /&gt;
&lt;br /&gt;
* whether two objects contain equal values;&lt;br /&gt;
* whether two names refer to the exact same object.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; (a == b);  // true&lt;br /&gt;
&lt;br /&gt;
// Address comparison would be needed&lt;br /&gt;
// to determine object identity. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
b = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
print(a == b)  # True&lt;br /&gt;
print(a is b)  # False &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In Python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
==  tests value equality&lt;br /&gt;
is  tests object identity&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not use &amp;lt;code&amp;gt;is&amp;lt;/code&amp;gt; when you intend to compare values.&lt;br /&gt;
&lt;br /&gt;
A common correct use is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if value is None:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 31. None and nullptr ==&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; represents the absence of a value.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Object* ptr = nullptr;&lt;br /&gt;
&lt;br /&gt;
if (ptr == nullptr) {&lt;br /&gt;
...&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = None&lt;br /&gt;
&lt;br /&gt;
if value is None:&lt;br /&gt;
... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 32. Exceptions ==&lt;br /&gt;
&lt;br /&gt;
Exception handling is conceptually similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    int value = std::stoi(text);&lt;br /&gt;
}&lt;br /&gt;
catch (const std::exception&amp;amp; e) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Invalid value\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    value = int(text)&lt;br /&gt;
except ValueError:&lt;br /&gt;
    print(&amp;quot;Invalid value&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python generally uses exceptions rather than special error return values for many failure conditions.&lt;br /&gt;
&lt;br /&gt;
== 33. A first Pythonic transformation ==&lt;br /&gt;
&lt;br /&gt;
Consider creating a vector/list containing the squares of the numbers from 0 to 9.&lt;br /&gt;
&lt;br /&gt;
A direct translation from C++ could look like this:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — direct translation |&lt;br /&gt;
| ----------------------------- |&lt;br /&gt;
|                               |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = []&lt;br /&gt;
&lt;br /&gt;
for i in range(10):&lt;br /&gt;
squares.append(i * i) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python code is correct.&lt;br /&gt;
&lt;br /&gt;
However, Python provides a concise construct called a &amp;#039;&amp;#039;&amp;#039;list comprehension&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — idiomatic |&lt;br /&gt;
| -------------------- |&lt;br /&gt;
|                      |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A condition can also be included.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
if (i % 2 == 0) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
}&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
if i % 2 == 0&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
An important objective of this course is not only to write programs that work in Python, but to learn to write programs in a style appropriate for Python.&lt;br /&gt;
&lt;br /&gt;
== 34. Example: processing student grades ==&lt;br /&gt;
&lt;br /&gt;
Consider the following problem:&lt;br /&gt;
&lt;br /&gt;
* store the grades of several students;&lt;br /&gt;
* compute the average;&lt;br /&gt;
* print the students whose grade is greater than or equal to the average.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;unordered_map&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt; grades = {&lt;br /&gt;
{&amp;quot;Ana&amp;quot;, 9.5},&lt;br /&gt;
{&amp;quot;Bob&amp;quot;, 7.0},&lt;br /&gt;
{&amp;quot;Carol&amp;quot;, 8.5},&lt;br /&gt;
{&amp;quot;Dan&amp;quot;, 6.0}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
double sum = 0.0;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    sum += grade;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
double average = sum / grades.size();&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Average: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; average&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    if (grade &amp;gt;= average) {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
                  &amp;lt;&amp;lt; grade&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
&amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
&amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
&amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
&amp;quot;Dan&amp;quot;: 6.0,&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
average = sum(grades.values()) / len(grades)&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;Average: {average:.2f}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
if grade &amp;gt;= average:&lt;br /&gt;
print(f&amp;quot;{name}: {grade}&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that the Python version uses existing language functionality rather than manually reproducing every operation.&lt;br /&gt;
&lt;br /&gt;
In particular:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sum(grades.values())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
expresses the intention directly.&lt;br /&gt;
&lt;br /&gt;
== 35. Exercise 1 — Translation ==&lt;br /&gt;
&lt;br /&gt;
Translate the following C++ program into Python.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
12, 7, 18, 3, 24, 11&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
int sum = 0;&lt;br /&gt;
int count = 0;&lt;br /&gt;
&lt;br /&gt;
for (int value : values) {&lt;br /&gt;
    if (value % 2 == 0) {&lt;br /&gt;
        sum += value;&lt;br /&gt;
        count++;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Count: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; count&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Sum: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; sum&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Your implementation:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# TODO&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
After implementing a direct translation, try to simplify the Python implementation.&lt;br /&gt;
&lt;br /&gt;
== 36. Exercise 2 — Statistics ==&lt;br /&gt;
&lt;br /&gt;
Write a Python program that:&lt;br /&gt;
&lt;br /&gt;
# reads a sequence of integer values;&lt;br /&gt;
&lt;br /&gt;
# stores them in a list;&lt;br /&gt;
&lt;br /&gt;
# prints the minimum value;&lt;br /&gt;
&lt;br /&gt;
# prints the maximum value;&lt;br /&gt;
&lt;br /&gt;
# prints the average;&lt;br /&gt;
&lt;br /&gt;
# prints how many values are above the average.&lt;br /&gt;
&lt;br /&gt;
The equivalent C++ structure might begin as follows:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values;&lt;br /&gt;
&lt;br /&gt;
// Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
int minimum = ...;&lt;br /&gt;
int maximum = ...;&lt;br /&gt;
double average = ...; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = []&lt;br /&gt;
&lt;br /&gt;
# Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
minimum = ...&lt;br /&gt;
maximum = ...&lt;br /&gt;
average = ... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Useful Python functions include:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
len(...)&lt;br /&gt;
sum(...)&lt;br /&gt;
min(...)&lt;br /&gt;
max(...)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 37. Exercise 3 — Word frequency ==&lt;br /&gt;
&lt;br /&gt;
Given a sentence, count how many times each word occurs.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python is simple and python is powerful&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
should produce information equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python -&amp;gt; 2&lt;br /&gt;
is -&amp;gt; 2&lt;br /&gt;
simple -&amp;gt; 1&lt;br /&gt;
and -&amp;gt; 1&lt;br /&gt;
powerful -&amp;gt; 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A possible C++ data structure and its Python equivalent are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt;&lt;br /&gt;
    frequencies;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
frequencies = {}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sentence.split()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to separate the sentence into words.&lt;br /&gt;
&lt;br /&gt;
== 38. Exercise 4 — Remove duplicates ==&lt;br /&gt;
&lt;br /&gt;
Given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [4, 2, 7, 4, 2, 9, 7, 1]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
create a collection containing every distinct value.&lt;br /&gt;
&lt;br /&gt;
Think about the following C++ and Python data structures:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; unique_values;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
unique_values = set()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Try to solve the problem both:&lt;br /&gt;
&lt;br /&gt;
# using an explicit loop;&lt;br /&gt;
&lt;br /&gt;
# using Python&amp;#039;s built-in functionality.&lt;br /&gt;
&lt;br /&gt;
== 39. Exercise 5 — Small problem ==&lt;br /&gt;
&lt;br /&gt;
Write a program that receives a list of students and their grades and determines:&lt;br /&gt;
&lt;br /&gt;
* the class average;&lt;br /&gt;
* the student with the highest grade;&lt;br /&gt;
* all students who passed;&lt;br /&gt;
* all students whose grade is above the class average.&lt;br /&gt;
&lt;br /&gt;
Suggested representation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt;&lt;br /&gt;
    grades;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
    &amp;quot;Dan&amp;quot;: 4.0,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Avoid using numeric indices unless an index is actually required.&lt;br /&gt;
&lt;br /&gt;
== 40. Creating a project environment ==&lt;br /&gt;
&lt;br /&gt;
Python applications commonly use a &amp;#039;&amp;#039;&amp;#039;virtual environment&amp;#039;&amp;#039;&amp;#039; to isolate project dependencies.&lt;br /&gt;
&lt;br /&gt;
Create a directory for the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir python-project&lt;br /&gt;
cd python-project&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a virtual environment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 -m venv .venv&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Linux/macOS:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source .venv/bin/activate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Windows PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
.venv\Scripts\Activate.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command prompt will normally indicate that the environment is active.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
(.venv) student@computer:~/python-project$&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Packages installed using &amp;lt;code&amp;gt;pip&amp;lt;/code&amp;gt; will now be installed inside this environment.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip install requests&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To inspect installed packages:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip list&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 41. Recommended initial project structure ==&lt;br /&gt;
&lt;br /&gt;
For the first laboratories, the following structure is sufficient:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python-project/&lt;br /&gt;
├── .venv/&lt;br /&gt;
├── main.py&lt;br /&gt;
├── README.md&lt;br /&gt;
└── .gitignore&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A basic &amp;lt;code&amp;gt;.gitignore&amp;lt;/code&amp;gt; should contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.venv/&lt;br /&gt;
__pycache__/&lt;br /&gt;
*.pyc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; commit the virtual environment to Git.&lt;br /&gt;
&lt;br /&gt;
== 42. C/C++ habits to reconsider in Python ==&lt;br /&gt;
&lt;br /&gt;
When transitioning from C/C++ to Python, the following patterns should generally be reconsidered.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ habit&lt;br /&gt;
&lt;br /&gt;
| ! Typical Python approach                |&lt;br /&gt;
| ---------------------------------------- |&lt;br /&gt;
| Iterate using an integer index           |&lt;br /&gt;
| Iterate directly over the objects        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually maintain counters               |&lt;br /&gt;
| Consider &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Search manually through a container      |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually calculate container size        |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;len()&amp;lt;/code&amp;gt;                   |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit temporary variable for swapping |&lt;br /&gt;
| Use tuple unpacking                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit loop for simple transformations |&lt;br /&gt;
| Consider a comprehension                 |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Long output expressions                  |&lt;br /&gt;
| Use f-strings                            |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Copy assumed after assignment            |&lt;br /&gt;
| Remember that names refer to objects     |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Check null using equality                |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;is None&amp;lt;/code&amp;gt;                 |&lt;br /&gt;
| }                                        |&lt;br /&gt;
&lt;br /&gt;
The objective is not simply to translate C/C++ syntax into Python syntax.&lt;br /&gt;
&lt;br /&gt;
Consider:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++ style translated literally&lt;br /&gt;
&lt;br /&gt;
| ! More idiomatic Python |&lt;br /&gt;
| ----------------------- |&lt;br /&gt;
|                         |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
    print(values[i])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for value in values:&lt;br /&gt;
    print(value)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Both programs are correct.&lt;br /&gt;
&lt;br /&gt;
The second expresses the programmer&amp;#039;s intention more directly.&lt;br /&gt;
&lt;br /&gt;
== 43. Summary ==&lt;br /&gt;
&lt;br /&gt;
The main mappings introduced in this laboratory are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python                                    |   |         |&lt;br /&gt;
| ------------------------------------------- | - | ------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;{ ... }&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| indentation                                 |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;                                      |   | &amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;                              |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt;                    |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_set&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::tuple&amp;lt;/code&amp;gt;                     |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;tuple&amp;lt;/code&amp;gt;                          |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt;                      |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cin&amp;lt;/code&amp;gt;                       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| indexed iteration                           |   |         |&lt;br /&gt;
| direct iteration / &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| manual search                               |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::pow(a, b)&amp;lt;/code&amp;gt;                 |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;a ** b&amp;lt;/code&amp;gt;                         |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| integer division                            |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| }                                           |   |         |&lt;br /&gt;
&lt;br /&gt;
The central idea to retain from this laboratory is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Python should not be treated merely as C++ with different syntax. Python provides its own abstractions and idioms, and using them generally produces simpler and more readable programs.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 44. Preparation for Lab 2 ==&lt;br /&gt;
&lt;br /&gt;
Before the next laboratory:&lt;br /&gt;
&lt;br /&gt;
# complete all exercises from this laboratory;&lt;br /&gt;
&lt;br /&gt;
# ensure Python 3 is installed;&lt;br /&gt;
&lt;br /&gt;
# create a virtual environment successfully;&lt;br /&gt;
&lt;br /&gt;
# create a Git repository for the semester project;&lt;br /&gt;
&lt;br /&gt;
# become familiar with lists, dictionaries, tuples and sets;&lt;br /&gt;
&lt;br /&gt;
# review the concepts of classes, objects, inheritance and composition from C++.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Lab 2&amp;#039;&amp;#039;&amp;#039;, these concepts will be revisited from the perspective of Python&amp;#039;s object model and Pythonic object-oriented programming.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8365</id>
		<title>Lab 1</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8365"/>
		<updated>2026-09-14T05:35:56Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* 8. Conditional statements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 1 — Python for C/C++ Programmers =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
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++.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
After completing this laboratory, you should be able to:&lt;br /&gt;
&lt;br /&gt;
* understand the basic syntax of Python;&lt;br /&gt;
* recognize the main differences between Python and C/C++;&lt;br /&gt;
* work with Python&amp;#039;s fundamental data types;&lt;br /&gt;
* use conditional statements and loops;&lt;br /&gt;
* work with lists, dictionaries, sets and tuples;&lt;br /&gt;
* define and call functions;&lt;br /&gt;
* understand basic Python object/reference semantics;&lt;br /&gt;
* recognize several common Python idioms;&lt;br /&gt;
* create and execute a simple Python project.&lt;br /&gt;
&lt;br /&gt;
Throughout this laboratory, equivalent or similar C/C++ and Python code will be presented side by side.&lt;br /&gt;
&lt;br /&gt;
**TOC**&lt;br /&gt;
&lt;br /&gt;
== 1. Running a Python program ==&lt;br /&gt;
&lt;br /&gt;
A C/C++ program normally has to be compiled before it is executed. Python programs are usually executed directly by the Python interpreter.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Hello, world!\n&amp;quot;;&lt;br /&gt;
return 0;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(&amp;quot;Hello, world!&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A Python source file normally uses the extension:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be executed using:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unlike C/C++, Python does not require a special &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
However, larger Python programs commonly use:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Program started\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
print(&amp;quot;Program started&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
if **name** == &amp;quot;**main**&amp;quot;:&lt;br /&gt;
main() &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python version is not mandatory, but this pattern will become useful when programs are split into multiple modules.&lt;br /&gt;
&lt;br /&gt;
== 2. Blocks and indentation ==&lt;br /&gt;
&lt;br /&gt;
In C and C++, braces delimit blocks of code.&lt;br /&gt;
&lt;br /&gt;
Python uses &amp;#039;&amp;#039;&amp;#039;indentation&amp;#039;&amp;#039;&amp;#039; instead.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (x &amp;gt; 10) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Large\n&amp;quot;;&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Value: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
    print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
    print(&amp;quot;Value:&amp;quot;, x)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Indentation in Python is part of the language syntax.&lt;br /&gt;
&lt;br /&gt;
The standard convention is &amp;#039;&amp;#039;&amp;#039;4 spaces per indentation level&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
For example, the following code is incorrect:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python will report an &amp;lt;code&amp;gt;IndentationError&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 3. Variables and types ==&lt;br /&gt;
&lt;br /&gt;
C/C++ variables have a declared static type.&lt;br /&gt;
&lt;br /&gt;
Python variables do not require explicit type declarations.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int count = 10;&lt;br /&gt;
double temperature = 23.5;&lt;br /&gt;
bool enabled = true;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
count = 10&lt;br /&gt;
temperature = 23.5&lt;br /&gt;
enabled = True&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python is &amp;#039;&amp;#039;&amp;#039;dynamically typed&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
This means that a variable name can later refer to an object of another type.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
// Not allowed:&lt;br /&gt;
// value = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
value = &amp;quot;hello&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This does &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; mean that Python has no types.&lt;br /&gt;
&lt;br /&gt;
Objects still have types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;typeinfo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; typeid(value).name();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
&lt;br /&gt;
print(type(value))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python will produce:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common basic types ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ type&lt;br /&gt;
! Python equivalent&lt;br /&gt;
! Example&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;42&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt; / &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;3.14&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt;&lt;br /&gt;
| No separate character type&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;A&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;str&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;Hello&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 4. Constants ==&lt;br /&gt;
&lt;br /&gt;
C++ can enforce that a value cannot be modified using &amp;lt;code&amp;gt;const&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python does not enforce constants at language level.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
const double PI = 3.1415926535;&lt;br /&gt;
const int MAX_USERS = 100; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
PI = 3.1415926535&lt;br /&gt;
MAX_USERS = 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
By convention, Python names that should be treated as constants are written using uppercase letters.&lt;br /&gt;
&lt;br /&gt;
Nothing prevents the programmer from changing them.&lt;br /&gt;
&lt;br /&gt;
== 5. Arithmetic operators ==&lt;br /&gt;
&lt;br /&gt;
Most arithmetic operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
int sum  = a + b;&lt;br /&gt;
int diff = a - b;&lt;br /&gt;
int prod = a * b;&lt;br /&gt;
int div  = a / b;&lt;br /&gt;
int rem  = a % b; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
sum_ = a + b&lt;br /&gt;
diff = a - b&lt;br /&gt;
prod = a * b&lt;br /&gt;
div = a // b&lt;br /&gt;
rem = a % b &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
There is an important difference between &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a / b;   // 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
print(a / b)   # 3.3333333333333335&lt;br /&gt;
print(a // b)  # 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In C/C++, dividing two integers performs integer division.&lt;br /&gt;
&lt;br /&gt;
In Python, the &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; operator always performs floating-point division, while &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt; performs floor division.&lt;br /&gt;
&lt;br /&gt;
Python also provides an exponentiation operator:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;cmath&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double result = std::pow(2, 8); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
result = 2 ** 8 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 6. Printing values ==&lt;br /&gt;
&lt;br /&gt;
Output using &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt; is replaced by the built-in &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
int age = 21;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; is &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; age&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; years old\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
age = 21&lt;br /&gt;
&lt;br /&gt;
print(name, &amp;quot;is&amp;quot;, age, &amp;quot;years old&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python also provides formatted string literals, commonly called &amp;#039;&amp;#039;&amp;#039;f-strings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Temperature: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; temperature&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; C\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Temperature: {temperature} C&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Expressions can appear directly inside an f-string:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Result: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; a + b&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Result: {a + b}&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 7. Reading input ==&lt;br /&gt;
&lt;br /&gt;
Console input is performed using &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An important difference is that &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt; &amp;#039;&amp;#039;&amp;#039;always returns a string&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int age;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Age: &amp;quot;;&lt;br /&gt;
std::cin &amp;gt;&amp;gt; age; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = int(input(&amp;quot;Age: &amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Without the conversion:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = input(&amp;quot;Age: &amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; is a string.&lt;br /&gt;
&lt;br /&gt;
Conversions are explicit:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = std::stoi(text);&lt;br /&gt;
double b = std::stod(text); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = int(text)&lt;br /&gt;
b = float(text) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 8. Conditional statements ==&lt;br /&gt;
&lt;br /&gt;
The overall structure is similar, but Python does not use parentheses or braces.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (temperature &amp;gt; 30) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Hot\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else if (temperature &amp;gt; 20) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Warm\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Cold\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if temperature &amp;gt; 30:&lt;br /&gt;
print(&amp;quot;Hot&amp;quot;)&lt;br /&gt;
elif temperature &amp;gt; 20:&lt;br /&gt;
print(&amp;quot;Warm&amp;quot;)&lt;br /&gt;
else:&lt;br /&gt;
print(&amp;quot;Cold&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python uses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
elif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
instead of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
else if&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 9. Boolean operators ==&lt;br /&gt;
&lt;br /&gt;
Python uses words instead of the C/C++ symbolic logical operators.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python         |   |         |&lt;br /&gt;
| ---------------- | - | ------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt;  |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| -                |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;           |   | &amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;  |   |         |&lt;br /&gt;
| -                |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;   |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| }                |   |         |&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (age &amp;gt;= 18 &amp;amp;&amp;amp; enabled) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if (!enabled) {&lt;br /&gt;
...&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if age &amp;gt;= 18 and enabled:&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
if not enabled:&lt;br /&gt;
... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 10. Comparison operators ==&lt;br /&gt;
&lt;br /&gt;
Most comparison operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Operation&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python              |&lt;br /&gt;
| --------------------- |&lt;br /&gt;
| equality              |&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| inequality            |&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| less than             |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                     |&lt;br /&gt;
| greater than          |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                     |&lt;br /&gt;
| less than or equal    |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| greater than or equal |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| }                     |&lt;br /&gt;
&lt;br /&gt;
Python also allows chained comparisons.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (0 &amp;lt;= x &amp;amp;&amp;amp; x &amp;lt; 100) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 0 &amp;lt;= x &amp;lt; 100:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 11. The ternary operator ==&lt;br /&gt;
&lt;br /&gt;
Both languages support conditional expressions.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int maximum = (a &amp;gt; b) ? a : b;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
maximum = a if a &amp;gt; b else b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python&amp;#039;s order is different:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
value_if_true if condition else value_if_false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 12. While loops ==&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;code&amp;gt;while&amp;lt;/code&amp;gt; loop is very similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int i = 0;&lt;br /&gt;
&lt;br /&gt;
while (i &amp;lt; 5) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
i++;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
&lt;br /&gt;
while i &amp;lt; 5:&lt;br /&gt;
print(i)&lt;br /&gt;
i += 1 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not have &amp;lt;code&amp;gt;++&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;--&amp;lt;/code&amp;gt; operators.&lt;br /&gt;
&lt;br /&gt;
Therefore:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i += 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is used instead.&lt;br /&gt;
&lt;br /&gt;
== 13. For loops ==&lt;br /&gt;
&lt;br /&gt;
This is one of the first important conceptual differences.&lt;br /&gt;
&lt;br /&gt;
In C/C++, a &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement commonly controls a numeric index.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement iterates over objects.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 5; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;range(5)&amp;lt;/code&amp;gt; generates values conceptually equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
0, 1, 2, 3, 4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A different starting value can be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 2; i &amp;lt; 10; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(2, 10):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A step can also be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i += 2) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(0, 10, 2):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 14. Iterating over containers ==&lt;br /&gt;
&lt;br /&gt;
Suppose we want to print every element of a vector/list.&lt;br /&gt;
&lt;br /&gt;
An index-based implementation is possible:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; values.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[i] &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
print(values[i]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
However, this is generally &amp;#039;&amp;#039;&amp;#039;not the preferred Python style&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
If the index is not required, iterate directly over the elements.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; value : values) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; value &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for value in values:&lt;br /&gt;
print(value) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The second Python version should normally be preferred.&lt;br /&gt;
&lt;br /&gt;
== 15. When both index and value are required ==&lt;br /&gt;
&lt;br /&gt;
C/C++ programmers frequently use an explicit index.&lt;br /&gt;
&lt;br /&gt;
Python provides &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;std::string&amp;gt; names = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; names.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; names[i]&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
names = [&amp;quot;Ana&amp;quot;, &amp;quot;Bob&amp;quot;, &amp;quot;Carol&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
for i, name in enumerate(names):&lt;br /&gt;
print(i, &amp;quot;:&amp;quot;, name) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This introduces another frequently used Python feature: &amp;#039;&amp;#039;&amp;#039;unpacking&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== 16. Lists ==&lt;br /&gt;
&lt;br /&gt;
The closest commonly used Python equivalent to &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.push_back(40);&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[0];&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values.size(); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
values.append(40)&lt;br /&gt;
&lt;br /&gt;
print(values[0])&lt;br /&gt;
print(len(values)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python lists can contain objects of different types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// A normal std::vector cannot&lt;br /&gt;
// contain unrelated types.&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
10, 20, 30&lt;br /&gt;
}; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [&lt;br /&gt;
10,&lt;br /&gt;
3.14,&lt;br /&gt;
&amp;quot;hello&amp;quot;,&lt;br /&gt;
True&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Although Python allows this, homogeneous collections are generally easier to understand and maintain when the elements conceptually represent the same kind of data.&lt;br /&gt;
&lt;br /&gt;
== 17. Negative indexing ==&lt;br /&gt;
&lt;br /&gt;
Python allows indexing from the end of a sequence.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {10, 20, 30, 40};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 1]; // 40&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 2]; // 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40]&lt;br /&gt;
&lt;br /&gt;
print(v[-1])  # 40&lt;br /&gt;
print(v[-2])  # 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 18. Slicing ==&lt;br /&gt;
&lt;br /&gt;
Python sequences support slicing directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {&lt;br /&gt;
    10, 20, 30, 40, 50&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; part(&lt;br /&gt;
v.begin() + 1,&lt;br /&gt;
v.begin() + 4&lt;br /&gt;
); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40, 50]&lt;br /&gt;
&lt;br /&gt;
part = v[1:4] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The result is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[20, 30, 40]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
General syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
sequence[start:stop:step]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// Requires an explicit loop or&lt;br /&gt;
// another algorithm.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [0, 1, 2, 3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
print(v[1:4])   # [1, 2, 3]&lt;br /&gt;
print(v[:3])    # [0, 1, 2]&lt;br /&gt;
print(v[3:])    # [3, 4, 5]&lt;br /&gt;
print(v[::2])   # [0, 2, 4]&lt;br /&gt;
print(v[::-1])  # [5, 4, 3, 2, 1, 0] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 19. Membership testing ==&lt;br /&gt;
&lt;br /&gt;
Checking whether an element exists in a container is commonly performed using &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
if (std::find(&lt;br /&gt;
values.begin(),&lt;br /&gt;
values.end(),&lt;br /&gt;
20&lt;br /&gt;
) != values.end()) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The opposite test uses &amp;lt;code&amp;gt;not in&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 100 not in values:&lt;br /&gt;
    print(&amp;quot;Not found&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 20. Dictionaries ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt; is similar to a C++ associative container such as &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades;&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10;&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; grades[&amp;quot;Ana&amp;quot;]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {}&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8&lt;br /&gt;
&lt;br /&gt;
print(grades[&amp;quot;Ana&amp;quot;]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A dictionary can also be initialized directly:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades = {&lt;br /&gt;
    {&amp;quot;Ana&amp;quot;, 10},&lt;br /&gt;
    {&amp;quot;Bob&amp;quot;, 8},&lt;br /&gt;
    {&amp;quot;Carol&amp;quot;, 9}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 10,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 8,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 9,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Iterating over keys and values:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; grade&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
    print(name, &amp;quot;:&amp;quot;, grade)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 21. Sets ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; represents a collection containing unique values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.insert(40);&lt;br /&gt;
&lt;br /&gt;
if (20 != values.end()) {&lt;br /&gt;
// conceptually test membership&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = {10, 20, 30}&lt;br /&gt;
&lt;br /&gt;
values.add(40)&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A more correct C++ membership test would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (values.find(20) != values.end()) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets are particularly useful when:&lt;br /&gt;
&lt;br /&gt;
* duplicate values should be removed;&lt;br /&gt;
* fast membership tests are required;&lt;br /&gt;
* mathematical set operations are useful.&lt;br /&gt;
&lt;br /&gt;
== 22. Tuples ==&lt;br /&gt;
&lt;br /&gt;
Tuples are similar to lists, but they are immutable.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::tuple&amp;lt;int, double, std::string&amp;gt; data {&lt;br /&gt;
    10,&lt;br /&gt;
    3.14,&lt;br /&gt;
    &amp;quot;test&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
data = (10, 3.14, &amp;quot;test&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python uses tuples extensively for returning or unpacking multiple values.&lt;br /&gt;
&lt;br /&gt;
== 23. Functions ==&lt;br /&gt;
&lt;br /&gt;
Functions are declared using &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    return a + b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int result = add(2, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def add(a, b):&lt;br /&gt;
return a + b&lt;br /&gt;
&lt;br /&gt;
result = add(2, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not require parameter or return types to be declared.&lt;br /&gt;
&lt;br /&gt;
However, Python supports optional &amp;#039;&amp;#039;&amp;#039;type hints&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
double average(&lt;br /&gt;
    double a,&lt;br /&gt;
    double b&lt;br /&gt;
) {&lt;br /&gt;
    return (a + b) / 2.0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(&lt;br /&gt;
    a: float,&lt;br /&gt;
    b: float&lt;br /&gt;
) -&amp;gt; float:&lt;br /&gt;
    return (a + b) / 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Type hints do not normally enforce types at runtime.&lt;br /&gt;
&lt;br /&gt;
They provide information for:&lt;br /&gt;
&lt;br /&gt;
* the programmer;&lt;br /&gt;
* IDEs;&lt;br /&gt;
* static analysis tools;&lt;br /&gt;
* documentation.&lt;br /&gt;
&lt;br /&gt;
== 24. Default parameters ==&lt;br /&gt;
&lt;br /&gt;
Both C++ and Python support default parameter values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void greet(&lt;br /&gt;
    const std::string&amp;amp; name = &amp;quot;World&amp;quot;&lt;br /&gt;
) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Hello, &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
greet();&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def greet(name=&amp;quot;World&amp;quot;):&lt;br /&gt;
print(f&amp;quot;Hello, {name}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
greet()&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 25. Named arguments ==&lt;br /&gt;
&lt;br /&gt;
Python allows function arguments to be specified by name.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void connect(&lt;br /&gt;
    std::string host,&lt;br /&gt;
    int port,&lt;br /&gt;
    bool secure&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
connect(&amp;quot;server.local&amp;quot;, 443, true); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def connect(host, port, secure):&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
connect(&lt;br /&gt;
host=&amp;quot;server.local&amp;quot;,&lt;br /&gt;
port=443,&lt;br /&gt;
secure=True&lt;br /&gt;
) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This can make calls with several parameters easier to understand.&lt;br /&gt;
&lt;br /&gt;
== 26. Returning multiple values ==&lt;br /&gt;
&lt;br /&gt;
In C++, multiple return values typically require a structure, tuple, pair or output parameters.&lt;br /&gt;
&lt;br /&gt;
Python commonly uses tuples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::pair&amp;lt;int, int&amp;gt; minmax(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    if (a &amp;lt; b)&lt;br /&gt;
        return {a, b};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return {b, a};&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
auto [minimum, maximum] = minmax(10, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def minmax(a, b):&lt;br /&gt;
if a &amp;lt; b:&lt;br /&gt;
return a, b&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return b, a&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
minimum, maximum = minmax(10, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is another example of tuple unpacking.&lt;br /&gt;
&lt;br /&gt;
== 27. Swapping variables ==&lt;br /&gt;
&lt;br /&gt;
A temporary variable is normally used in C.&lt;br /&gt;
&lt;br /&gt;
Python supports unpacking directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int temp = a;&lt;br /&gt;
a = b;&lt;br /&gt;
b = temp;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a, b = b, a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 28. Strings ==&lt;br /&gt;
&lt;br /&gt;
Python strings behave more like C++ &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt; objects than traditional C character arrays.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text.size();&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text[0]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(len(text))&lt;br /&gt;
print(text[0]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
String concatenation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string first = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
std::string second = &amp;quot;World&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::string result =&lt;br /&gt;
first + &amp;quot; &amp;quot; + second; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
first = &amp;quot;Hello&amp;quot;&lt;br /&gt;
second = &amp;quot;World&amp;quot;&lt;br /&gt;
&lt;br /&gt;
result = first + &amp;quot; &amp;quot; + second &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python strings provide many built-in operations:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Usually requires algorithms,&lt;br /&gt;
// explicit transformations,&lt;br /&gt;
// or utility functions. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(text.upper())&lt;br /&gt;
print(text.lower())&lt;br /&gt;
print(text.startswith(&amp;quot;he&amp;quot;))&lt;br /&gt;
print(text.endswith(&amp;quot;lo&amp;quot;))&lt;br /&gt;
print(text.replace(&amp;quot;l&amp;quot;, &amp;quot;x&amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 29. Lists versus arrays: important semantic difference ==&lt;br /&gt;
&lt;br /&gt;
Consider assigning one collection to another.&lt;br /&gt;
&lt;br /&gt;
With normal C++ value semantics, copying a vector produces another vector.&lt;br /&gt;
&lt;br /&gt;
Python assignment behaves differently.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&lt;br /&gt;
b[0] = 100;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a[0];  // 1&lt;br /&gt;
std::cout &amp;lt;&amp;lt; b[0];  // 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
b = a&lt;br /&gt;
&lt;br /&gt;
b[0] = 100&lt;br /&gt;
&lt;br /&gt;
print(a[0])  # 100&lt;br /&gt;
print(b[0])  # 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This happens because:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
does not copy the list.&lt;br /&gt;
&lt;br /&gt;
Both names refer to the &amp;#039;&amp;#039;&amp;#039;same list object&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Conceptually:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
       +-------------+&lt;br /&gt;
a ---&amp;gt; | [1, 2, 3]   |&lt;br /&gt;
       +-------------+&lt;br /&gt;
          ^&lt;br /&gt;
          |&lt;br /&gt;
b --------+&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To explicitly create a shallow copy:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a.copy()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = list(a)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This distinction is extremely important when using Python.&lt;br /&gt;
&lt;br /&gt;
== 30. Equality versus identity ==&lt;br /&gt;
&lt;br /&gt;
Python distinguishes between:&lt;br /&gt;
&lt;br /&gt;
* whether two objects contain equal values;&lt;br /&gt;
* whether two names refer to the exact same object.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; (a == b);  // true&lt;br /&gt;
&lt;br /&gt;
// Address comparison would be needed&lt;br /&gt;
// to determine object identity. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
b = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
print(a == b)  # True&lt;br /&gt;
print(a is b)  # False &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In Python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
==  tests value equality&lt;br /&gt;
is  tests object identity&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not use &amp;lt;code&amp;gt;is&amp;lt;/code&amp;gt; when you intend to compare values.&lt;br /&gt;
&lt;br /&gt;
A common correct use is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if value is None:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 31. None and nullptr ==&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; represents the absence of a value.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Object* ptr = nullptr;&lt;br /&gt;
&lt;br /&gt;
if (ptr == nullptr) {&lt;br /&gt;
...&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = None&lt;br /&gt;
&lt;br /&gt;
if value is None:&lt;br /&gt;
... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 32. Exceptions ==&lt;br /&gt;
&lt;br /&gt;
Exception handling is conceptually similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    int value = std::stoi(text);&lt;br /&gt;
}&lt;br /&gt;
catch (const std::exception&amp;amp; e) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Invalid value\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    value = int(text)&lt;br /&gt;
except ValueError:&lt;br /&gt;
    print(&amp;quot;Invalid value&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python generally uses exceptions rather than special error return values for many failure conditions.&lt;br /&gt;
&lt;br /&gt;
== 33. A first Pythonic transformation ==&lt;br /&gt;
&lt;br /&gt;
Consider creating a vector/list containing the squares of the numbers from 0 to 9.&lt;br /&gt;
&lt;br /&gt;
A direct translation from C++ could look like this:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — direct translation |&lt;br /&gt;
| ----------------------------- |&lt;br /&gt;
|                               |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = []&lt;br /&gt;
&lt;br /&gt;
for i in range(10):&lt;br /&gt;
squares.append(i * i) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python code is correct.&lt;br /&gt;
&lt;br /&gt;
However, Python provides a concise construct called a &amp;#039;&amp;#039;&amp;#039;list comprehension&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — idiomatic |&lt;br /&gt;
| -------------------- |&lt;br /&gt;
|                      |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A condition can also be included.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
if (i % 2 == 0) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
}&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
if i % 2 == 0&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
An important objective of this course is not only to write programs that work in Python, but to learn to write programs in a style appropriate for Python.&lt;br /&gt;
&lt;br /&gt;
== 34. Example: processing student grades ==&lt;br /&gt;
&lt;br /&gt;
Consider the following problem:&lt;br /&gt;
&lt;br /&gt;
* store the grades of several students;&lt;br /&gt;
* compute the average;&lt;br /&gt;
* print the students whose grade is greater than or equal to the average.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;unordered_map&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt; grades = {&lt;br /&gt;
{&amp;quot;Ana&amp;quot;, 9.5},&lt;br /&gt;
{&amp;quot;Bob&amp;quot;, 7.0},&lt;br /&gt;
{&amp;quot;Carol&amp;quot;, 8.5},&lt;br /&gt;
{&amp;quot;Dan&amp;quot;, 6.0}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
double sum = 0.0;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    sum += grade;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
double average = sum / grades.size();&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Average: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; average&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    if (grade &amp;gt;= average) {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
                  &amp;lt;&amp;lt; grade&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
&amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
&amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
&amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
&amp;quot;Dan&amp;quot;: 6.0,&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
average = sum(grades.values()) / len(grades)&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;Average: {average:.2f}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
if grade &amp;gt;= average:&lt;br /&gt;
print(f&amp;quot;{name}: {grade}&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that the Python version uses existing language functionality rather than manually reproducing every operation.&lt;br /&gt;
&lt;br /&gt;
In particular:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sum(grades.values())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
expresses the intention directly.&lt;br /&gt;
&lt;br /&gt;
== 35. Exercise 1 — Translation ==&lt;br /&gt;
&lt;br /&gt;
Translate the following C++ program into Python.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
12, 7, 18, 3, 24, 11&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
int sum = 0;&lt;br /&gt;
int count = 0;&lt;br /&gt;
&lt;br /&gt;
for (int value : values) {&lt;br /&gt;
    if (value % 2 == 0) {&lt;br /&gt;
        sum += value;&lt;br /&gt;
        count++;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Count: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; count&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Sum: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; sum&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Your implementation:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# TODO&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
After implementing a direct translation, try to simplify the Python implementation.&lt;br /&gt;
&lt;br /&gt;
== 36. Exercise 2 — Statistics ==&lt;br /&gt;
&lt;br /&gt;
Write a Python program that:&lt;br /&gt;
&lt;br /&gt;
# reads a sequence of integer values;&lt;br /&gt;
&lt;br /&gt;
# stores them in a list;&lt;br /&gt;
&lt;br /&gt;
# prints the minimum value;&lt;br /&gt;
&lt;br /&gt;
# prints the maximum value;&lt;br /&gt;
&lt;br /&gt;
# prints the average;&lt;br /&gt;
&lt;br /&gt;
# prints how many values are above the average.&lt;br /&gt;
&lt;br /&gt;
The equivalent C++ structure might begin as follows:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values;&lt;br /&gt;
&lt;br /&gt;
// Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
int minimum = ...;&lt;br /&gt;
int maximum = ...;&lt;br /&gt;
double average = ...; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = []&lt;br /&gt;
&lt;br /&gt;
# Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
minimum = ...&lt;br /&gt;
maximum = ...&lt;br /&gt;
average = ... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Useful Python functions include:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
len(...)&lt;br /&gt;
sum(...)&lt;br /&gt;
min(...)&lt;br /&gt;
max(...)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 37. Exercise 3 — Word frequency ==&lt;br /&gt;
&lt;br /&gt;
Given a sentence, count how many times each word occurs.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python is simple and python is powerful&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
should produce information equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python -&amp;gt; 2&lt;br /&gt;
is -&amp;gt; 2&lt;br /&gt;
simple -&amp;gt; 1&lt;br /&gt;
and -&amp;gt; 1&lt;br /&gt;
powerful -&amp;gt; 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A possible C++ data structure and its Python equivalent are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt;&lt;br /&gt;
    frequencies;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
frequencies = {}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sentence.split()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to separate the sentence into words.&lt;br /&gt;
&lt;br /&gt;
== 38. Exercise 4 — Remove duplicates ==&lt;br /&gt;
&lt;br /&gt;
Given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [4, 2, 7, 4, 2, 9, 7, 1]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
create a collection containing every distinct value.&lt;br /&gt;
&lt;br /&gt;
Think about the following C++ and Python data structures:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; unique_values;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
unique_values = set()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Try to solve the problem both:&lt;br /&gt;
&lt;br /&gt;
# using an explicit loop;&lt;br /&gt;
&lt;br /&gt;
# using Python&amp;#039;s built-in functionality.&lt;br /&gt;
&lt;br /&gt;
== 39. Exercise 5 — Small problem ==&lt;br /&gt;
&lt;br /&gt;
Write a program that receives a list of students and their grades and determines:&lt;br /&gt;
&lt;br /&gt;
* the class average;&lt;br /&gt;
* the student with the highest grade;&lt;br /&gt;
* all students who passed;&lt;br /&gt;
* all students whose grade is above the class average.&lt;br /&gt;
&lt;br /&gt;
Suggested representation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt;&lt;br /&gt;
    grades;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
    &amp;quot;Dan&amp;quot;: 4.0,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Avoid using numeric indices unless an index is actually required.&lt;br /&gt;
&lt;br /&gt;
== 40. Creating a project environment ==&lt;br /&gt;
&lt;br /&gt;
Python applications commonly use a &amp;#039;&amp;#039;&amp;#039;virtual environment&amp;#039;&amp;#039;&amp;#039; to isolate project dependencies.&lt;br /&gt;
&lt;br /&gt;
Create a directory for the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir python-project&lt;br /&gt;
cd python-project&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a virtual environment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 -m venv .venv&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Linux/macOS:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source .venv/bin/activate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Windows PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
.venv\Scripts\Activate.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command prompt will normally indicate that the environment is active.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
(.venv) student@computer:~/python-project$&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Packages installed using &amp;lt;code&amp;gt;pip&amp;lt;/code&amp;gt; will now be installed inside this environment.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip install requests&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To inspect installed packages:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip list&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 41. Recommended initial project structure ==&lt;br /&gt;
&lt;br /&gt;
For the first laboratories, the following structure is sufficient:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python-project/&lt;br /&gt;
├── .venv/&lt;br /&gt;
├── main.py&lt;br /&gt;
├── README.md&lt;br /&gt;
└── .gitignore&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A basic &amp;lt;code&amp;gt;.gitignore&amp;lt;/code&amp;gt; should contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.venv/&lt;br /&gt;
__pycache__/&lt;br /&gt;
*.pyc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; commit the virtual environment to Git.&lt;br /&gt;
&lt;br /&gt;
== 42. C/C++ habits to reconsider in Python ==&lt;br /&gt;
&lt;br /&gt;
When transitioning from C/C++ to Python, the following patterns should generally be reconsidered.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ habit&lt;br /&gt;
&lt;br /&gt;
| ! Typical Python approach                |&lt;br /&gt;
| ---------------------------------------- |&lt;br /&gt;
| Iterate using an integer index           |&lt;br /&gt;
| Iterate directly over the objects        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually maintain counters               |&lt;br /&gt;
| Consider &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Search manually through a container      |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually calculate container size        |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;len()&amp;lt;/code&amp;gt;                   |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit temporary variable for swapping |&lt;br /&gt;
| Use tuple unpacking                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit loop for simple transformations |&lt;br /&gt;
| Consider a comprehension                 |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Long output expressions                  |&lt;br /&gt;
| Use f-strings                            |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Copy assumed after assignment            |&lt;br /&gt;
| Remember that names refer to objects     |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Check null using equality                |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;is None&amp;lt;/code&amp;gt;                 |&lt;br /&gt;
| }                                        |&lt;br /&gt;
&lt;br /&gt;
The objective is not simply to translate C/C++ syntax into Python syntax.&lt;br /&gt;
&lt;br /&gt;
Consider:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++ style translated literally&lt;br /&gt;
&lt;br /&gt;
| ! More idiomatic Python |&lt;br /&gt;
| ----------------------- |&lt;br /&gt;
|                         |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
    print(values[i])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for value in values:&lt;br /&gt;
    print(value)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Both programs are correct.&lt;br /&gt;
&lt;br /&gt;
The second expresses the programmer&amp;#039;s intention more directly.&lt;br /&gt;
&lt;br /&gt;
== 43. Summary ==&lt;br /&gt;
&lt;br /&gt;
The main mappings introduced in this laboratory are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python                                    |   |         |&lt;br /&gt;
| ------------------------------------------- | - | ------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;{ ... }&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| indentation                                 |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;                                      |   | &amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;                              |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt;                    |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_set&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::tuple&amp;lt;/code&amp;gt;                     |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;tuple&amp;lt;/code&amp;gt;                          |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt;                      |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cin&amp;lt;/code&amp;gt;                       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| indexed iteration                           |   |         |&lt;br /&gt;
| direct iteration / &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| manual search                               |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::pow(a, b)&amp;lt;/code&amp;gt;                 |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;a ** b&amp;lt;/code&amp;gt;                         |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| integer division                            |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| }                                           |   |         |&lt;br /&gt;
&lt;br /&gt;
The central idea to retain from this laboratory is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Python should not be treated merely as C++ with different syntax. Python provides its own abstractions and idioms, and using them generally produces simpler and more readable programs.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 44. Preparation for Lab 2 ==&lt;br /&gt;
&lt;br /&gt;
Before the next laboratory:&lt;br /&gt;
&lt;br /&gt;
# complete all exercises from this laboratory;&lt;br /&gt;
&lt;br /&gt;
# ensure Python 3 is installed;&lt;br /&gt;
&lt;br /&gt;
# create a virtual environment successfully;&lt;br /&gt;
&lt;br /&gt;
# create a Git repository for the semester project;&lt;br /&gt;
&lt;br /&gt;
# become familiar with lists, dictionaries, tuples and sets;&lt;br /&gt;
&lt;br /&gt;
# review the concepts of classes, objects, inheritance and composition from C++.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Lab 2&amp;#039;&amp;#039;&amp;#039;, these concepts will be revisited from the perspective of Python&amp;#039;s object model and Pythonic object-oriented programming.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8364</id>
		<title>Lab 1</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8364"/>
		<updated>2026-09-14T05:33:45Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* 7. Reading input */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 1 — Python for C/C++ Programmers =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
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++.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
After completing this laboratory, you should be able to:&lt;br /&gt;
&lt;br /&gt;
* understand the basic syntax of Python;&lt;br /&gt;
* recognize the main differences between Python and C/C++;&lt;br /&gt;
* work with Python&amp;#039;s fundamental data types;&lt;br /&gt;
* use conditional statements and loops;&lt;br /&gt;
* work with lists, dictionaries, sets and tuples;&lt;br /&gt;
* define and call functions;&lt;br /&gt;
* understand basic Python object/reference semantics;&lt;br /&gt;
* recognize several common Python idioms;&lt;br /&gt;
* create and execute a simple Python project.&lt;br /&gt;
&lt;br /&gt;
Throughout this laboratory, equivalent or similar C/C++ and Python code will be presented side by side.&lt;br /&gt;
&lt;br /&gt;
**TOC**&lt;br /&gt;
&lt;br /&gt;
== 1. Running a Python program ==&lt;br /&gt;
&lt;br /&gt;
A C/C++ program normally has to be compiled before it is executed. Python programs are usually executed directly by the Python interpreter.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Hello, world!\n&amp;quot;;&lt;br /&gt;
return 0;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(&amp;quot;Hello, world!&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A Python source file normally uses the extension:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be executed using:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unlike C/C++, Python does not require a special &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
However, larger Python programs commonly use:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Program started\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
print(&amp;quot;Program started&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
if **name** == &amp;quot;**main**&amp;quot;:&lt;br /&gt;
main() &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python version is not mandatory, but this pattern will become useful when programs are split into multiple modules.&lt;br /&gt;
&lt;br /&gt;
== 2. Blocks and indentation ==&lt;br /&gt;
&lt;br /&gt;
In C and C++, braces delimit blocks of code.&lt;br /&gt;
&lt;br /&gt;
Python uses &amp;#039;&amp;#039;&amp;#039;indentation&amp;#039;&amp;#039;&amp;#039; instead.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (x &amp;gt; 10) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Large\n&amp;quot;;&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Value: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
    print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
    print(&amp;quot;Value:&amp;quot;, x)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Indentation in Python is part of the language syntax.&lt;br /&gt;
&lt;br /&gt;
The standard convention is &amp;#039;&amp;#039;&amp;#039;4 spaces per indentation level&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
For example, the following code is incorrect:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python will report an &amp;lt;code&amp;gt;IndentationError&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 3. Variables and types ==&lt;br /&gt;
&lt;br /&gt;
C/C++ variables have a declared static type.&lt;br /&gt;
&lt;br /&gt;
Python variables do not require explicit type declarations.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int count = 10;&lt;br /&gt;
double temperature = 23.5;&lt;br /&gt;
bool enabled = true;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
count = 10&lt;br /&gt;
temperature = 23.5&lt;br /&gt;
enabled = True&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python is &amp;#039;&amp;#039;&amp;#039;dynamically typed&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
This means that a variable name can later refer to an object of another type.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
// Not allowed:&lt;br /&gt;
// value = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
value = &amp;quot;hello&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This does &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; mean that Python has no types.&lt;br /&gt;
&lt;br /&gt;
Objects still have types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;typeinfo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; typeid(value).name();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
&lt;br /&gt;
print(type(value))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python will produce:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common basic types ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ type&lt;br /&gt;
! Python equivalent&lt;br /&gt;
! Example&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;42&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt; / &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;3.14&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt;&lt;br /&gt;
| No separate character type&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;A&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;str&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;Hello&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 4. Constants ==&lt;br /&gt;
&lt;br /&gt;
C++ can enforce that a value cannot be modified using &amp;lt;code&amp;gt;const&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python does not enforce constants at language level.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
const double PI = 3.1415926535;&lt;br /&gt;
const int MAX_USERS = 100; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
PI = 3.1415926535&lt;br /&gt;
MAX_USERS = 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
By convention, Python names that should be treated as constants are written using uppercase letters.&lt;br /&gt;
&lt;br /&gt;
Nothing prevents the programmer from changing them.&lt;br /&gt;
&lt;br /&gt;
== 5. Arithmetic operators ==&lt;br /&gt;
&lt;br /&gt;
Most arithmetic operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
int sum  = a + b;&lt;br /&gt;
int diff = a - b;&lt;br /&gt;
int prod = a * b;&lt;br /&gt;
int div  = a / b;&lt;br /&gt;
int rem  = a % b; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
sum_ = a + b&lt;br /&gt;
diff = a - b&lt;br /&gt;
prod = a * b&lt;br /&gt;
div = a // b&lt;br /&gt;
rem = a % b &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
There is an important difference between &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a / b;   // 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
print(a / b)   # 3.3333333333333335&lt;br /&gt;
print(a // b)  # 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In C/C++, dividing two integers performs integer division.&lt;br /&gt;
&lt;br /&gt;
In Python, the &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; operator always performs floating-point division, while &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt; performs floor division.&lt;br /&gt;
&lt;br /&gt;
Python also provides an exponentiation operator:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;cmath&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double result = std::pow(2, 8); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
result = 2 ** 8 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 6. Printing values ==&lt;br /&gt;
&lt;br /&gt;
Output using &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt; is replaced by the built-in &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
int age = 21;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; is &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; age&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; years old\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
age = 21&lt;br /&gt;
&lt;br /&gt;
print(name, &amp;quot;is&amp;quot;, age, &amp;quot;years old&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python also provides formatted string literals, commonly called &amp;#039;&amp;#039;&amp;#039;f-strings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Temperature: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; temperature&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; C\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Temperature: {temperature} C&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Expressions can appear directly inside an f-string:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Result: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; a + b&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Result: {a + b}&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 7. Reading input ==&lt;br /&gt;
&lt;br /&gt;
Console input is performed using &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An important difference is that &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt; &amp;#039;&amp;#039;&amp;#039;always returns a string&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int age;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Age: &amp;quot;;&lt;br /&gt;
std::cin &amp;gt;&amp;gt; age; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = int(input(&amp;quot;Age: &amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Without the conversion:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = input(&amp;quot;Age: &amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; is a string.&lt;br /&gt;
&lt;br /&gt;
Conversions are explicit:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = std::stoi(text);&lt;br /&gt;
double b = std::stod(text); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = int(text)&lt;br /&gt;
b = float(text) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 8. Conditional statements ==&lt;br /&gt;
&lt;br /&gt;
The overall structure is similar, but Python does not use parentheses or braces.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (temperature &amp;gt; 30) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Hot\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else if (temperature &amp;gt; 20) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Warm\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Cold\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if temperature &amp;gt; 30:&lt;br /&gt;
    print(&amp;quot;Hot&amp;quot;)&lt;br /&gt;
elif temperature &amp;gt; 20:&lt;br /&gt;
    print(&amp;quot;Warm&amp;quot;)&lt;br /&gt;
else:&lt;br /&gt;
    print(&amp;quot;Cold&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python uses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
elif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
instead of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
else if&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 9. Boolean operators ==&lt;br /&gt;
&lt;br /&gt;
Python uses words instead of the C/C++ symbolic logical operators.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python         |   |         |&lt;br /&gt;
| ---------------- | - | ------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt;  |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| -                |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;           |   | &amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;  |   |         |&lt;br /&gt;
| -                |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;   |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| }                |   |         |&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (age &amp;gt;= 18 &amp;amp;&amp;amp; enabled) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if (!enabled) {&lt;br /&gt;
...&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if age &amp;gt;= 18 and enabled:&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
if not enabled:&lt;br /&gt;
... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 10. Comparison operators ==&lt;br /&gt;
&lt;br /&gt;
Most comparison operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Operation&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python              |&lt;br /&gt;
| --------------------- |&lt;br /&gt;
| equality              |&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| inequality            |&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| less than             |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                     |&lt;br /&gt;
| greater than          |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                     |&lt;br /&gt;
| less than or equal    |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| greater than or equal |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| }                     |&lt;br /&gt;
&lt;br /&gt;
Python also allows chained comparisons.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (0 &amp;lt;= x &amp;amp;&amp;amp; x &amp;lt; 100) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 0 &amp;lt;= x &amp;lt; 100:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 11. The ternary operator ==&lt;br /&gt;
&lt;br /&gt;
Both languages support conditional expressions.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int maximum = (a &amp;gt; b) ? a : b;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
maximum = a if a &amp;gt; b else b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python&amp;#039;s order is different:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
value_if_true if condition else value_if_false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 12. While loops ==&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;code&amp;gt;while&amp;lt;/code&amp;gt; loop is very similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int i = 0;&lt;br /&gt;
&lt;br /&gt;
while (i &amp;lt; 5) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
i++;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
&lt;br /&gt;
while i &amp;lt; 5:&lt;br /&gt;
print(i)&lt;br /&gt;
i += 1 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not have &amp;lt;code&amp;gt;++&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;--&amp;lt;/code&amp;gt; operators.&lt;br /&gt;
&lt;br /&gt;
Therefore:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i += 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is used instead.&lt;br /&gt;
&lt;br /&gt;
== 13. For loops ==&lt;br /&gt;
&lt;br /&gt;
This is one of the first important conceptual differences.&lt;br /&gt;
&lt;br /&gt;
In C/C++, a &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement commonly controls a numeric index.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement iterates over objects.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 5; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;range(5)&amp;lt;/code&amp;gt; generates values conceptually equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
0, 1, 2, 3, 4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A different starting value can be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 2; i &amp;lt; 10; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(2, 10):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A step can also be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i += 2) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(0, 10, 2):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 14. Iterating over containers ==&lt;br /&gt;
&lt;br /&gt;
Suppose we want to print every element of a vector/list.&lt;br /&gt;
&lt;br /&gt;
An index-based implementation is possible:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; values.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[i] &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
print(values[i]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
However, this is generally &amp;#039;&amp;#039;&amp;#039;not the preferred Python style&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
If the index is not required, iterate directly over the elements.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; value : values) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; value &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for value in values:&lt;br /&gt;
print(value) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The second Python version should normally be preferred.&lt;br /&gt;
&lt;br /&gt;
== 15. When both index and value are required ==&lt;br /&gt;
&lt;br /&gt;
C/C++ programmers frequently use an explicit index.&lt;br /&gt;
&lt;br /&gt;
Python provides &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;std::string&amp;gt; names = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; names.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; names[i]&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
names = [&amp;quot;Ana&amp;quot;, &amp;quot;Bob&amp;quot;, &amp;quot;Carol&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
for i, name in enumerate(names):&lt;br /&gt;
print(i, &amp;quot;:&amp;quot;, name) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This introduces another frequently used Python feature: &amp;#039;&amp;#039;&amp;#039;unpacking&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== 16. Lists ==&lt;br /&gt;
&lt;br /&gt;
The closest commonly used Python equivalent to &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.push_back(40);&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[0];&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values.size(); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
values.append(40)&lt;br /&gt;
&lt;br /&gt;
print(values[0])&lt;br /&gt;
print(len(values)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python lists can contain objects of different types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// A normal std::vector cannot&lt;br /&gt;
// contain unrelated types.&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
10, 20, 30&lt;br /&gt;
}; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [&lt;br /&gt;
10,&lt;br /&gt;
3.14,&lt;br /&gt;
&amp;quot;hello&amp;quot;,&lt;br /&gt;
True&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Although Python allows this, homogeneous collections are generally easier to understand and maintain when the elements conceptually represent the same kind of data.&lt;br /&gt;
&lt;br /&gt;
== 17. Negative indexing ==&lt;br /&gt;
&lt;br /&gt;
Python allows indexing from the end of a sequence.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {10, 20, 30, 40};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 1]; // 40&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 2]; // 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40]&lt;br /&gt;
&lt;br /&gt;
print(v[-1])  # 40&lt;br /&gt;
print(v[-2])  # 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 18. Slicing ==&lt;br /&gt;
&lt;br /&gt;
Python sequences support slicing directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {&lt;br /&gt;
    10, 20, 30, 40, 50&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; part(&lt;br /&gt;
v.begin() + 1,&lt;br /&gt;
v.begin() + 4&lt;br /&gt;
); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40, 50]&lt;br /&gt;
&lt;br /&gt;
part = v[1:4] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The result is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[20, 30, 40]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
General syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
sequence[start:stop:step]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// Requires an explicit loop or&lt;br /&gt;
// another algorithm.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [0, 1, 2, 3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
print(v[1:4])   # [1, 2, 3]&lt;br /&gt;
print(v[:3])    # [0, 1, 2]&lt;br /&gt;
print(v[3:])    # [3, 4, 5]&lt;br /&gt;
print(v[::2])   # [0, 2, 4]&lt;br /&gt;
print(v[::-1])  # [5, 4, 3, 2, 1, 0] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 19. Membership testing ==&lt;br /&gt;
&lt;br /&gt;
Checking whether an element exists in a container is commonly performed using &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
if (std::find(&lt;br /&gt;
values.begin(),&lt;br /&gt;
values.end(),&lt;br /&gt;
20&lt;br /&gt;
) != values.end()) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The opposite test uses &amp;lt;code&amp;gt;not in&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 100 not in values:&lt;br /&gt;
    print(&amp;quot;Not found&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 20. Dictionaries ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt; is similar to a C++ associative container such as &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades;&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10;&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; grades[&amp;quot;Ana&amp;quot;]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {}&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8&lt;br /&gt;
&lt;br /&gt;
print(grades[&amp;quot;Ana&amp;quot;]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A dictionary can also be initialized directly:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades = {&lt;br /&gt;
    {&amp;quot;Ana&amp;quot;, 10},&lt;br /&gt;
    {&amp;quot;Bob&amp;quot;, 8},&lt;br /&gt;
    {&amp;quot;Carol&amp;quot;, 9}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 10,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 8,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 9,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Iterating over keys and values:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; grade&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
    print(name, &amp;quot;:&amp;quot;, grade)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 21. Sets ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; represents a collection containing unique values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.insert(40);&lt;br /&gt;
&lt;br /&gt;
if (20 != values.end()) {&lt;br /&gt;
// conceptually test membership&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = {10, 20, 30}&lt;br /&gt;
&lt;br /&gt;
values.add(40)&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A more correct C++ membership test would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (values.find(20) != values.end()) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets are particularly useful when:&lt;br /&gt;
&lt;br /&gt;
* duplicate values should be removed;&lt;br /&gt;
* fast membership tests are required;&lt;br /&gt;
* mathematical set operations are useful.&lt;br /&gt;
&lt;br /&gt;
== 22. Tuples ==&lt;br /&gt;
&lt;br /&gt;
Tuples are similar to lists, but they are immutable.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::tuple&amp;lt;int, double, std::string&amp;gt; data {&lt;br /&gt;
    10,&lt;br /&gt;
    3.14,&lt;br /&gt;
    &amp;quot;test&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
data = (10, 3.14, &amp;quot;test&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python uses tuples extensively for returning or unpacking multiple values.&lt;br /&gt;
&lt;br /&gt;
== 23. Functions ==&lt;br /&gt;
&lt;br /&gt;
Functions are declared using &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    return a + b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int result = add(2, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def add(a, b):&lt;br /&gt;
return a + b&lt;br /&gt;
&lt;br /&gt;
result = add(2, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not require parameter or return types to be declared.&lt;br /&gt;
&lt;br /&gt;
However, Python supports optional &amp;#039;&amp;#039;&amp;#039;type hints&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
double average(&lt;br /&gt;
    double a,&lt;br /&gt;
    double b&lt;br /&gt;
) {&lt;br /&gt;
    return (a + b) / 2.0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(&lt;br /&gt;
    a: float,&lt;br /&gt;
    b: float&lt;br /&gt;
) -&amp;gt; float:&lt;br /&gt;
    return (a + b) / 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Type hints do not normally enforce types at runtime.&lt;br /&gt;
&lt;br /&gt;
They provide information for:&lt;br /&gt;
&lt;br /&gt;
* the programmer;&lt;br /&gt;
* IDEs;&lt;br /&gt;
* static analysis tools;&lt;br /&gt;
* documentation.&lt;br /&gt;
&lt;br /&gt;
== 24. Default parameters ==&lt;br /&gt;
&lt;br /&gt;
Both C++ and Python support default parameter values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void greet(&lt;br /&gt;
    const std::string&amp;amp; name = &amp;quot;World&amp;quot;&lt;br /&gt;
) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Hello, &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
greet();&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def greet(name=&amp;quot;World&amp;quot;):&lt;br /&gt;
print(f&amp;quot;Hello, {name}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
greet()&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 25. Named arguments ==&lt;br /&gt;
&lt;br /&gt;
Python allows function arguments to be specified by name.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void connect(&lt;br /&gt;
    std::string host,&lt;br /&gt;
    int port,&lt;br /&gt;
    bool secure&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
connect(&amp;quot;server.local&amp;quot;, 443, true); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def connect(host, port, secure):&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
connect(&lt;br /&gt;
host=&amp;quot;server.local&amp;quot;,&lt;br /&gt;
port=443,&lt;br /&gt;
secure=True&lt;br /&gt;
) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This can make calls with several parameters easier to understand.&lt;br /&gt;
&lt;br /&gt;
== 26. Returning multiple values ==&lt;br /&gt;
&lt;br /&gt;
In C++, multiple return values typically require a structure, tuple, pair or output parameters.&lt;br /&gt;
&lt;br /&gt;
Python commonly uses tuples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::pair&amp;lt;int, int&amp;gt; minmax(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    if (a &amp;lt; b)&lt;br /&gt;
        return {a, b};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return {b, a};&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
auto [minimum, maximum] = minmax(10, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def minmax(a, b):&lt;br /&gt;
if a &amp;lt; b:&lt;br /&gt;
return a, b&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return b, a&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
minimum, maximum = minmax(10, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is another example of tuple unpacking.&lt;br /&gt;
&lt;br /&gt;
== 27. Swapping variables ==&lt;br /&gt;
&lt;br /&gt;
A temporary variable is normally used in C.&lt;br /&gt;
&lt;br /&gt;
Python supports unpacking directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int temp = a;&lt;br /&gt;
a = b;&lt;br /&gt;
b = temp;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a, b = b, a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 28. Strings ==&lt;br /&gt;
&lt;br /&gt;
Python strings behave more like C++ &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt; objects than traditional C character arrays.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text.size();&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text[0]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(len(text))&lt;br /&gt;
print(text[0]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
String concatenation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string first = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
std::string second = &amp;quot;World&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::string result =&lt;br /&gt;
first + &amp;quot; &amp;quot; + second; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
first = &amp;quot;Hello&amp;quot;&lt;br /&gt;
second = &amp;quot;World&amp;quot;&lt;br /&gt;
&lt;br /&gt;
result = first + &amp;quot; &amp;quot; + second &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python strings provide many built-in operations:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Usually requires algorithms,&lt;br /&gt;
// explicit transformations,&lt;br /&gt;
// or utility functions. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(text.upper())&lt;br /&gt;
print(text.lower())&lt;br /&gt;
print(text.startswith(&amp;quot;he&amp;quot;))&lt;br /&gt;
print(text.endswith(&amp;quot;lo&amp;quot;))&lt;br /&gt;
print(text.replace(&amp;quot;l&amp;quot;, &amp;quot;x&amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 29. Lists versus arrays: important semantic difference ==&lt;br /&gt;
&lt;br /&gt;
Consider assigning one collection to another.&lt;br /&gt;
&lt;br /&gt;
With normal C++ value semantics, copying a vector produces another vector.&lt;br /&gt;
&lt;br /&gt;
Python assignment behaves differently.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&lt;br /&gt;
b[0] = 100;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a[0];  // 1&lt;br /&gt;
std::cout &amp;lt;&amp;lt; b[0];  // 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
b = a&lt;br /&gt;
&lt;br /&gt;
b[0] = 100&lt;br /&gt;
&lt;br /&gt;
print(a[0])  # 100&lt;br /&gt;
print(b[0])  # 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This happens because:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
does not copy the list.&lt;br /&gt;
&lt;br /&gt;
Both names refer to the &amp;#039;&amp;#039;&amp;#039;same list object&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Conceptually:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
       +-------------+&lt;br /&gt;
a ---&amp;gt; | [1, 2, 3]   |&lt;br /&gt;
       +-------------+&lt;br /&gt;
          ^&lt;br /&gt;
          |&lt;br /&gt;
b --------+&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To explicitly create a shallow copy:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a.copy()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = list(a)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This distinction is extremely important when using Python.&lt;br /&gt;
&lt;br /&gt;
== 30. Equality versus identity ==&lt;br /&gt;
&lt;br /&gt;
Python distinguishes between:&lt;br /&gt;
&lt;br /&gt;
* whether two objects contain equal values;&lt;br /&gt;
* whether two names refer to the exact same object.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; (a == b);  // true&lt;br /&gt;
&lt;br /&gt;
// Address comparison would be needed&lt;br /&gt;
// to determine object identity. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
b = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
print(a == b)  # True&lt;br /&gt;
print(a is b)  # False &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In Python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
==  tests value equality&lt;br /&gt;
is  tests object identity&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not use &amp;lt;code&amp;gt;is&amp;lt;/code&amp;gt; when you intend to compare values.&lt;br /&gt;
&lt;br /&gt;
A common correct use is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if value is None:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 31. None and nullptr ==&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; represents the absence of a value.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Object* ptr = nullptr;&lt;br /&gt;
&lt;br /&gt;
if (ptr == nullptr) {&lt;br /&gt;
...&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = None&lt;br /&gt;
&lt;br /&gt;
if value is None:&lt;br /&gt;
... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 32. Exceptions ==&lt;br /&gt;
&lt;br /&gt;
Exception handling is conceptually similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    int value = std::stoi(text);&lt;br /&gt;
}&lt;br /&gt;
catch (const std::exception&amp;amp; e) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Invalid value\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    value = int(text)&lt;br /&gt;
except ValueError:&lt;br /&gt;
    print(&amp;quot;Invalid value&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python generally uses exceptions rather than special error return values for many failure conditions.&lt;br /&gt;
&lt;br /&gt;
== 33. A first Pythonic transformation ==&lt;br /&gt;
&lt;br /&gt;
Consider creating a vector/list containing the squares of the numbers from 0 to 9.&lt;br /&gt;
&lt;br /&gt;
A direct translation from C++ could look like this:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — direct translation |&lt;br /&gt;
| ----------------------------- |&lt;br /&gt;
|                               |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = []&lt;br /&gt;
&lt;br /&gt;
for i in range(10):&lt;br /&gt;
squares.append(i * i) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python code is correct.&lt;br /&gt;
&lt;br /&gt;
However, Python provides a concise construct called a &amp;#039;&amp;#039;&amp;#039;list comprehension&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — idiomatic |&lt;br /&gt;
| -------------------- |&lt;br /&gt;
|                      |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A condition can also be included.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
if (i % 2 == 0) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
}&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
if i % 2 == 0&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
An important objective of this course is not only to write programs that work in Python, but to learn to write programs in a style appropriate for Python.&lt;br /&gt;
&lt;br /&gt;
== 34. Example: processing student grades ==&lt;br /&gt;
&lt;br /&gt;
Consider the following problem:&lt;br /&gt;
&lt;br /&gt;
* store the grades of several students;&lt;br /&gt;
* compute the average;&lt;br /&gt;
* print the students whose grade is greater than or equal to the average.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;unordered_map&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt; grades = {&lt;br /&gt;
{&amp;quot;Ana&amp;quot;, 9.5},&lt;br /&gt;
{&amp;quot;Bob&amp;quot;, 7.0},&lt;br /&gt;
{&amp;quot;Carol&amp;quot;, 8.5},&lt;br /&gt;
{&amp;quot;Dan&amp;quot;, 6.0}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
double sum = 0.0;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    sum += grade;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
double average = sum / grades.size();&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Average: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; average&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    if (grade &amp;gt;= average) {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
                  &amp;lt;&amp;lt; grade&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
&amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
&amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
&amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
&amp;quot;Dan&amp;quot;: 6.0,&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
average = sum(grades.values()) / len(grades)&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;Average: {average:.2f}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
if grade &amp;gt;= average:&lt;br /&gt;
print(f&amp;quot;{name}: {grade}&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that the Python version uses existing language functionality rather than manually reproducing every operation.&lt;br /&gt;
&lt;br /&gt;
In particular:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sum(grades.values())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
expresses the intention directly.&lt;br /&gt;
&lt;br /&gt;
== 35. Exercise 1 — Translation ==&lt;br /&gt;
&lt;br /&gt;
Translate the following C++ program into Python.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
12, 7, 18, 3, 24, 11&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
int sum = 0;&lt;br /&gt;
int count = 0;&lt;br /&gt;
&lt;br /&gt;
for (int value : values) {&lt;br /&gt;
    if (value % 2 == 0) {&lt;br /&gt;
        sum += value;&lt;br /&gt;
        count++;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Count: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; count&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Sum: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; sum&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Your implementation:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# TODO&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
After implementing a direct translation, try to simplify the Python implementation.&lt;br /&gt;
&lt;br /&gt;
== 36. Exercise 2 — Statistics ==&lt;br /&gt;
&lt;br /&gt;
Write a Python program that:&lt;br /&gt;
&lt;br /&gt;
# reads a sequence of integer values;&lt;br /&gt;
&lt;br /&gt;
# stores them in a list;&lt;br /&gt;
&lt;br /&gt;
# prints the minimum value;&lt;br /&gt;
&lt;br /&gt;
# prints the maximum value;&lt;br /&gt;
&lt;br /&gt;
# prints the average;&lt;br /&gt;
&lt;br /&gt;
# prints how many values are above the average.&lt;br /&gt;
&lt;br /&gt;
The equivalent C++ structure might begin as follows:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values;&lt;br /&gt;
&lt;br /&gt;
// Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
int minimum = ...;&lt;br /&gt;
int maximum = ...;&lt;br /&gt;
double average = ...; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = []&lt;br /&gt;
&lt;br /&gt;
# Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
minimum = ...&lt;br /&gt;
maximum = ...&lt;br /&gt;
average = ... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Useful Python functions include:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
len(...)&lt;br /&gt;
sum(...)&lt;br /&gt;
min(...)&lt;br /&gt;
max(...)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 37. Exercise 3 — Word frequency ==&lt;br /&gt;
&lt;br /&gt;
Given a sentence, count how many times each word occurs.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python is simple and python is powerful&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
should produce information equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python -&amp;gt; 2&lt;br /&gt;
is -&amp;gt; 2&lt;br /&gt;
simple -&amp;gt; 1&lt;br /&gt;
and -&amp;gt; 1&lt;br /&gt;
powerful -&amp;gt; 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A possible C++ data structure and its Python equivalent are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt;&lt;br /&gt;
    frequencies;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
frequencies = {}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sentence.split()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to separate the sentence into words.&lt;br /&gt;
&lt;br /&gt;
== 38. Exercise 4 — Remove duplicates ==&lt;br /&gt;
&lt;br /&gt;
Given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [4, 2, 7, 4, 2, 9, 7, 1]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
create a collection containing every distinct value.&lt;br /&gt;
&lt;br /&gt;
Think about the following C++ and Python data structures:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; unique_values;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
unique_values = set()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Try to solve the problem both:&lt;br /&gt;
&lt;br /&gt;
# using an explicit loop;&lt;br /&gt;
&lt;br /&gt;
# using Python&amp;#039;s built-in functionality.&lt;br /&gt;
&lt;br /&gt;
== 39. Exercise 5 — Small problem ==&lt;br /&gt;
&lt;br /&gt;
Write a program that receives a list of students and their grades and determines:&lt;br /&gt;
&lt;br /&gt;
* the class average;&lt;br /&gt;
* the student with the highest grade;&lt;br /&gt;
* all students who passed;&lt;br /&gt;
* all students whose grade is above the class average.&lt;br /&gt;
&lt;br /&gt;
Suggested representation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt;&lt;br /&gt;
    grades;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
    &amp;quot;Dan&amp;quot;: 4.0,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Avoid using numeric indices unless an index is actually required.&lt;br /&gt;
&lt;br /&gt;
== 40. Creating a project environment ==&lt;br /&gt;
&lt;br /&gt;
Python applications commonly use a &amp;#039;&amp;#039;&amp;#039;virtual environment&amp;#039;&amp;#039;&amp;#039; to isolate project dependencies.&lt;br /&gt;
&lt;br /&gt;
Create a directory for the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir python-project&lt;br /&gt;
cd python-project&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a virtual environment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 -m venv .venv&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Linux/macOS:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source .venv/bin/activate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Windows PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
.venv\Scripts\Activate.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command prompt will normally indicate that the environment is active.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
(.venv) student@computer:~/python-project$&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Packages installed using &amp;lt;code&amp;gt;pip&amp;lt;/code&amp;gt; will now be installed inside this environment.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip install requests&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To inspect installed packages:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip list&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 41. Recommended initial project structure ==&lt;br /&gt;
&lt;br /&gt;
For the first laboratories, the following structure is sufficient:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python-project/&lt;br /&gt;
├── .venv/&lt;br /&gt;
├── main.py&lt;br /&gt;
├── README.md&lt;br /&gt;
└── .gitignore&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A basic &amp;lt;code&amp;gt;.gitignore&amp;lt;/code&amp;gt; should contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.venv/&lt;br /&gt;
__pycache__/&lt;br /&gt;
*.pyc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; commit the virtual environment to Git.&lt;br /&gt;
&lt;br /&gt;
== 42. C/C++ habits to reconsider in Python ==&lt;br /&gt;
&lt;br /&gt;
When transitioning from C/C++ to Python, the following patterns should generally be reconsidered.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ habit&lt;br /&gt;
&lt;br /&gt;
| ! Typical Python approach                |&lt;br /&gt;
| ---------------------------------------- |&lt;br /&gt;
| Iterate using an integer index           |&lt;br /&gt;
| Iterate directly over the objects        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually maintain counters               |&lt;br /&gt;
| Consider &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Search manually through a container      |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually calculate container size        |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;len()&amp;lt;/code&amp;gt;                   |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit temporary variable for swapping |&lt;br /&gt;
| Use tuple unpacking                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit loop for simple transformations |&lt;br /&gt;
| Consider a comprehension                 |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Long output expressions                  |&lt;br /&gt;
| Use f-strings                            |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Copy assumed after assignment            |&lt;br /&gt;
| Remember that names refer to objects     |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Check null using equality                |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;is None&amp;lt;/code&amp;gt;                 |&lt;br /&gt;
| }                                        |&lt;br /&gt;
&lt;br /&gt;
The objective is not simply to translate C/C++ syntax into Python syntax.&lt;br /&gt;
&lt;br /&gt;
Consider:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++ style translated literally&lt;br /&gt;
&lt;br /&gt;
| ! More idiomatic Python |&lt;br /&gt;
| ----------------------- |&lt;br /&gt;
|                         |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
    print(values[i])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for value in values:&lt;br /&gt;
    print(value)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Both programs are correct.&lt;br /&gt;
&lt;br /&gt;
The second expresses the programmer&amp;#039;s intention more directly.&lt;br /&gt;
&lt;br /&gt;
== 43. Summary ==&lt;br /&gt;
&lt;br /&gt;
The main mappings introduced in this laboratory are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python                                    |   |         |&lt;br /&gt;
| ------------------------------------------- | - | ------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;{ ... }&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| indentation                                 |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;                                      |   | &amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;                              |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt;                    |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_set&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::tuple&amp;lt;/code&amp;gt;                     |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;tuple&amp;lt;/code&amp;gt;                          |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt;                      |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cin&amp;lt;/code&amp;gt;                       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| indexed iteration                           |   |         |&lt;br /&gt;
| direct iteration / &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| manual search                               |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::pow(a, b)&amp;lt;/code&amp;gt;                 |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;a ** b&amp;lt;/code&amp;gt;                         |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| integer division                            |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| }                                           |   |         |&lt;br /&gt;
&lt;br /&gt;
The central idea to retain from this laboratory is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Python should not be treated merely as C++ with different syntax. Python provides its own abstractions and idioms, and using them generally produces simpler and more readable programs.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 44. Preparation for Lab 2 ==&lt;br /&gt;
&lt;br /&gt;
Before the next laboratory:&lt;br /&gt;
&lt;br /&gt;
# complete all exercises from this laboratory;&lt;br /&gt;
&lt;br /&gt;
# ensure Python 3 is installed;&lt;br /&gt;
&lt;br /&gt;
# create a virtual environment successfully;&lt;br /&gt;
&lt;br /&gt;
# create a Git repository for the semester project;&lt;br /&gt;
&lt;br /&gt;
# become familiar with lists, dictionaries, tuples and sets;&lt;br /&gt;
&lt;br /&gt;
# review the concepts of classes, objects, inheritance and composition from C++.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Lab 2&amp;#039;&amp;#039;&amp;#039;, these concepts will be revisited from the perspective of Python&amp;#039;s object model and Pythonic object-oriented programming.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8363</id>
		<title>Lab 1</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8363"/>
		<updated>2026-09-14T05:32:14Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* 6. Printing values */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 1 — Python for C/C++ Programmers =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
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++.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
After completing this laboratory, you should be able to:&lt;br /&gt;
&lt;br /&gt;
* understand the basic syntax of Python;&lt;br /&gt;
* recognize the main differences between Python and C/C++;&lt;br /&gt;
* work with Python&amp;#039;s fundamental data types;&lt;br /&gt;
* use conditional statements and loops;&lt;br /&gt;
* work with lists, dictionaries, sets and tuples;&lt;br /&gt;
* define and call functions;&lt;br /&gt;
* understand basic Python object/reference semantics;&lt;br /&gt;
* recognize several common Python idioms;&lt;br /&gt;
* create and execute a simple Python project.&lt;br /&gt;
&lt;br /&gt;
Throughout this laboratory, equivalent or similar C/C++ and Python code will be presented side by side.&lt;br /&gt;
&lt;br /&gt;
**TOC**&lt;br /&gt;
&lt;br /&gt;
== 1. Running a Python program ==&lt;br /&gt;
&lt;br /&gt;
A C/C++ program normally has to be compiled before it is executed. Python programs are usually executed directly by the Python interpreter.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Hello, world!\n&amp;quot;;&lt;br /&gt;
return 0;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(&amp;quot;Hello, world!&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A Python source file normally uses the extension:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be executed using:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unlike C/C++, Python does not require a special &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
However, larger Python programs commonly use:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Program started\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
print(&amp;quot;Program started&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
if **name** == &amp;quot;**main**&amp;quot;:&lt;br /&gt;
main() &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python version is not mandatory, but this pattern will become useful when programs are split into multiple modules.&lt;br /&gt;
&lt;br /&gt;
== 2. Blocks and indentation ==&lt;br /&gt;
&lt;br /&gt;
In C and C++, braces delimit blocks of code.&lt;br /&gt;
&lt;br /&gt;
Python uses &amp;#039;&amp;#039;&amp;#039;indentation&amp;#039;&amp;#039;&amp;#039; instead.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (x &amp;gt; 10) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Large\n&amp;quot;;&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Value: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
    print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
    print(&amp;quot;Value:&amp;quot;, x)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Indentation in Python is part of the language syntax.&lt;br /&gt;
&lt;br /&gt;
The standard convention is &amp;#039;&amp;#039;&amp;#039;4 spaces per indentation level&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
For example, the following code is incorrect:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python will report an &amp;lt;code&amp;gt;IndentationError&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 3. Variables and types ==&lt;br /&gt;
&lt;br /&gt;
C/C++ variables have a declared static type.&lt;br /&gt;
&lt;br /&gt;
Python variables do not require explicit type declarations.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int count = 10;&lt;br /&gt;
double temperature = 23.5;&lt;br /&gt;
bool enabled = true;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
count = 10&lt;br /&gt;
temperature = 23.5&lt;br /&gt;
enabled = True&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python is &amp;#039;&amp;#039;&amp;#039;dynamically typed&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
This means that a variable name can later refer to an object of another type.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
// Not allowed:&lt;br /&gt;
// value = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
value = &amp;quot;hello&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This does &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; mean that Python has no types.&lt;br /&gt;
&lt;br /&gt;
Objects still have types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;typeinfo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; typeid(value).name();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
&lt;br /&gt;
print(type(value))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python will produce:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common basic types ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ type&lt;br /&gt;
! Python equivalent&lt;br /&gt;
! Example&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;42&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt; / &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;3.14&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt;&lt;br /&gt;
| No separate character type&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;A&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;str&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;Hello&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 4. Constants ==&lt;br /&gt;
&lt;br /&gt;
C++ can enforce that a value cannot be modified using &amp;lt;code&amp;gt;const&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python does not enforce constants at language level.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
const double PI = 3.1415926535;&lt;br /&gt;
const int MAX_USERS = 100; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
PI = 3.1415926535&lt;br /&gt;
MAX_USERS = 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
By convention, Python names that should be treated as constants are written using uppercase letters.&lt;br /&gt;
&lt;br /&gt;
Nothing prevents the programmer from changing them.&lt;br /&gt;
&lt;br /&gt;
== 5. Arithmetic operators ==&lt;br /&gt;
&lt;br /&gt;
Most arithmetic operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
int sum  = a + b;&lt;br /&gt;
int diff = a - b;&lt;br /&gt;
int prod = a * b;&lt;br /&gt;
int div  = a / b;&lt;br /&gt;
int rem  = a % b; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
sum_ = a + b&lt;br /&gt;
diff = a - b&lt;br /&gt;
prod = a * b&lt;br /&gt;
div = a // b&lt;br /&gt;
rem = a % b &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
There is an important difference between &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a / b;   // 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
print(a / b)   # 3.3333333333333335&lt;br /&gt;
print(a // b)  # 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In C/C++, dividing two integers performs integer division.&lt;br /&gt;
&lt;br /&gt;
In Python, the &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; operator always performs floating-point division, while &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt; performs floor division.&lt;br /&gt;
&lt;br /&gt;
Python also provides an exponentiation operator:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;cmath&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double result = std::pow(2, 8); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
result = 2 ** 8 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 6. Printing values ==&lt;br /&gt;
&lt;br /&gt;
Output using &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt; is replaced by the built-in &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
int age = 21;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; is &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; age&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; years old\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
age = 21&lt;br /&gt;
&lt;br /&gt;
print(name, &amp;quot;is&amp;quot;, age, &amp;quot;years old&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python also provides formatted string literals, commonly called &amp;#039;&amp;#039;&amp;#039;f-strings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Temperature: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; temperature&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; C\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Temperature: {temperature} C&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Expressions can appear directly inside an f-string:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Result: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; a + b&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Result: {a + b}&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 7. Reading input ==&lt;br /&gt;
&lt;br /&gt;
Console input is performed using &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An important difference is that &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt; &amp;#039;&amp;#039;&amp;#039;always returns a string&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int age;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Age: &amp;quot;;&lt;br /&gt;
std::cin &amp;gt;&amp;gt; age; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = int(input(&amp;quot;Age: &amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Without the conversion:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = input(&amp;quot;Age: &amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; is a string.&lt;br /&gt;
&lt;br /&gt;
Conversions are explicit:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = std::stoi(text);&lt;br /&gt;
double b = std::stod(text);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = int(text)&lt;br /&gt;
b = float(text)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 8. Conditional statements ==&lt;br /&gt;
&lt;br /&gt;
The overall structure is similar, but Python does not use parentheses or braces.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (temperature &amp;gt; 30) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Hot\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else if (temperature &amp;gt; 20) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Warm\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Cold\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if temperature &amp;gt; 30:&lt;br /&gt;
    print(&amp;quot;Hot&amp;quot;)&lt;br /&gt;
elif temperature &amp;gt; 20:&lt;br /&gt;
    print(&amp;quot;Warm&amp;quot;)&lt;br /&gt;
else:&lt;br /&gt;
    print(&amp;quot;Cold&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python uses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
elif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
instead of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
else if&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 9. Boolean operators ==&lt;br /&gt;
&lt;br /&gt;
Python uses words instead of the C/C++ symbolic logical operators.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python         |   |         |&lt;br /&gt;
| ---------------- | - | ------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt;  |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| -                |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;           |   | &amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;  |   |         |&lt;br /&gt;
| -                |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;   |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| }                |   |         |&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (age &amp;gt;= 18 &amp;amp;&amp;amp; enabled) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if (!enabled) {&lt;br /&gt;
...&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if age &amp;gt;= 18 and enabled:&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
if not enabled:&lt;br /&gt;
... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 10. Comparison operators ==&lt;br /&gt;
&lt;br /&gt;
Most comparison operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Operation&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python              |&lt;br /&gt;
| --------------------- |&lt;br /&gt;
| equality              |&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| inequality            |&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| less than             |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                     |&lt;br /&gt;
| greater than          |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                     |&lt;br /&gt;
| less than or equal    |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| greater than or equal |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| }                     |&lt;br /&gt;
&lt;br /&gt;
Python also allows chained comparisons.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (0 &amp;lt;= x &amp;amp;&amp;amp; x &amp;lt; 100) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 0 &amp;lt;= x &amp;lt; 100:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 11. The ternary operator ==&lt;br /&gt;
&lt;br /&gt;
Both languages support conditional expressions.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int maximum = (a &amp;gt; b) ? a : b;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
maximum = a if a &amp;gt; b else b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python&amp;#039;s order is different:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
value_if_true if condition else value_if_false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 12. While loops ==&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;code&amp;gt;while&amp;lt;/code&amp;gt; loop is very similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int i = 0;&lt;br /&gt;
&lt;br /&gt;
while (i &amp;lt; 5) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
i++;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
&lt;br /&gt;
while i &amp;lt; 5:&lt;br /&gt;
print(i)&lt;br /&gt;
i += 1 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not have &amp;lt;code&amp;gt;++&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;--&amp;lt;/code&amp;gt; operators.&lt;br /&gt;
&lt;br /&gt;
Therefore:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i += 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is used instead.&lt;br /&gt;
&lt;br /&gt;
== 13. For loops ==&lt;br /&gt;
&lt;br /&gt;
This is one of the first important conceptual differences.&lt;br /&gt;
&lt;br /&gt;
In C/C++, a &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement commonly controls a numeric index.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement iterates over objects.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 5; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;range(5)&amp;lt;/code&amp;gt; generates values conceptually equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
0, 1, 2, 3, 4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A different starting value can be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 2; i &amp;lt; 10; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(2, 10):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A step can also be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i += 2) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(0, 10, 2):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 14. Iterating over containers ==&lt;br /&gt;
&lt;br /&gt;
Suppose we want to print every element of a vector/list.&lt;br /&gt;
&lt;br /&gt;
An index-based implementation is possible:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; values.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[i] &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
print(values[i]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
However, this is generally &amp;#039;&amp;#039;&amp;#039;not the preferred Python style&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
If the index is not required, iterate directly over the elements.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; value : values) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; value &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for value in values:&lt;br /&gt;
print(value) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The second Python version should normally be preferred.&lt;br /&gt;
&lt;br /&gt;
== 15. When both index and value are required ==&lt;br /&gt;
&lt;br /&gt;
C/C++ programmers frequently use an explicit index.&lt;br /&gt;
&lt;br /&gt;
Python provides &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;std::string&amp;gt; names = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; names.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; names[i]&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
names = [&amp;quot;Ana&amp;quot;, &amp;quot;Bob&amp;quot;, &amp;quot;Carol&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
for i, name in enumerate(names):&lt;br /&gt;
print(i, &amp;quot;:&amp;quot;, name) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This introduces another frequently used Python feature: &amp;#039;&amp;#039;&amp;#039;unpacking&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== 16. Lists ==&lt;br /&gt;
&lt;br /&gt;
The closest commonly used Python equivalent to &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.push_back(40);&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[0];&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values.size(); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
values.append(40)&lt;br /&gt;
&lt;br /&gt;
print(values[0])&lt;br /&gt;
print(len(values)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python lists can contain objects of different types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// A normal std::vector cannot&lt;br /&gt;
// contain unrelated types.&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
10, 20, 30&lt;br /&gt;
}; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [&lt;br /&gt;
10,&lt;br /&gt;
3.14,&lt;br /&gt;
&amp;quot;hello&amp;quot;,&lt;br /&gt;
True&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Although Python allows this, homogeneous collections are generally easier to understand and maintain when the elements conceptually represent the same kind of data.&lt;br /&gt;
&lt;br /&gt;
== 17. Negative indexing ==&lt;br /&gt;
&lt;br /&gt;
Python allows indexing from the end of a sequence.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {10, 20, 30, 40};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 1]; // 40&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 2]; // 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40]&lt;br /&gt;
&lt;br /&gt;
print(v[-1])  # 40&lt;br /&gt;
print(v[-2])  # 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 18. Slicing ==&lt;br /&gt;
&lt;br /&gt;
Python sequences support slicing directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {&lt;br /&gt;
    10, 20, 30, 40, 50&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; part(&lt;br /&gt;
v.begin() + 1,&lt;br /&gt;
v.begin() + 4&lt;br /&gt;
); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40, 50]&lt;br /&gt;
&lt;br /&gt;
part = v[1:4] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The result is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[20, 30, 40]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
General syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
sequence[start:stop:step]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// Requires an explicit loop or&lt;br /&gt;
// another algorithm.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [0, 1, 2, 3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
print(v[1:4])   # [1, 2, 3]&lt;br /&gt;
print(v[:3])    # [0, 1, 2]&lt;br /&gt;
print(v[3:])    # [3, 4, 5]&lt;br /&gt;
print(v[::2])   # [0, 2, 4]&lt;br /&gt;
print(v[::-1])  # [5, 4, 3, 2, 1, 0] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 19. Membership testing ==&lt;br /&gt;
&lt;br /&gt;
Checking whether an element exists in a container is commonly performed using &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
if (std::find(&lt;br /&gt;
values.begin(),&lt;br /&gt;
values.end(),&lt;br /&gt;
20&lt;br /&gt;
) != values.end()) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The opposite test uses &amp;lt;code&amp;gt;not in&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 100 not in values:&lt;br /&gt;
    print(&amp;quot;Not found&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 20. Dictionaries ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt; is similar to a C++ associative container such as &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades;&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10;&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; grades[&amp;quot;Ana&amp;quot;]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {}&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8&lt;br /&gt;
&lt;br /&gt;
print(grades[&amp;quot;Ana&amp;quot;]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A dictionary can also be initialized directly:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades = {&lt;br /&gt;
    {&amp;quot;Ana&amp;quot;, 10},&lt;br /&gt;
    {&amp;quot;Bob&amp;quot;, 8},&lt;br /&gt;
    {&amp;quot;Carol&amp;quot;, 9}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 10,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 8,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 9,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Iterating over keys and values:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; grade&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
    print(name, &amp;quot;:&amp;quot;, grade)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 21. Sets ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; represents a collection containing unique values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.insert(40);&lt;br /&gt;
&lt;br /&gt;
if (20 != values.end()) {&lt;br /&gt;
// conceptually test membership&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = {10, 20, 30}&lt;br /&gt;
&lt;br /&gt;
values.add(40)&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A more correct C++ membership test would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (values.find(20) != values.end()) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets are particularly useful when:&lt;br /&gt;
&lt;br /&gt;
* duplicate values should be removed;&lt;br /&gt;
* fast membership tests are required;&lt;br /&gt;
* mathematical set operations are useful.&lt;br /&gt;
&lt;br /&gt;
== 22. Tuples ==&lt;br /&gt;
&lt;br /&gt;
Tuples are similar to lists, but they are immutable.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::tuple&amp;lt;int, double, std::string&amp;gt; data {&lt;br /&gt;
    10,&lt;br /&gt;
    3.14,&lt;br /&gt;
    &amp;quot;test&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
data = (10, 3.14, &amp;quot;test&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python uses tuples extensively for returning or unpacking multiple values.&lt;br /&gt;
&lt;br /&gt;
== 23. Functions ==&lt;br /&gt;
&lt;br /&gt;
Functions are declared using &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    return a + b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int result = add(2, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def add(a, b):&lt;br /&gt;
return a + b&lt;br /&gt;
&lt;br /&gt;
result = add(2, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not require parameter or return types to be declared.&lt;br /&gt;
&lt;br /&gt;
However, Python supports optional &amp;#039;&amp;#039;&amp;#039;type hints&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
double average(&lt;br /&gt;
    double a,&lt;br /&gt;
    double b&lt;br /&gt;
) {&lt;br /&gt;
    return (a + b) / 2.0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(&lt;br /&gt;
    a: float,&lt;br /&gt;
    b: float&lt;br /&gt;
) -&amp;gt; float:&lt;br /&gt;
    return (a + b) / 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Type hints do not normally enforce types at runtime.&lt;br /&gt;
&lt;br /&gt;
They provide information for:&lt;br /&gt;
&lt;br /&gt;
* the programmer;&lt;br /&gt;
* IDEs;&lt;br /&gt;
* static analysis tools;&lt;br /&gt;
* documentation.&lt;br /&gt;
&lt;br /&gt;
== 24. Default parameters ==&lt;br /&gt;
&lt;br /&gt;
Both C++ and Python support default parameter values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void greet(&lt;br /&gt;
    const std::string&amp;amp; name = &amp;quot;World&amp;quot;&lt;br /&gt;
) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Hello, &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
greet();&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def greet(name=&amp;quot;World&amp;quot;):&lt;br /&gt;
print(f&amp;quot;Hello, {name}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
greet()&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 25. Named arguments ==&lt;br /&gt;
&lt;br /&gt;
Python allows function arguments to be specified by name.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void connect(&lt;br /&gt;
    std::string host,&lt;br /&gt;
    int port,&lt;br /&gt;
    bool secure&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
connect(&amp;quot;server.local&amp;quot;, 443, true); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def connect(host, port, secure):&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
connect(&lt;br /&gt;
host=&amp;quot;server.local&amp;quot;,&lt;br /&gt;
port=443,&lt;br /&gt;
secure=True&lt;br /&gt;
) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This can make calls with several parameters easier to understand.&lt;br /&gt;
&lt;br /&gt;
== 26. Returning multiple values ==&lt;br /&gt;
&lt;br /&gt;
In C++, multiple return values typically require a structure, tuple, pair or output parameters.&lt;br /&gt;
&lt;br /&gt;
Python commonly uses tuples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::pair&amp;lt;int, int&amp;gt; minmax(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    if (a &amp;lt; b)&lt;br /&gt;
        return {a, b};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return {b, a};&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
auto [minimum, maximum] = minmax(10, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def minmax(a, b):&lt;br /&gt;
if a &amp;lt; b:&lt;br /&gt;
return a, b&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return b, a&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
minimum, maximum = minmax(10, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is another example of tuple unpacking.&lt;br /&gt;
&lt;br /&gt;
== 27. Swapping variables ==&lt;br /&gt;
&lt;br /&gt;
A temporary variable is normally used in C.&lt;br /&gt;
&lt;br /&gt;
Python supports unpacking directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int temp = a;&lt;br /&gt;
a = b;&lt;br /&gt;
b = temp;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a, b = b, a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 28. Strings ==&lt;br /&gt;
&lt;br /&gt;
Python strings behave more like C++ &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt; objects than traditional C character arrays.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text.size();&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text[0]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(len(text))&lt;br /&gt;
print(text[0]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
String concatenation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string first = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
std::string second = &amp;quot;World&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::string result =&lt;br /&gt;
first + &amp;quot; &amp;quot; + second; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
first = &amp;quot;Hello&amp;quot;&lt;br /&gt;
second = &amp;quot;World&amp;quot;&lt;br /&gt;
&lt;br /&gt;
result = first + &amp;quot; &amp;quot; + second &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python strings provide many built-in operations:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Usually requires algorithms,&lt;br /&gt;
// explicit transformations,&lt;br /&gt;
// or utility functions. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(text.upper())&lt;br /&gt;
print(text.lower())&lt;br /&gt;
print(text.startswith(&amp;quot;he&amp;quot;))&lt;br /&gt;
print(text.endswith(&amp;quot;lo&amp;quot;))&lt;br /&gt;
print(text.replace(&amp;quot;l&amp;quot;, &amp;quot;x&amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 29. Lists versus arrays: important semantic difference ==&lt;br /&gt;
&lt;br /&gt;
Consider assigning one collection to another.&lt;br /&gt;
&lt;br /&gt;
With normal C++ value semantics, copying a vector produces another vector.&lt;br /&gt;
&lt;br /&gt;
Python assignment behaves differently.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&lt;br /&gt;
b[0] = 100;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a[0];  // 1&lt;br /&gt;
std::cout &amp;lt;&amp;lt; b[0];  // 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
b = a&lt;br /&gt;
&lt;br /&gt;
b[0] = 100&lt;br /&gt;
&lt;br /&gt;
print(a[0])  # 100&lt;br /&gt;
print(b[0])  # 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This happens because:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
does not copy the list.&lt;br /&gt;
&lt;br /&gt;
Both names refer to the &amp;#039;&amp;#039;&amp;#039;same list object&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Conceptually:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
       +-------------+&lt;br /&gt;
a ---&amp;gt; | [1, 2, 3]   |&lt;br /&gt;
       +-------------+&lt;br /&gt;
          ^&lt;br /&gt;
          |&lt;br /&gt;
b --------+&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To explicitly create a shallow copy:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a.copy()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = list(a)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This distinction is extremely important when using Python.&lt;br /&gt;
&lt;br /&gt;
== 30. Equality versus identity ==&lt;br /&gt;
&lt;br /&gt;
Python distinguishes between:&lt;br /&gt;
&lt;br /&gt;
* whether two objects contain equal values;&lt;br /&gt;
* whether two names refer to the exact same object.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; (a == b);  // true&lt;br /&gt;
&lt;br /&gt;
// Address comparison would be needed&lt;br /&gt;
// to determine object identity. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
b = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
print(a == b)  # True&lt;br /&gt;
print(a is b)  # False &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In Python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
==  tests value equality&lt;br /&gt;
is  tests object identity&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not use &amp;lt;code&amp;gt;is&amp;lt;/code&amp;gt; when you intend to compare values.&lt;br /&gt;
&lt;br /&gt;
A common correct use is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if value is None:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 31. None and nullptr ==&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; represents the absence of a value.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Object* ptr = nullptr;&lt;br /&gt;
&lt;br /&gt;
if (ptr == nullptr) {&lt;br /&gt;
...&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = None&lt;br /&gt;
&lt;br /&gt;
if value is None:&lt;br /&gt;
... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 32. Exceptions ==&lt;br /&gt;
&lt;br /&gt;
Exception handling is conceptually similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    int value = std::stoi(text);&lt;br /&gt;
}&lt;br /&gt;
catch (const std::exception&amp;amp; e) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Invalid value\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    value = int(text)&lt;br /&gt;
except ValueError:&lt;br /&gt;
    print(&amp;quot;Invalid value&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python generally uses exceptions rather than special error return values for many failure conditions.&lt;br /&gt;
&lt;br /&gt;
== 33. A first Pythonic transformation ==&lt;br /&gt;
&lt;br /&gt;
Consider creating a vector/list containing the squares of the numbers from 0 to 9.&lt;br /&gt;
&lt;br /&gt;
A direct translation from C++ could look like this:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — direct translation |&lt;br /&gt;
| ----------------------------- |&lt;br /&gt;
|                               |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = []&lt;br /&gt;
&lt;br /&gt;
for i in range(10):&lt;br /&gt;
squares.append(i * i) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python code is correct.&lt;br /&gt;
&lt;br /&gt;
However, Python provides a concise construct called a &amp;#039;&amp;#039;&amp;#039;list comprehension&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — idiomatic |&lt;br /&gt;
| -------------------- |&lt;br /&gt;
|                      |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A condition can also be included.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
if (i % 2 == 0) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
}&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
if i % 2 == 0&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
An important objective of this course is not only to write programs that work in Python, but to learn to write programs in a style appropriate for Python.&lt;br /&gt;
&lt;br /&gt;
== 34. Example: processing student grades ==&lt;br /&gt;
&lt;br /&gt;
Consider the following problem:&lt;br /&gt;
&lt;br /&gt;
* store the grades of several students;&lt;br /&gt;
* compute the average;&lt;br /&gt;
* print the students whose grade is greater than or equal to the average.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;unordered_map&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt; grades = {&lt;br /&gt;
{&amp;quot;Ana&amp;quot;, 9.5},&lt;br /&gt;
{&amp;quot;Bob&amp;quot;, 7.0},&lt;br /&gt;
{&amp;quot;Carol&amp;quot;, 8.5},&lt;br /&gt;
{&amp;quot;Dan&amp;quot;, 6.0}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
double sum = 0.0;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    sum += grade;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
double average = sum / grades.size();&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Average: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; average&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    if (grade &amp;gt;= average) {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
                  &amp;lt;&amp;lt; grade&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
&amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
&amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
&amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
&amp;quot;Dan&amp;quot;: 6.0,&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
average = sum(grades.values()) / len(grades)&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;Average: {average:.2f}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
if grade &amp;gt;= average:&lt;br /&gt;
print(f&amp;quot;{name}: {grade}&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that the Python version uses existing language functionality rather than manually reproducing every operation.&lt;br /&gt;
&lt;br /&gt;
In particular:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sum(grades.values())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
expresses the intention directly.&lt;br /&gt;
&lt;br /&gt;
== 35. Exercise 1 — Translation ==&lt;br /&gt;
&lt;br /&gt;
Translate the following C++ program into Python.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
12, 7, 18, 3, 24, 11&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
int sum = 0;&lt;br /&gt;
int count = 0;&lt;br /&gt;
&lt;br /&gt;
for (int value : values) {&lt;br /&gt;
    if (value % 2 == 0) {&lt;br /&gt;
        sum += value;&lt;br /&gt;
        count++;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Count: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; count&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Sum: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; sum&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Your implementation:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# TODO&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
After implementing a direct translation, try to simplify the Python implementation.&lt;br /&gt;
&lt;br /&gt;
== 36. Exercise 2 — Statistics ==&lt;br /&gt;
&lt;br /&gt;
Write a Python program that:&lt;br /&gt;
&lt;br /&gt;
# reads a sequence of integer values;&lt;br /&gt;
&lt;br /&gt;
# stores them in a list;&lt;br /&gt;
&lt;br /&gt;
# prints the minimum value;&lt;br /&gt;
&lt;br /&gt;
# prints the maximum value;&lt;br /&gt;
&lt;br /&gt;
# prints the average;&lt;br /&gt;
&lt;br /&gt;
# prints how many values are above the average.&lt;br /&gt;
&lt;br /&gt;
The equivalent C++ structure might begin as follows:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values;&lt;br /&gt;
&lt;br /&gt;
// Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
int minimum = ...;&lt;br /&gt;
int maximum = ...;&lt;br /&gt;
double average = ...; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = []&lt;br /&gt;
&lt;br /&gt;
# Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
minimum = ...&lt;br /&gt;
maximum = ...&lt;br /&gt;
average = ... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Useful Python functions include:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
len(...)&lt;br /&gt;
sum(...)&lt;br /&gt;
min(...)&lt;br /&gt;
max(...)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 37. Exercise 3 — Word frequency ==&lt;br /&gt;
&lt;br /&gt;
Given a sentence, count how many times each word occurs.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python is simple and python is powerful&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
should produce information equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python -&amp;gt; 2&lt;br /&gt;
is -&amp;gt; 2&lt;br /&gt;
simple -&amp;gt; 1&lt;br /&gt;
and -&amp;gt; 1&lt;br /&gt;
powerful -&amp;gt; 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A possible C++ data structure and its Python equivalent are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt;&lt;br /&gt;
    frequencies;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
frequencies = {}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sentence.split()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to separate the sentence into words.&lt;br /&gt;
&lt;br /&gt;
== 38. Exercise 4 — Remove duplicates ==&lt;br /&gt;
&lt;br /&gt;
Given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [4, 2, 7, 4, 2, 9, 7, 1]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
create a collection containing every distinct value.&lt;br /&gt;
&lt;br /&gt;
Think about the following C++ and Python data structures:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; unique_values;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
unique_values = set()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Try to solve the problem both:&lt;br /&gt;
&lt;br /&gt;
# using an explicit loop;&lt;br /&gt;
&lt;br /&gt;
# using Python&amp;#039;s built-in functionality.&lt;br /&gt;
&lt;br /&gt;
== 39. Exercise 5 — Small problem ==&lt;br /&gt;
&lt;br /&gt;
Write a program that receives a list of students and their grades and determines:&lt;br /&gt;
&lt;br /&gt;
* the class average;&lt;br /&gt;
* the student with the highest grade;&lt;br /&gt;
* all students who passed;&lt;br /&gt;
* all students whose grade is above the class average.&lt;br /&gt;
&lt;br /&gt;
Suggested representation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt;&lt;br /&gt;
    grades;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
    &amp;quot;Dan&amp;quot;: 4.0,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Avoid using numeric indices unless an index is actually required.&lt;br /&gt;
&lt;br /&gt;
== 40. Creating a project environment ==&lt;br /&gt;
&lt;br /&gt;
Python applications commonly use a &amp;#039;&amp;#039;&amp;#039;virtual environment&amp;#039;&amp;#039;&amp;#039; to isolate project dependencies.&lt;br /&gt;
&lt;br /&gt;
Create a directory for the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir python-project&lt;br /&gt;
cd python-project&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a virtual environment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 -m venv .venv&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Linux/macOS:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source .venv/bin/activate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Windows PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
.venv\Scripts\Activate.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command prompt will normally indicate that the environment is active.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
(.venv) student@computer:~/python-project$&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Packages installed using &amp;lt;code&amp;gt;pip&amp;lt;/code&amp;gt; will now be installed inside this environment.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip install requests&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To inspect installed packages:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip list&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 41. Recommended initial project structure ==&lt;br /&gt;
&lt;br /&gt;
For the first laboratories, the following structure is sufficient:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python-project/&lt;br /&gt;
├── .venv/&lt;br /&gt;
├── main.py&lt;br /&gt;
├── README.md&lt;br /&gt;
└── .gitignore&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A basic &amp;lt;code&amp;gt;.gitignore&amp;lt;/code&amp;gt; should contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.venv/&lt;br /&gt;
__pycache__/&lt;br /&gt;
*.pyc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; commit the virtual environment to Git.&lt;br /&gt;
&lt;br /&gt;
== 42. C/C++ habits to reconsider in Python ==&lt;br /&gt;
&lt;br /&gt;
When transitioning from C/C++ to Python, the following patterns should generally be reconsidered.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ habit&lt;br /&gt;
&lt;br /&gt;
| ! Typical Python approach                |&lt;br /&gt;
| ---------------------------------------- |&lt;br /&gt;
| Iterate using an integer index           |&lt;br /&gt;
| Iterate directly over the objects        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually maintain counters               |&lt;br /&gt;
| Consider &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Search manually through a container      |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually calculate container size        |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;len()&amp;lt;/code&amp;gt;                   |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit temporary variable for swapping |&lt;br /&gt;
| Use tuple unpacking                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit loop for simple transformations |&lt;br /&gt;
| Consider a comprehension                 |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Long output expressions                  |&lt;br /&gt;
| Use f-strings                            |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Copy assumed after assignment            |&lt;br /&gt;
| Remember that names refer to objects     |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Check null using equality                |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;is None&amp;lt;/code&amp;gt;                 |&lt;br /&gt;
| }                                        |&lt;br /&gt;
&lt;br /&gt;
The objective is not simply to translate C/C++ syntax into Python syntax.&lt;br /&gt;
&lt;br /&gt;
Consider:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++ style translated literally&lt;br /&gt;
&lt;br /&gt;
| ! More idiomatic Python |&lt;br /&gt;
| ----------------------- |&lt;br /&gt;
|                         |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
    print(values[i])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for value in values:&lt;br /&gt;
    print(value)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Both programs are correct.&lt;br /&gt;
&lt;br /&gt;
The second expresses the programmer&amp;#039;s intention more directly.&lt;br /&gt;
&lt;br /&gt;
== 43. Summary ==&lt;br /&gt;
&lt;br /&gt;
The main mappings introduced in this laboratory are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python                                    |   |         |&lt;br /&gt;
| ------------------------------------------- | - | ------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;{ ... }&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| indentation                                 |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;                                      |   | &amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;                              |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt;                    |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_set&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::tuple&amp;lt;/code&amp;gt;                     |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;tuple&amp;lt;/code&amp;gt;                          |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt;                      |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cin&amp;lt;/code&amp;gt;                       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| indexed iteration                           |   |         |&lt;br /&gt;
| direct iteration / &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| manual search                               |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::pow(a, b)&amp;lt;/code&amp;gt;                 |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;a ** b&amp;lt;/code&amp;gt;                         |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| integer division                            |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| }                                           |   |         |&lt;br /&gt;
&lt;br /&gt;
The central idea to retain from this laboratory is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Python should not be treated merely as C++ with different syntax. Python provides its own abstractions and idioms, and using them generally produces simpler and more readable programs.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 44. Preparation for Lab 2 ==&lt;br /&gt;
&lt;br /&gt;
Before the next laboratory:&lt;br /&gt;
&lt;br /&gt;
# complete all exercises from this laboratory;&lt;br /&gt;
&lt;br /&gt;
# ensure Python 3 is installed;&lt;br /&gt;
&lt;br /&gt;
# create a virtual environment successfully;&lt;br /&gt;
&lt;br /&gt;
# create a Git repository for the semester project;&lt;br /&gt;
&lt;br /&gt;
# become familiar with lists, dictionaries, tuples and sets;&lt;br /&gt;
&lt;br /&gt;
# review the concepts of classes, objects, inheritance and composition from C++.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Lab 2&amp;#039;&amp;#039;&amp;#039;, these concepts will be revisited from the perspective of Python&amp;#039;s object model and Pythonic object-oriented programming.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8362</id>
		<title>Lab 1</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8362"/>
		<updated>2026-09-14T05:30:36Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* 5. Arithmetic operators */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 1 — Python for C/C++ Programmers =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
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++.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
After completing this laboratory, you should be able to:&lt;br /&gt;
&lt;br /&gt;
* understand the basic syntax of Python;&lt;br /&gt;
* recognize the main differences between Python and C/C++;&lt;br /&gt;
* work with Python&amp;#039;s fundamental data types;&lt;br /&gt;
* use conditional statements and loops;&lt;br /&gt;
* work with lists, dictionaries, sets and tuples;&lt;br /&gt;
* define and call functions;&lt;br /&gt;
* understand basic Python object/reference semantics;&lt;br /&gt;
* recognize several common Python idioms;&lt;br /&gt;
* create and execute a simple Python project.&lt;br /&gt;
&lt;br /&gt;
Throughout this laboratory, equivalent or similar C/C++ and Python code will be presented side by side.&lt;br /&gt;
&lt;br /&gt;
**TOC**&lt;br /&gt;
&lt;br /&gt;
== 1. Running a Python program ==&lt;br /&gt;
&lt;br /&gt;
A C/C++ program normally has to be compiled before it is executed. Python programs are usually executed directly by the Python interpreter.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Hello, world!\n&amp;quot;;&lt;br /&gt;
return 0;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(&amp;quot;Hello, world!&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A Python source file normally uses the extension:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be executed using:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unlike C/C++, Python does not require a special &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
However, larger Python programs commonly use:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Program started\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
print(&amp;quot;Program started&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
if **name** == &amp;quot;**main**&amp;quot;:&lt;br /&gt;
main() &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python version is not mandatory, but this pattern will become useful when programs are split into multiple modules.&lt;br /&gt;
&lt;br /&gt;
== 2. Blocks and indentation ==&lt;br /&gt;
&lt;br /&gt;
In C and C++, braces delimit blocks of code.&lt;br /&gt;
&lt;br /&gt;
Python uses &amp;#039;&amp;#039;&amp;#039;indentation&amp;#039;&amp;#039;&amp;#039; instead.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (x &amp;gt; 10) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Large\n&amp;quot;;&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Value: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
    print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
    print(&amp;quot;Value:&amp;quot;, x)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Indentation in Python is part of the language syntax.&lt;br /&gt;
&lt;br /&gt;
The standard convention is &amp;#039;&amp;#039;&amp;#039;4 spaces per indentation level&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
For example, the following code is incorrect:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python will report an &amp;lt;code&amp;gt;IndentationError&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 3. Variables and types ==&lt;br /&gt;
&lt;br /&gt;
C/C++ variables have a declared static type.&lt;br /&gt;
&lt;br /&gt;
Python variables do not require explicit type declarations.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int count = 10;&lt;br /&gt;
double temperature = 23.5;&lt;br /&gt;
bool enabled = true;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
count = 10&lt;br /&gt;
temperature = 23.5&lt;br /&gt;
enabled = True&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python is &amp;#039;&amp;#039;&amp;#039;dynamically typed&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
This means that a variable name can later refer to an object of another type.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
// Not allowed:&lt;br /&gt;
// value = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
value = &amp;quot;hello&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This does &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; mean that Python has no types.&lt;br /&gt;
&lt;br /&gt;
Objects still have types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;typeinfo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; typeid(value).name();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
&lt;br /&gt;
print(type(value))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python will produce:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common basic types ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ type&lt;br /&gt;
! Python equivalent&lt;br /&gt;
! Example&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;42&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt; / &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;3.14&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt;&lt;br /&gt;
| No separate character type&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;A&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;str&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;Hello&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 4. Constants ==&lt;br /&gt;
&lt;br /&gt;
C++ can enforce that a value cannot be modified using &amp;lt;code&amp;gt;const&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python does not enforce constants at language level.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
const double PI = 3.1415926535;&lt;br /&gt;
const int MAX_USERS = 100; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
PI = 3.1415926535&lt;br /&gt;
MAX_USERS = 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
By convention, Python names that should be treated as constants are written using uppercase letters.&lt;br /&gt;
&lt;br /&gt;
Nothing prevents the programmer from changing them.&lt;br /&gt;
&lt;br /&gt;
== 5. Arithmetic operators ==&lt;br /&gt;
&lt;br /&gt;
Most arithmetic operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
int sum  = a + b;&lt;br /&gt;
int diff = a - b;&lt;br /&gt;
int prod = a * b;&lt;br /&gt;
int div  = a / b;&lt;br /&gt;
int rem  = a % b; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
sum_ = a + b&lt;br /&gt;
diff = a - b&lt;br /&gt;
prod = a * b&lt;br /&gt;
div = a // b&lt;br /&gt;
rem = a % b &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
There is an important difference between &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a / b;   // 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
print(a / b)   # 3.3333333333333335&lt;br /&gt;
print(a // b)  # 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In C/C++, dividing two integers performs integer division.&lt;br /&gt;
&lt;br /&gt;
In Python, the &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; operator always performs floating-point division, while &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt; performs floor division.&lt;br /&gt;
&lt;br /&gt;
Python also provides an exponentiation operator:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;cmath&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double result = std::pow(2, 8); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
result = 2 ** 8 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 6. Printing values ==&lt;br /&gt;
&lt;br /&gt;
Output using &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt; is replaced by the built-in &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
int age = 21;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; is &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; age&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; years old\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
age = 21&lt;br /&gt;
&lt;br /&gt;
print(name, &amp;quot;is&amp;quot;, age, &amp;quot;years old&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python also provides formatted string literals, commonly called &amp;#039;&amp;#039;&amp;#039;f-strings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Temperature: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; temperature&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot; C\n&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Temperature: {temperature} C&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Expressions can appear directly inside an f-string:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Result: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; a + b&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Result: {a + b}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 7. Reading input ==&lt;br /&gt;
&lt;br /&gt;
Console input is performed using &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An important difference is that &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt; &amp;#039;&amp;#039;&amp;#039;always returns a string&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int age;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Age: &amp;quot;;&lt;br /&gt;
std::cin &amp;gt;&amp;gt; age; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = int(input(&amp;quot;Age: &amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Without the conversion:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = input(&amp;quot;Age: &amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; is a string.&lt;br /&gt;
&lt;br /&gt;
Conversions are explicit:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = std::stoi(text);&lt;br /&gt;
double b = std::stod(text);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = int(text)&lt;br /&gt;
b = float(text)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 8. Conditional statements ==&lt;br /&gt;
&lt;br /&gt;
The overall structure is similar, but Python does not use parentheses or braces.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (temperature &amp;gt; 30) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Hot\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else if (temperature &amp;gt; 20) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Warm\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Cold\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if temperature &amp;gt; 30:&lt;br /&gt;
    print(&amp;quot;Hot&amp;quot;)&lt;br /&gt;
elif temperature &amp;gt; 20:&lt;br /&gt;
    print(&amp;quot;Warm&amp;quot;)&lt;br /&gt;
else:&lt;br /&gt;
    print(&amp;quot;Cold&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python uses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
elif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
instead of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
else if&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 9. Boolean operators ==&lt;br /&gt;
&lt;br /&gt;
Python uses words instead of the C/C++ symbolic logical operators.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python         |   |         |&lt;br /&gt;
| ---------------- | - | ------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt;  |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| -                |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;           |   | &amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;  |   |         |&lt;br /&gt;
| -                |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;   |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| }                |   |         |&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (age &amp;gt;= 18 &amp;amp;&amp;amp; enabled) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if (!enabled) {&lt;br /&gt;
...&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if age &amp;gt;= 18 and enabled:&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
if not enabled:&lt;br /&gt;
... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 10. Comparison operators ==&lt;br /&gt;
&lt;br /&gt;
Most comparison operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Operation&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python              |&lt;br /&gt;
| --------------------- |&lt;br /&gt;
| equality              |&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| inequality            |&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| less than             |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                     |&lt;br /&gt;
| greater than          |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                     |&lt;br /&gt;
| less than or equal    |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| greater than or equal |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| }                     |&lt;br /&gt;
&lt;br /&gt;
Python also allows chained comparisons.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (0 &amp;lt;= x &amp;amp;&amp;amp; x &amp;lt; 100) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 0 &amp;lt;= x &amp;lt; 100:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 11. The ternary operator ==&lt;br /&gt;
&lt;br /&gt;
Both languages support conditional expressions.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int maximum = (a &amp;gt; b) ? a : b;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
maximum = a if a &amp;gt; b else b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python&amp;#039;s order is different:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
value_if_true if condition else value_if_false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 12. While loops ==&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;code&amp;gt;while&amp;lt;/code&amp;gt; loop is very similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int i = 0;&lt;br /&gt;
&lt;br /&gt;
while (i &amp;lt; 5) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
i++;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
&lt;br /&gt;
while i &amp;lt; 5:&lt;br /&gt;
print(i)&lt;br /&gt;
i += 1 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not have &amp;lt;code&amp;gt;++&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;--&amp;lt;/code&amp;gt; operators.&lt;br /&gt;
&lt;br /&gt;
Therefore:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i += 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is used instead.&lt;br /&gt;
&lt;br /&gt;
== 13. For loops ==&lt;br /&gt;
&lt;br /&gt;
This is one of the first important conceptual differences.&lt;br /&gt;
&lt;br /&gt;
In C/C++, a &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement commonly controls a numeric index.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement iterates over objects.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 5; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;range(5)&amp;lt;/code&amp;gt; generates values conceptually equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
0, 1, 2, 3, 4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A different starting value can be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 2; i &amp;lt; 10; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(2, 10):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A step can also be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i += 2) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(0, 10, 2):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 14. Iterating over containers ==&lt;br /&gt;
&lt;br /&gt;
Suppose we want to print every element of a vector/list.&lt;br /&gt;
&lt;br /&gt;
An index-based implementation is possible:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; values.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[i] &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
print(values[i]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
However, this is generally &amp;#039;&amp;#039;&amp;#039;not the preferred Python style&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
If the index is not required, iterate directly over the elements.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; value : values) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; value &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for value in values:&lt;br /&gt;
print(value) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The second Python version should normally be preferred.&lt;br /&gt;
&lt;br /&gt;
== 15. When both index and value are required ==&lt;br /&gt;
&lt;br /&gt;
C/C++ programmers frequently use an explicit index.&lt;br /&gt;
&lt;br /&gt;
Python provides &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;std::string&amp;gt; names = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; names.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; names[i]&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
names = [&amp;quot;Ana&amp;quot;, &amp;quot;Bob&amp;quot;, &amp;quot;Carol&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
for i, name in enumerate(names):&lt;br /&gt;
print(i, &amp;quot;:&amp;quot;, name) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This introduces another frequently used Python feature: &amp;#039;&amp;#039;&amp;#039;unpacking&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== 16. Lists ==&lt;br /&gt;
&lt;br /&gt;
The closest commonly used Python equivalent to &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.push_back(40);&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[0];&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values.size(); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
values.append(40)&lt;br /&gt;
&lt;br /&gt;
print(values[0])&lt;br /&gt;
print(len(values)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python lists can contain objects of different types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// A normal std::vector cannot&lt;br /&gt;
// contain unrelated types.&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
10, 20, 30&lt;br /&gt;
}; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [&lt;br /&gt;
10,&lt;br /&gt;
3.14,&lt;br /&gt;
&amp;quot;hello&amp;quot;,&lt;br /&gt;
True&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Although Python allows this, homogeneous collections are generally easier to understand and maintain when the elements conceptually represent the same kind of data.&lt;br /&gt;
&lt;br /&gt;
== 17. Negative indexing ==&lt;br /&gt;
&lt;br /&gt;
Python allows indexing from the end of a sequence.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {10, 20, 30, 40};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 1]; // 40&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 2]; // 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40]&lt;br /&gt;
&lt;br /&gt;
print(v[-1])  # 40&lt;br /&gt;
print(v[-2])  # 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 18. Slicing ==&lt;br /&gt;
&lt;br /&gt;
Python sequences support slicing directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {&lt;br /&gt;
    10, 20, 30, 40, 50&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; part(&lt;br /&gt;
v.begin() + 1,&lt;br /&gt;
v.begin() + 4&lt;br /&gt;
); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40, 50]&lt;br /&gt;
&lt;br /&gt;
part = v[1:4] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The result is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[20, 30, 40]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
General syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
sequence[start:stop:step]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// Requires an explicit loop or&lt;br /&gt;
// another algorithm.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [0, 1, 2, 3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
print(v[1:4])   # [1, 2, 3]&lt;br /&gt;
print(v[:3])    # [0, 1, 2]&lt;br /&gt;
print(v[3:])    # [3, 4, 5]&lt;br /&gt;
print(v[::2])   # [0, 2, 4]&lt;br /&gt;
print(v[::-1])  # [5, 4, 3, 2, 1, 0] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 19. Membership testing ==&lt;br /&gt;
&lt;br /&gt;
Checking whether an element exists in a container is commonly performed using &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
if (std::find(&lt;br /&gt;
values.begin(),&lt;br /&gt;
values.end(),&lt;br /&gt;
20&lt;br /&gt;
) != values.end()) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The opposite test uses &amp;lt;code&amp;gt;not in&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 100 not in values:&lt;br /&gt;
    print(&amp;quot;Not found&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 20. Dictionaries ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt; is similar to a C++ associative container such as &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades;&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10;&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; grades[&amp;quot;Ana&amp;quot;]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {}&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8&lt;br /&gt;
&lt;br /&gt;
print(grades[&amp;quot;Ana&amp;quot;]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A dictionary can also be initialized directly:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades = {&lt;br /&gt;
    {&amp;quot;Ana&amp;quot;, 10},&lt;br /&gt;
    {&amp;quot;Bob&amp;quot;, 8},&lt;br /&gt;
    {&amp;quot;Carol&amp;quot;, 9}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 10,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 8,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 9,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Iterating over keys and values:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; grade&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
    print(name, &amp;quot;:&amp;quot;, grade)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 21. Sets ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; represents a collection containing unique values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.insert(40);&lt;br /&gt;
&lt;br /&gt;
if (20 != values.end()) {&lt;br /&gt;
// conceptually test membership&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = {10, 20, 30}&lt;br /&gt;
&lt;br /&gt;
values.add(40)&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A more correct C++ membership test would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (values.find(20) != values.end()) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets are particularly useful when:&lt;br /&gt;
&lt;br /&gt;
* duplicate values should be removed;&lt;br /&gt;
* fast membership tests are required;&lt;br /&gt;
* mathematical set operations are useful.&lt;br /&gt;
&lt;br /&gt;
== 22. Tuples ==&lt;br /&gt;
&lt;br /&gt;
Tuples are similar to lists, but they are immutable.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::tuple&amp;lt;int, double, std::string&amp;gt; data {&lt;br /&gt;
    10,&lt;br /&gt;
    3.14,&lt;br /&gt;
    &amp;quot;test&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
data = (10, 3.14, &amp;quot;test&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python uses tuples extensively for returning or unpacking multiple values.&lt;br /&gt;
&lt;br /&gt;
== 23. Functions ==&lt;br /&gt;
&lt;br /&gt;
Functions are declared using &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    return a + b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int result = add(2, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def add(a, b):&lt;br /&gt;
return a + b&lt;br /&gt;
&lt;br /&gt;
result = add(2, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not require parameter or return types to be declared.&lt;br /&gt;
&lt;br /&gt;
However, Python supports optional &amp;#039;&amp;#039;&amp;#039;type hints&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
double average(&lt;br /&gt;
    double a,&lt;br /&gt;
    double b&lt;br /&gt;
) {&lt;br /&gt;
    return (a + b) / 2.0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(&lt;br /&gt;
    a: float,&lt;br /&gt;
    b: float&lt;br /&gt;
) -&amp;gt; float:&lt;br /&gt;
    return (a + b) / 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Type hints do not normally enforce types at runtime.&lt;br /&gt;
&lt;br /&gt;
They provide information for:&lt;br /&gt;
&lt;br /&gt;
* the programmer;&lt;br /&gt;
* IDEs;&lt;br /&gt;
* static analysis tools;&lt;br /&gt;
* documentation.&lt;br /&gt;
&lt;br /&gt;
== 24. Default parameters ==&lt;br /&gt;
&lt;br /&gt;
Both C++ and Python support default parameter values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void greet(&lt;br /&gt;
    const std::string&amp;amp; name = &amp;quot;World&amp;quot;&lt;br /&gt;
) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Hello, &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
greet();&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def greet(name=&amp;quot;World&amp;quot;):&lt;br /&gt;
print(f&amp;quot;Hello, {name}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
greet()&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 25. Named arguments ==&lt;br /&gt;
&lt;br /&gt;
Python allows function arguments to be specified by name.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void connect(&lt;br /&gt;
    std::string host,&lt;br /&gt;
    int port,&lt;br /&gt;
    bool secure&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
connect(&amp;quot;server.local&amp;quot;, 443, true); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def connect(host, port, secure):&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
connect(&lt;br /&gt;
host=&amp;quot;server.local&amp;quot;,&lt;br /&gt;
port=443,&lt;br /&gt;
secure=True&lt;br /&gt;
) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This can make calls with several parameters easier to understand.&lt;br /&gt;
&lt;br /&gt;
== 26. Returning multiple values ==&lt;br /&gt;
&lt;br /&gt;
In C++, multiple return values typically require a structure, tuple, pair or output parameters.&lt;br /&gt;
&lt;br /&gt;
Python commonly uses tuples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::pair&amp;lt;int, int&amp;gt; minmax(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    if (a &amp;lt; b)&lt;br /&gt;
        return {a, b};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return {b, a};&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
auto [minimum, maximum] = minmax(10, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def minmax(a, b):&lt;br /&gt;
if a &amp;lt; b:&lt;br /&gt;
return a, b&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return b, a&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
minimum, maximum = minmax(10, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is another example of tuple unpacking.&lt;br /&gt;
&lt;br /&gt;
== 27. Swapping variables ==&lt;br /&gt;
&lt;br /&gt;
A temporary variable is normally used in C.&lt;br /&gt;
&lt;br /&gt;
Python supports unpacking directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int temp = a;&lt;br /&gt;
a = b;&lt;br /&gt;
b = temp;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a, b = b, a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 28. Strings ==&lt;br /&gt;
&lt;br /&gt;
Python strings behave more like C++ &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt; objects than traditional C character arrays.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text.size();&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text[0]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(len(text))&lt;br /&gt;
print(text[0]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
String concatenation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string first = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
std::string second = &amp;quot;World&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::string result =&lt;br /&gt;
first + &amp;quot; &amp;quot; + second; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
first = &amp;quot;Hello&amp;quot;&lt;br /&gt;
second = &amp;quot;World&amp;quot;&lt;br /&gt;
&lt;br /&gt;
result = first + &amp;quot; &amp;quot; + second &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python strings provide many built-in operations:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Usually requires algorithms,&lt;br /&gt;
// explicit transformations,&lt;br /&gt;
// or utility functions. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(text.upper())&lt;br /&gt;
print(text.lower())&lt;br /&gt;
print(text.startswith(&amp;quot;he&amp;quot;))&lt;br /&gt;
print(text.endswith(&amp;quot;lo&amp;quot;))&lt;br /&gt;
print(text.replace(&amp;quot;l&amp;quot;, &amp;quot;x&amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 29. Lists versus arrays: important semantic difference ==&lt;br /&gt;
&lt;br /&gt;
Consider assigning one collection to another.&lt;br /&gt;
&lt;br /&gt;
With normal C++ value semantics, copying a vector produces another vector.&lt;br /&gt;
&lt;br /&gt;
Python assignment behaves differently.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&lt;br /&gt;
b[0] = 100;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a[0];  // 1&lt;br /&gt;
std::cout &amp;lt;&amp;lt; b[0];  // 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
b = a&lt;br /&gt;
&lt;br /&gt;
b[0] = 100&lt;br /&gt;
&lt;br /&gt;
print(a[0])  # 100&lt;br /&gt;
print(b[0])  # 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This happens because:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
does not copy the list.&lt;br /&gt;
&lt;br /&gt;
Both names refer to the &amp;#039;&amp;#039;&amp;#039;same list object&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Conceptually:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
       +-------------+&lt;br /&gt;
a ---&amp;gt; | [1, 2, 3]   |&lt;br /&gt;
       +-------------+&lt;br /&gt;
          ^&lt;br /&gt;
          |&lt;br /&gt;
b --------+&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To explicitly create a shallow copy:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a.copy()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = list(a)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This distinction is extremely important when using Python.&lt;br /&gt;
&lt;br /&gt;
== 30. Equality versus identity ==&lt;br /&gt;
&lt;br /&gt;
Python distinguishes between:&lt;br /&gt;
&lt;br /&gt;
* whether two objects contain equal values;&lt;br /&gt;
* whether two names refer to the exact same object.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; (a == b);  // true&lt;br /&gt;
&lt;br /&gt;
// Address comparison would be needed&lt;br /&gt;
// to determine object identity. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
b = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
print(a == b)  # True&lt;br /&gt;
print(a is b)  # False &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In Python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
==  tests value equality&lt;br /&gt;
is  tests object identity&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not use &amp;lt;code&amp;gt;is&amp;lt;/code&amp;gt; when you intend to compare values.&lt;br /&gt;
&lt;br /&gt;
A common correct use is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if value is None:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 31. None and nullptr ==&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; represents the absence of a value.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Object* ptr = nullptr;&lt;br /&gt;
&lt;br /&gt;
if (ptr == nullptr) {&lt;br /&gt;
...&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = None&lt;br /&gt;
&lt;br /&gt;
if value is None:&lt;br /&gt;
... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 32. Exceptions ==&lt;br /&gt;
&lt;br /&gt;
Exception handling is conceptually similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    int value = std::stoi(text);&lt;br /&gt;
}&lt;br /&gt;
catch (const std::exception&amp;amp; e) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Invalid value\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    value = int(text)&lt;br /&gt;
except ValueError:&lt;br /&gt;
    print(&amp;quot;Invalid value&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python generally uses exceptions rather than special error return values for many failure conditions.&lt;br /&gt;
&lt;br /&gt;
== 33. A first Pythonic transformation ==&lt;br /&gt;
&lt;br /&gt;
Consider creating a vector/list containing the squares of the numbers from 0 to 9.&lt;br /&gt;
&lt;br /&gt;
A direct translation from C++ could look like this:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — direct translation |&lt;br /&gt;
| ----------------------------- |&lt;br /&gt;
|                               |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = []&lt;br /&gt;
&lt;br /&gt;
for i in range(10):&lt;br /&gt;
squares.append(i * i) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python code is correct.&lt;br /&gt;
&lt;br /&gt;
However, Python provides a concise construct called a &amp;#039;&amp;#039;&amp;#039;list comprehension&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — idiomatic |&lt;br /&gt;
| -------------------- |&lt;br /&gt;
|                      |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A condition can also be included.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
if (i % 2 == 0) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
}&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
if i % 2 == 0&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
An important objective of this course is not only to write programs that work in Python, but to learn to write programs in a style appropriate for Python.&lt;br /&gt;
&lt;br /&gt;
== 34. Example: processing student grades ==&lt;br /&gt;
&lt;br /&gt;
Consider the following problem:&lt;br /&gt;
&lt;br /&gt;
* store the grades of several students;&lt;br /&gt;
* compute the average;&lt;br /&gt;
* print the students whose grade is greater than or equal to the average.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;unordered_map&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt; grades = {&lt;br /&gt;
{&amp;quot;Ana&amp;quot;, 9.5},&lt;br /&gt;
{&amp;quot;Bob&amp;quot;, 7.0},&lt;br /&gt;
{&amp;quot;Carol&amp;quot;, 8.5},&lt;br /&gt;
{&amp;quot;Dan&amp;quot;, 6.0}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
double sum = 0.0;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    sum += grade;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
double average = sum / grades.size();&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Average: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; average&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    if (grade &amp;gt;= average) {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
                  &amp;lt;&amp;lt; grade&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
&amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
&amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
&amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
&amp;quot;Dan&amp;quot;: 6.0,&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
average = sum(grades.values()) / len(grades)&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;Average: {average:.2f}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
if grade &amp;gt;= average:&lt;br /&gt;
print(f&amp;quot;{name}: {grade}&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that the Python version uses existing language functionality rather than manually reproducing every operation.&lt;br /&gt;
&lt;br /&gt;
In particular:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sum(grades.values())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
expresses the intention directly.&lt;br /&gt;
&lt;br /&gt;
== 35. Exercise 1 — Translation ==&lt;br /&gt;
&lt;br /&gt;
Translate the following C++ program into Python.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
12, 7, 18, 3, 24, 11&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
int sum = 0;&lt;br /&gt;
int count = 0;&lt;br /&gt;
&lt;br /&gt;
for (int value : values) {&lt;br /&gt;
    if (value % 2 == 0) {&lt;br /&gt;
        sum += value;&lt;br /&gt;
        count++;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Count: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; count&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Sum: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; sum&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Your implementation:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# TODO&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
After implementing a direct translation, try to simplify the Python implementation.&lt;br /&gt;
&lt;br /&gt;
== 36. Exercise 2 — Statistics ==&lt;br /&gt;
&lt;br /&gt;
Write a Python program that:&lt;br /&gt;
&lt;br /&gt;
# reads a sequence of integer values;&lt;br /&gt;
&lt;br /&gt;
# stores them in a list;&lt;br /&gt;
&lt;br /&gt;
# prints the minimum value;&lt;br /&gt;
&lt;br /&gt;
# prints the maximum value;&lt;br /&gt;
&lt;br /&gt;
# prints the average;&lt;br /&gt;
&lt;br /&gt;
# prints how many values are above the average.&lt;br /&gt;
&lt;br /&gt;
The equivalent C++ structure might begin as follows:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values;&lt;br /&gt;
&lt;br /&gt;
// Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
int minimum = ...;&lt;br /&gt;
int maximum = ...;&lt;br /&gt;
double average = ...; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = []&lt;br /&gt;
&lt;br /&gt;
# Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
minimum = ...&lt;br /&gt;
maximum = ...&lt;br /&gt;
average = ... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Useful Python functions include:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
len(...)&lt;br /&gt;
sum(...)&lt;br /&gt;
min(...)&lt;br /&gt;
max(...)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 37. Exercise 3 — Word frequency ==&lt;br /&gt;
&lt;br /&gt;
Given a sentence, count how many times each word occurs.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python is simple and python is powerful&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
should produce information equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python -&amp;gt; 2&lt;br /&gt;
is -&amp;gt; 2&lt;br /&gt;
simple -&amp;gt; 1&lt;br /&gt;
and -&amp;gt; 1&lt;br /&gt;
powerful -&amp;gt; 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A possible C++ data structure and its Python equivalent are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt;&lt;br /&gt;
    frequencies;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
frequencies = {}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sentence.split()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to separate the sentence into words.&lt;br /&gt;
&lt;br /&gt;
== 38. Exercise 4 — Remove duplicates ==&lt;br /&gt;
&lt;br /&gt;
Given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [4, 2, 7, 4, 2, 9, 7, 1]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
create a collection containing every distinct value.&lt;br /&gt;
&lt;br /&gt;
Think about the following C++ and Python data structures:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; unique_values;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
unique_values = set()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Try to solve the problem both:&lt;br /&gt;
&lt;br /&gt;
# using an explicit loop;&lt;br /&gt;
&lt;br /&gt;
# using Python&amp;#039;s built-in functionality.&lt;br /&gt;
&lt;br /&gt;
== 39. Exercise 5 — Small problem ==&lt;br /&gt;
&lt;br /&gt;
Write a program that receives a list of students and their grades and determines:&lt;br /&gt;
&lt;br /&gt;
* the class average;&lt;br /&gt;
* the student with the highest grade;&lt;br /&gt;
* all students who passed;&lt;br /&gt;
* all students whose grade is above the class average.&lt;br /&gt;
&lt;br /&gt;
Suggested representation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt;&lt;br /&gt;
    grades;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
    &amp;quot;Dan&amp;quot;: 4.0,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Avoid using numeric indices unless an index is actually required.&lt;br /&gt;
&lt;br /&gt;
== 40. Creating a project environment ==&lt;br /&gt;
&lt;br /&gt;
Python applications commonly use a &amp;#039;&amp;#039;&amp;#039;virtual environment&amp;#039;&amp;#039;&amp;#039; to isolate project dependencies.&lt;br /&gt;
&lt;br /&gt;
Create a directory for the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir python-project&lt;br /&gt;
cd python-project&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a virtual environment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 -m venv .venv&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Linux/macOS:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source .venv/bin/activate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Windows PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
.venv\Scripts\Activate.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command prompt will normally indicate that the environment is active.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
(.venv) student@computer:~/python-project$&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Packages installed using &amp;lt;code&amp;gt;pip&amp;lt;/code&amp;gt; will now be installed inside this environment.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip install requests&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To inspect installed packages:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip list&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 41. Recommended initial project structure ==&lt;br /&gt;
&lt;br /&gt;
For the first laboratories, the following structure is sufficient:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python-project/&lt;br /&gt;
├── .venv/&lt;br /&gt;
├── main.py&lt;br /&gt;
├── README.md&lt;br /&gt;
└── .gitignore&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A basic &amp;lt;code&amp;gt;.gitignore&amp;lt;/code&amp;gt; should contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.venv/&lt;br /&gt;
__pycache__/&lt;br /&gt;
*.pyc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; commit the virtual environment to Git.&lt;br /&gt;
&lt;br /&gt;
== 42. C/C++ habits to reconsider in Python ==&lt;br /&gt;
&lt;br /&gt;
When transitioning from C/C++ to Python, the following patterns should generally be reconsidered.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ habit&lt;br /&gt;
&lt;br /&gt;
| ! Typical Python approach                |&lt;br /&gt;
| ---------------------------------------- |&lt;br /&gt;
| Iterate using an integer index           |&lt;br /&gt;
| Iterate directly over the objects        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually maintain counters               |&lt;br /&gt;
| Consider &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Search manually through a container      |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually calculate container size        |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;len()&amp;lt;/code&amp;gt;                   |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit temporary variable for swapping |&lt;br /&gt;
| Use tuple unpacking                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit loop for simple transformations |&lt;br /&gt;
| Consider a comprehension                 |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Long output expressions                  |&lt;br /&gt;
| Use f-strings                            |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Copy assumed after assignment            |&lt;br /&gt;
| Remember that names refer to objects     |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Check null using equality                |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;is None&amp;lt;/code&amp;gt;                 |&lt;br /&gt;
| }                                        |&lt;br /&gt;
&lt;br /&gt;
The objective is not simply to translate C/C++ syntax into Python syntax.&lt;br /&gt;
&lt;br /&gt;
Consider:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++ style translated literally&lt;br /&gt;
&lt;br /&gt;
| ! More idiomatic Python |&lt;br /&gt;
| ----------------------- |&lt;br /&gt;
|                         |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
    print(values[i])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for value in values:&lt;br /&gt;
    print(value)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Both programs are correct.&lt;br /&gt;
&lt;br /&gt;
The second expresses the programmer&amp;#039;s intention more directly.&lt;br /&gt;
&lt;br /&gt;
== 43. Summary ==&lt;br /&gt;
&lt;br /&gt;
The main mappings introduced in this laboratory are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python                                    |   |         |&lt;br /&gt;
| ------------------------------------------- | - | ------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;{ ... }&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| indentation                                 |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;                                      |   | &amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;                              |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt;                    |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_set&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::tuple&amp;lt;/code&amp;gt;                     |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;tuple&amp;lt;/code&amp;gt;                          |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt;                      |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cin&amp;lt;/code&amp;gt;                       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| indexed iteration                           |   |         |&lt;br /&gt;
| direct iteration / &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| manual search                               |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::pow(a, b)&amp;lt;/code&amp;gt;                 |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;a ** b&amp;lt;/code&amp;gt;                         |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| integer division                            |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| }                                           |   |         |&lt;br /&gt;
&lt;br /&gt;
The central idea to retain from this laboratory is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Python should not be treated merely as C++ with different syntax. Python provides its own abstractions and idioms, and using them generally produces simpler and more readable programs.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 44. Preparation for Lab 2 ==&lt;br /&gt;
&lt;br /&gt;
Before the next laboratory:&lt;br /&gt;
&lt;br /&gt;
# complete all exercises from this laboratory;&lt;br /&gt;
&lt;br /&gt;
# ensure Python 3 is installed;&lt;br /&gt;
&lt;br /&gt;
# create a virtual environment successfully;&lt;br /&gt;
&lt;br /&gt;
# create a Git repository for the semester project;&lt;br /&gt;
&lt;br /&gt;
# become familiar with lists, dictionaries, tuples and sets;&lt;br /&gt;
&lt;br /&gt;
# review the concepts of classes, objects, inheritance and composition from C++.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Lab 2&amp;#039;&amp;#039;&amp;#039;, these concepts will be revisited from the perspective of Python&amp;#039;s object model and Pythonic object-oriented programming.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8361</id>
		<title>Lab 1</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8361"/>
		<updated>2026-09-14T05:27:12Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* 4. Constants */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 1 — Python for C/C++ Programmers =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
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++.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
After completing this laboratory, you should be able to:&lt;br /&gt;
&lt;br /&gt;
* understand the basic syntax of Python;&lt;br /&gt;
* recognize the main differences between Python and C/C++;&lt;br /&gt;
* work with Python&amp;#039;s fundamental data types;&lt;br /&gt;
* use conditional statements and loops;&lt;br /&gt;
* work with lists, dictionaries, sets and tuples;&lt;br /&gt;
* define and call functions;&lt;br /&gt;
* understand basic Python object/reference semantics;&lt;br /&gt;
* recognize several common Python idioms;&lt;br /&gt;
* create and execute a simple Python project.&lt;br /&gt;
&lt;br /&gt;
Throughout this laboratory, equivalent or similar C/C++ and Python code will be presented side by side.&lt;br /&gt;
&lt;br /&gt;
**TOC**&lt;br /&gt;
&lt;br /&gt;
== 1. Running a Python program ==&lt;br /&gt;
&lt;br /&gt;
A C/C++ program normally has to be compiled before it is executed. Python programs are usually executed directly by the Python interpreter.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Hello, world!\n&amp;quot;;&lt;br /&gt;
return 0;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(&amp;quot;Hello, world!&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A Python source file normally uses the extension:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be executed using:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unlike C/C++, Python does not require a special &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
However, larger Python programs commonly use:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Program started\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
print(&amp;quot;Program started&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
if **name** == &amp;quot;**main**&amp;quot;:&lt;br /&gt;
main() &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python version is not mandatory, but this pattern will become useful when programs are split into multiple modules.&lt;br /&gt;
&lt;br /&gt;
== 2. Blocks and indentation ==&lt;br /&gt;
&lt;br /&gt;
In C and C++, braces delimit blocks of code.&lt;br /&gt;
&lt;br /&gt;
Python uses &amp;#039;&amp;#039;&amp;#039;indentation&amp;#039;&amp;#039;&amp;#039; instead.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (x &amp;gt; 10) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Large\n&amp;quot;;&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Value: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
    print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
    print(&amp;quot;Value:&amp;quot;, x)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Indentation in Python is part of the language syntax.&lt;br /&gt;
&lt;br /&gt;
The standard convention is &amp;#039;&amp;#039;&amp;#039;4 spaces per indentation level&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
For example, the following code is incorrect:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python will report an &amp;lt;code&amp;gt;IndentationError&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 3. Variables and types ==&lt;br /&gt;
&lt;br /&gt;
C/C++ variables have a declared static type.&lt;br /&gt;
&lt;br /&gt;
Python variables do not require explicit type declarations.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int count = 10;&lt;br /&gt;
double temperature = 23.5;&lt;br /&gt;
bool enabled = true;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
count = 10&lt;br /&gt;
temperature = 23.5&lt;br /&gt;
enabled = True&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python is &amp;#039;&amp;#039;&amp;#039;dynamically typed&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
This means that a variable name can later refer to an object of another type.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
// Not allowed:&lt;br /&gt;
// value = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
value = &amp;quot;hello&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This does &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; mean that Python has no types.&lt;br /&gt;
&lt;br /&gt;
Objects still have types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;typeinfo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; typeid(value).name();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
&lt;br /&gt;
print(type(value))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python will produce:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common basic types ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ type&lt;br /&gt;
! Python equivalent&lt;br /&gt;
! Example&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;42&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt; / &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;3.14&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt;&lt;br /&gt;
| No separate character type&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;A&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;str&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;Hello&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 4. Constants ==&lt;br /&gt;
&lt;br /&gt;
C++ can enforce that a value cannot be modified using &amp;lt;code&amp;gt;const&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python does not enforce constants at language level.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
const double PI = 3.1415926535;&lt;br /&gt;
const int MAX_USERS = 100; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
PI = 3.1415926535&lt;br /&gt;
MAX_USERS = 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
By convention, Python names that should be treated as constants are written using uppercase letters.&lt;br /&gt;
&lt;br /&gt;
Nothing prevents the programmer from changing them.&lt;br /&gt;
&lt;br /&gt;
== 5. Arithmetic operators ==&lt;br /&gt;
&lt;br /&gt;
Most arithmetic operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
int sum  = a + b;&lt;br /&gt;
int diff = a - b;&lt;br /&gt;
int prod = a * b;&lt;br /&gt;
int div  = a / b;&lt;br /&gt;
int rem  = a % b; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
sum_ = a + b&lt;br /&gt;
diff = a - b&lt;br /&gt;
prod = a * b&lt;br /&gt;
div = a // b&lt;br /&gt;
rem = a % b &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
There is an important difference between &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a / b;   // 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
print(a / b)   # 3.3333333333333335&lt;br /&gt;
print(a // b)  # 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python also provides an exponentiation operator:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;cmath&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double result = std::pow(2, 8); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
result = 2 ** 8 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 6. Printing values ==&lt;br /&gt;
&lt;br /&gt;
Output using &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt; is replaced by the built-in &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
int age = 21;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; is &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; age&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; years old\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
age = 21&lt;br /&gt;
&lt;br /&gt;
print(name, &amp;quot;is&amp;quot;, age, &amp;quot;years old&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python also provides formatted string literals, commonly called &amp;#039;&amp;#039;&amp;#039;f-strings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Temperature: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; temperature&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot; C\n&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Temperature: {temperature} C&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Expressions can appear directly inside an f-string:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Result: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; a + b&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Result: {a + b}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 7. Reading input ==&lt;br /&gt;
&lt;br /&gt;
Console input is performed using &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An important difference is that &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt; &amp;#039;&amp;#039;&amp;#039;always returns a string&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int age;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Age: &amp;quot;;&lt;br /&gt;
std::cin &amp;gt;&amp;gt; age; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = int(input(&amp;quot;Age: &amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Without the conversion:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = input(&amp;quot;Age: &amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; is a string.&lt;br /&gt;
&lt;br /&gt;
Conversions are explicit:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = std::stoi(text);&lt;br /&gt;
double b = std::stod(text);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = int(text)&lt;br /&gt;
b = float(text)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 8. Conditional statements ==&lt;br /&gt;
&lt;br /&gt;
The overall structure is similar, but Python does not use parentheses or braces.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (temperature &amp;gt; 30) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Hot\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else if (temperature &amp;gt; 20) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Warm\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Cold\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if temperature &amp;gt; 30:&lt;br /&gt;
    print(&amp;quot;Hot&amp;quot;)&lt;br /&gt;
elif temperature &amp;gt; 20:&lt;br /&gt;
    print(&amp;quot;Warm&amp;quot;)&lt;br /&gt;
else:&lt;br /&gt;
    print(&amp;quot;Cold&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python uses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
elif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
instead of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
else if&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 9. Boolean operators ==&lt;br /&gt;
&lt;br /&gt;
Python uses words instead of the C/C++ symbolic logical operators.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python         |   |         |&lt;br /&gt;
| ---------------- | - | ------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt;  |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| -                |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;           |   | &amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;  |   |         |&lt;br /&gt;
| -                |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;   |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| }                |   |         |&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (age &amp;gt;= 18 &amp;amp;&amp;amp; enabled) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if (!enabled) {&lt;br /&gt;
...&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if age &amp;gt;= 18 and enabled:&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
if not enabled:&lt;br /&gt;
... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 10. Comparison operators ==&lt;br /&gt;
&lt;br /&gt;
Most comparison operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Operation&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python              |&lt;br /&gt;
| --------------------- |&lt;br /&gt;
| equality              |&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| inequality            |&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| less than             |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                     |&lt;br /&gt;
| greater than          |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                     |&lt;br /&gt;
| less than or equal    |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| greater than or equal |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| }                     |&lt;br /&gt;
&lt;br /&gt;
Python also allows chained comparisons.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (0 &amp;lt;= x &amp;amp;&amp;amp; x &amp;lt; 100) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 0 &amp;lt;= x &amp;lt; 100:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 11. The ternary operator ==&lt;br /&gt;
&lt;br /&gt;
Both languages support conditional expressions.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int maximum = (a &amp;gt; b) ? a : b;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
maximum = a if a &amp;gt; b else b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python&amp;#039;s order is different:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
value_if_true if condition else value_if_false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 12. While loops ==&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;code&amp;gt;while&amp;lt;/code&amp;gt; loop is very similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int i = 0;&lt;br /&gt;
&lt;br /&gt;
while (i &amp;lt; 5) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
i++;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
&lt;br /&gt;
while i &amp;lt; 5:&lt;br /&gt;
print(i)&lt;br /&gt;
i += 1 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not have &amp;lt;code&amp;gt;++&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;--&amp;lt;/code&amp;gt; operators.&lt;br /&gt;
&lt;br /&gt;
Therefore:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i += 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is used instead.&lt;br /&gt;
&lt;br /&gt;
== 13. For loops ==&lt;br /&gt;
&lt;br /&gt;
This is one of the first important conceptual differences.&lt;br /&gt;
&lt;br /&gt;
In C/C++, a &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement commonly controls a numeric index.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement iterates over objects.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 5; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;range(5)&amp;lt;/code&amp;gt; generates values conceptually equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
0, 1, 2, 3, 4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A different starting value can be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 2; i &amp;lt; 10; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(2, 10):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A step can also be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i += 2) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(0, 10, 2):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 14. Iterating over containers ==&lt;br /&gt;
&lt;br /&gt;
Suppose we want to print every element of a vector/list.&lt;br /&gt;
&lt;br /&gt;
An index-based implementation is possible:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; values.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[i] &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
print(values[i]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
However, this is generally &amp;#039;&amp;#039;&amp;#039;not the preferred Python style&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
If the index is not required, iterate directly over the elements.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; value : values) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; value &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for value in values:&lt;br /&gt;
print(value) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The second Python version should normally be preferred.&lt;br /&gt;
&lt;br /&gt;
== 15. When both index and value are required ==&lt;br /&gt;
&lt;br /&gt;
C/C++ programmers frequently use an explicit index.&lt;br /&gt;
&lt;br /&gt;
Python provides &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;std::string&amp;gt; names = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; names.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; names[i]&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
names = [&amp;quot;Ana&amp;quot;, &amp;quot;Bob&amp;quot;, &amp;quot;Carol&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
for i, name in enumerate(names):&lt;br /&gt;
print(i, &amp;quot;:&amp;quot;, name) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This introduces another frequently used Python feature: &amp;#039;&amp;#039;&amp;#039;unpacking&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== 16. Lists ==&lt;br /&gt;
&lt;br /&gt;
The closest commonly used Python equivalent to &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.push_back(40);&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[0];&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values.size(); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
values.append(40)&lt;br /&gt;
&lt;br /&gt;
print(values[0])&lt;br /&gt;
print(len(values)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python lists can contain objects of different types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// A normal std::vector cannot&lt;br /&gt;
// contain unrelated types.&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
10, 20, 30&lt;br /&gt;
}; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [&lt;br /&gt;
10,&lt;br /&gt;
3.14,&lt;br /&gt;
&amp;quot;hello&amp;quot;,&lt;br /&gt;
True&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Although Python allows this, homogeneous collections are generally easier to understand and maintain when the elements conceptually represent the same kind of data.&lt;br /&gt;
&lt;br /&gt;
== 17. Negative indexing ==&lt;br /&gt;
&lt;br /&gt;
Python allows indexing from the end of a sequence.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {10, 20, 30, 40};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 1]; // 40&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 2]; // 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40]&lt;br /&gt;
&lt;br /&gt;
print(v[-1])  # 40&lt;br /&gt;
print(v[-2])  # 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 18. Slicing ==&lt;br /&gt;
&lt;br /&gt;
Python sequences support slicing directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {&lt;br /&gt;
    10, 20, 30, 40, 50&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; part(&lt;br /&gt;
v.begin() + 1,&lt;br /&gt;
v.begin() + 4&lt;br /&gt;
); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40, 50]&lt;br /&gt;
&lt;br /&gt;
part = v[1:4] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The result is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[20, 30, 40]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
General syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
sequence[start:stop:step]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// Requires an explicit loop or&lt;br /&gt;
// another algorithm.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [0, 1, 2, 3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
print(v[1:4])   # [1, 2, 3]&lt;br /&gt;
print(v[:3])    # [0, 1, 2]&lt;br /&gt;
print(v[3:])    # [3, 4, 5]&lt;br /&gt;
print(v[::2])   # [0, 2, 4]&lt;br /&gt;
print(v[::-1])  # [5, 4, 3, 2, 1, 0] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 19. Membership testing ==&lt;br /&gt;
&lt;br /&gt;
Checking whether an element exists in a container is commonly performed using &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
if (std::find(&lt;br /&gt;
values.begin(),&lt;br /&gt;
values.end(),&lt;br /&gt;
20&lt;br /&gt;
) != values.end()) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The opposite test uses &amp;lt;code&amp;gt;not in&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 100 not in values:&lt;br /&gt;
    print(&amp;quot;Not found&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 20. Dictionaries ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt; is similar to a C++ associative container such as &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades;&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10;&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; grades[&amp;quot;Ana&amp;quot;]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {}&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8&lt;br /&gt;
&lt;br /&gt;
print(grades[&amp;quot;Ana&amp;quot;]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A dictionary can also be initialized directly:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades = {&lt;br /&gt;
    {&amp;quot;Ana&amp;quot;, 10},&lt;br /&gt;
    {&amp;quot;Bob&amp;quot;, 8},&lt;br /&gt;
    {&amp;quot;Carol&amp;quot;, 9}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 10,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 8,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 9,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Iterating over keys and values:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; grade&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
    print(name, &amp;quot;:&amp;quot;, grade)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 21. Sets ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; represents a collection containing unique values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.insert(40);&lt;br /&gt;
&lt;br /&gt;
if (20 != values.end()) {&lt;br /&gt;
// conceptually test membership&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = {10, 20, 30}&lt;br /&gt;
&lt;br /&gt;
values.add(40)&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A more correct C++ membership test would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (values.find(20) != values.end()) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets are particularly useful when:&lt;br /&gt;
&lt;br /&gt;
* duplicate values should be removed;&lt;br /&gt;
* fast membership tests are required;&lt;br /&gt;
* mathematical set operations are useful.&lt;br /&gt;
&lt;br /&gt;
== 22. Tuples ==&lt;br /&gt;
&lt;br /&gt;
Tuples are similar to lists, but they are immutable.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::tuple&amp;lt;int, double, std::string&amp;gt; data {&lt;br /&gt;
    10,&lt;br /&gt;
    3.14,&lt;br /&gt;
    &amp;quot;test&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
data = (10, 3.14, &amp;quot;test&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python uses tuples extensively for returning or unpacking multiple values.&lt;br /&gt;
&lt;br /&gt;
== 23. Functions ==&lt;br /&gt;
&lt;br /&gt;
Functions are declared using &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    return a + b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int result = add(2, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def add(a, b):&lt;br /&gt;
return a + b&lt;br /&gt;
&lt;br /&gt;
result = add(2, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not require parameter or return types to be declared.&lt;br /&gt;
&lt;br /&gt;
However, Python supports optional &amp;#039;&amp;#039;&amp;#039;type hints&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
double average(&lt;br /&gt;
    double a,&lt;br /&gt;
    double b&lt;br /&gt;
) {&lt;br /&gt;
    return (a + b) / 2.0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(&lt;br /&gt;
    a: float,&lt;br /&gt;
    b: float&lt;br /&gt;
) -&amp;gt; float:&lt;br /&gt;
    return (a + b) / 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Type hints do not normally enforce types at runtime.&lt;br /&gt;
&lt;br /&gt;
They provide information for:&lt;br /&gt;
&lt;br /&gt;
* the programmer;&lt;br /&gt;
* IDEs;&lt;br /&gt;
* static analysis tools;&lt;br /&gt;
* documentation.&lt;br /&gt;
&lt;br /&gt;
== 24. Default parameters ==&lt;br /&gt;
&lt;br /&gt;
Both C++ and Python support default parameter values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void greet(&lt;br /&gt;
    const std::string&amp;amp; name = &amp;quot;World&amp;quot;&lt;br /&gt;
) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Hello, &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
greet();&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def greet(name=&amp;quot;World&amp;quot;):&lt;br /&gt;
print(f&amp;quot;Hello, {name}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
greet()&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 25. Named arguments ==&lt;br /&gt;
&lt;br /&gt;
Python allows function arguments to be specified by name.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void connect(&lt;br /&gt;
    std::string host,&lt;br /&gt;
    int port,&lt;br /&gt;
    bool secure&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
connect(&amp;quot;server.local&amp;quot;, 443, true); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def connect(host, port, secure):&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
connect(&lt;br /&gt;
host=&amp;quot;server.local&amp;quot;,&lt;br /&gt;
port=443,&lt;br /&gt;
secure=True&lt;br /&gt;
) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This can make calls with several parameters easier to understand.&lt;br /&gt;
&lt;br /&gt;
== 26. Returning multiple values ==&lt;br /&gt;
&lt;br /&gt;
In C++, multiple return values typically require a structure, tuple, pair or output parameters.&lt;br /&gt;
&lt;br /&gt;
Python commonly uses tuples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::pair&amp;lt;int, int&amp;gt; minmax(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    if (a &amp;lt; b)&lt;br /&gt;
        return {a, b};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return {b, a};&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
auto [minimum, maximum] = minmax(10, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def minmax(a, b):&lt;br /&gt;
if a &amp;lt; b:&lt;br /&gt;
return a, b&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return b, a&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
minimum, maximum = minmax(10, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is another example of tuple unpacking.&lt;br /&gt;
&lt;br /&gt;
== 27. Swapping variables ==&lt;br /&gt;
&lt;br /&gt;
A temporary variable is normally used in C.&lt;br /&gt;
&lt;br /&gt;
Python supports unpacking directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int temp = a;&lt;br /&gt;
a = b;&lt;br /&gt;
b = temp;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a, b = b, a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 28. Strings ==&lt;br /&gt;
&lt;br /&gt;
Python strings behave more like C++ &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt; objects than traditional C character arrays.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text.size();&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text[0]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(len(text))&lt;br /&gt;
print(text[0]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
String concatenation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string first = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
std::string second = &amp;quot;World&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::string result =&lt;br /&gt;
first + &amp;quot; &amp;quot; + second; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
first = &amp;quot;Hello&amp;quot;&lt;br /&gt;
second = &amp;quot;World&amp;quot;&lt;br /&gt;
&lt;br /&gt;
result = first + &amp;quot; &amp;quot; + second &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python strings provide many built-in operations:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Usually requires algorithms,&lt;br /&gt;
// explicit transformations,&lt;br /&gt;
// or utility functions. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(text.upper())&lt;br /&gt;
print(text.lower())&lt;br /&gt;
print(text.startswith(&amp;quot;he&amp;quot;))&lt;br /&gt;
print(text.endswith(&amp;quot;lo&amp;quot;))&lt;br /&gt;
print(text.replace(&amp;quot;l&amp;quot;, &amp;quot;x&amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 29. Lists versus arrays: important semantic difference ==&lt;br /&gt;
&lt;br /&gt;
Consider assigning one collection to another.&lt;br /&gt;
&lt;br /&gt;
With normal C++ value semantics, copying a vector produces another vector.&lt;br /&gt;
&lt;br /&gt;
Python assignment behaves differently.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&lt;br /&gt;
b[0] = 100;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a[0];  // 1&lt;br /&gt;
std::cout &amp;lt;&amp;lt; b[0];  // 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
b = a&lt;br /&gt;
&lt;br /&gt;
b[0] = 100&lt;br /&gt;
&lt;br /&gt;
print(a[0])  # 100&lt;br /&gt;
print(b[0])  # 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This happens because:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
does not copy the list.&lt;br /&gt;
&lt;br /&gt;
Both names refer to the &amp;#039;&amp;#039;&amp;#039;same list object&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Conceptually:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
       +-------------+&lt;br /&gt;
a ---&amp;gt; | [1, 2, 3]   |&lt;br /&gt;
       +-------------+&lt;br /&gt;
          ^&lt;br /&gt;
          |&lt;br /&gt;
b --------+&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To explicitly create a shallow copy:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a.copy()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = list(a)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This distinction is extremely important when using Python.&lt;br /&gt;
&lt;br /&gt;
== 30. Equality versus identity ==&lt;br /&gt;
&lt;br /&gt;
Python distinguishes between:&lt;br /&gt;
&lt;br /&gt;
* whether two objects contain equal values;&lt;br /&gt;
* whether two names refer to the exact same object.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; (a == b);  // true&lt;br /&gt;
&lt;br /&gt;
// Address comparison would be needed&lt;br /&gt;
// to determine object identity. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
b = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
print(a == b)  # True&lt;br /&gt;
print(a is b)  # False &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In Python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
==  tests value equality&lt;br /&gt;
is  tests object identity&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not use &amp;lt;code&amp;gt;is&amp;lt;/code&amp;gt; when you intend to compare values.&lt;br /&gt;
&lt;br /&gt;
A common correct use is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if value is None:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 31. None and nullptr ==&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; represents the absence of a value.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Object* ptr = nullptr;&lt;br /&gt;
&lt;br /&gt;
if (ptr == nullptr) {&lt;br /&gt;
...&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = None&lt;br /&gt;
&lt;br /&gt;
if value is None:&lt;br /&gt;
... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 32. Exceptions ==&lt;br /&gt;
&lt;br /&gt;
Exception handling is conceptually similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    int value = std::stoi(text);&lt;br /&gt;
}&lt;br /&gt;
catch (const std::exception&amp;amp; e) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Invalid value\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    value = int(text)&lt;br /&gt;
except ValueError:&lt;br /&gt;
    print(&amp;quot;Invalid value&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python generally uses exceptions rather than special error return values for many failure conditions.&lt;br /&gt;
&lt;br /&gt;
== 33. A first Pythonic transformation ==&lt;br /&gt;
&lt;br /&gt;
Consider creating a vector/list containing the squares of the numbers from 0 to 9.&lt;br /&gt;
&lt;br /&gt;
A direct translation from C++ could look like this:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — direct translation |&lt;br /&gt;
| ----------------------------- |&lt;br /&gt;
|                               |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = []&lt;br /&gt;
&lt;br /&gt;
for i in range(10):&lt;br /&gt;
squares.append(i * i) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python code is correct.&lt;br /&gt;
&lt;br /&gt;
However, Python provides a concise construct called a &amp;#039;&amp;#039;&amp;#039;list comprehension&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — idiomatic |&lt;br /&gt;
| -------------------- |&lt;br /&gt;
|                      |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A condition can also be included.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
if (i % 2 == 0) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
}&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
if i % 2 == 0&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
An important objective of this course is not only to write programs that work in Python, but to learn to write programs in a style appropriate for Python.&lt;br /&gt;
&lt;br /&gt;
== 34. Example: processing student grades ==&lt;br /&gt;
&lt;br /&gt;
Consider the following problem:&lt;br /&gt;
&lt;br /&gt;
* store the grades of several students;&lt;br /&gt;
* compute the average;&lt;br /&gt;
* print the students whose grade is greater than or equal to the average.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;unordered_map&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt; grades = {&lt;br /&gt;
{&amp;quot;Ana&amp;quot;, 9.5},&lt;br /&gt;
{&amp;quot;Bob&amp;quot;, 7.0},&lt;br /&gt;
{&amp;quot;Carol&amp;quot;, 8.5},&lt;br /&gt;
{&amp;quot;Dan&amp;quot;, 6.0}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
double sum = 0.0;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    sum += grade;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
double average = sum / grades.size();&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Average: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; average&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    if (grade &amp;gt;= average) {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
                  &amp;lt;&amp;lt; grade&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
&amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
&amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
&amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
&amp;quot;Dan&amp;quot;: 6.0,&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
average = sum(grades.values()) / len(grades)&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;Average: {average:.2f}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
if grade &amp;gt;= average:&lt;br /&gt;
print(f&amp;quot;{name}: {grade}&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that the Python version uses existing language functionality rather than manually reproducing every operation.&lt;br /&gt;
&lt;br /&gt;
In particular:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sum(grades.values())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
expresses the intention directly.&lt;br /&gt;
&lt;br /&gt;
== 35. Exercise 1 — Translation ==&lt;br /&gt;
&lt;br /&gt;
Translate the following C++ program into Python.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
12, 7, 18, 3, 24, 11&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
int sum = 0;&lt;br /&gt;
int count = 0;&lt;br /&gt;
&lt;br /&gt;
for (int value : values) {&lt;br /&gt;
    if (value % 2 == 0) {&lt;br /&gt;
        sum += value;&lt;br /&gt;
        count++;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Count: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; count&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Sum: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; sum&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Your implementation:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# TODO&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
After implementing a direct translation, try to simplify the Python implementation.&lt;br /&gt;
&lt;br /&gt;
== 36. Exercise 2 — Statistics ==&lt;br /&gt;
&lt;br /&gt;
Write a Python program that:&lt;br /&gt;
&lt;br /&gt;
# reads a sequence of integer values;&lt;br /&gt;
&lt;br /&gt;
# stores them in a list;&lt;br /&gt;
&lt;br /&gt;
# prints the minimum value;&lt;br /&gt;
&lt;br /&gt;
# prints the maximum value;&lt;br /&gt;
&lt;br /&gt;
# prints the average;&lt;br /&gt;
&lt;br /&gt;
# prints how many values are above the average.&lt;br /&gt;
&lt;br /&gt;
The equivalent C++ structure might begin as follows:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values;&lt;br /&gt;
&lt;br /&gt;
// Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
int minimum = ...;&lt;br /&gt;
int maximum = ...;&lt;br /&gt;
double average = ...; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = []&lt;br /&gt;
&lt;br /&gt;
# Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
minimum = ...&lt;br /&gt;
maximum = ...&lt;br /&gt;
average = ... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Useful Python functions include:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
len(...)&lt;br /&gt;
sum(...)&lt;br /&gt;
min(...)&lt;br /&gt;
max(...)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 37. Exercise 3 — Word frequency ==&lt;br /&gt;
&lt;br /&gt;
Given a sentence, count how many times each word occurs.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python is simple and python is powerful&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
should produce information equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python -&amp;gt; 2&lt;br /&gt;
is -&amp;gt; 2&lt;br /&gt;
simple -&amp;gt; 1&lt;br /&gt;
and -&amp;gt; 1&lt;br /&gt;
powerful -&amp;gt; 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A possible C++ data structure and its Python equivalent are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt;&lt;br /&gt;
    frequencies;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
frequencies = {}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sentence.split()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to separate the sentence into words.&lt;br /&gt;
&lt;br /&gt;
== 38. Exercise 4 — Remove duplicates ==&lt;br /&gt;
&lt;br /&gt;
Given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [4, 2, 7, 4, 2, 9, 7, 1]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
create a collection containing every distinct value.&lt;br /&gt;
&lt;br /&gt;
Think about the following C++ and Python data structures:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; unique_values;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
unique_values = set()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Try to solve the problem both:&lt;br /&gt;
&lt;br /&gt;
# using an explicit loop;&lt;br /&gt;
&lt;br /&gt;
# using Python&amp;#039;s built-in functionality.&lt;br /&gt;
&lt;br /&gt;
== 39. Exercise 5 — Small problem ==&lt;br /&gt;
&lt;br /&gt;
Write a program that receives a list of students and their grades and determines:&lt;br /&gt;
&lt;br /&gt;
* the class average;&lt;br /&gt;
* the student with the highest grade;&lt;br /&gt;
* all students who passed;&lt;br /&gt;
* all students whose grade is above the class average.&lt;br /&gt;
&lt;br /&gt;
Suggested representation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt;&lt;br /&gt;
    grades;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
    &amp;quot;Dan&amp;quot;: 4.0,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Avoid using numeric indices unless an index is actually required.&lt;br /&gt;
&lt;br /&gt;
== 40. Creating a project environment ==&lt;br /&gt;
&lt;br /&gt;
Python applications commonly use a &amp;#039;&amp;#039;&amp;#039;virtual environment&amp;#039;&amp;#039;&amp;#039; to isolate project dependencies.&lt;br /&gt;
&lt;br /&gt;
Create a directory for the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir python-project&lt;br /&gt;
cd python-project&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a virtual environment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 -m venv .venv&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Linux/macOS:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source .venv/bin/activate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Windows PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
.venv\Scripts\Activate.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command prompt will normally indicate that the environment is active.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
(.venv) student@computer:~/python-project$&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Packages installed using &amp;lt;code&amp;gt;pip&amp;lt;/code&amp;gt; will now be installed inside this environment.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip install requests&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To inspect installed packages:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip list&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 41. Recommended initial project structure ==&lt;br /&gt;
&lt;br /&gt;
For the first laboratories, the following structure is sufficient:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python-project/&lt;br /&gt;
├── .venv/&lt;br /&gt;
├── main.py&lt;br /&gt;
├── README.md&lt;br /&gt;
└── .gitignore&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A basic &amp;lt;code&amp;gt;.gitignore&amp;lt;/code&amp;gt; should contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.venv/&lt;br /&gt;
__pycache__/&lt;br /&gt;
*.pyc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; commit the virtual environment to Git.&lt;br /&gt;
&lt;br /&gt;
== 42. C/C++ habits to reconsider in Python ==&lt;br /&gt;
&lt;br /&gt;
When transitioning from C/C++ to Python, the following patterns should generally be reconsidered.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ habit&lt;br /&gt;
&lt;br /&gt;
| ! Typical Python approach                |&lt;br /&gt;
| ---------------------------------------- |&lt;br /&gt;
| Iterate using an integer index           |&lt;br /&gt;
| Iterate directly over the objects        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually maintain counters               |&lt;br /&gt;
| Consider &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Search manually through a container      |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually calculate container size        |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;len()&amp;lt;/code&amp;gt;                   |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit temporary variable for swapping |&lt;br /&gt;
| Use tuple unpacking                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit loop for simple transformations |&lt;br /&gt;
| Consider a comprehension                 |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Long output expressions                  |&lt;br /&gt;
| Use f-strings                            |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Copy assumed after assignment            |&lt;br /&gt;
| Remember that names refer to objects     |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Check null using equality                |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;is None&amp;lt;/code&amp;gt;                 |&lt;br /&gt;
| }                                        |&lt;br /&gt;
&lt;br /&gt;
The objective is not simply to translate C/C++ syntax into Python syntax.&lt;br /&gt;
&lt;br /&gt;
Consider:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++ style translated literally&lt;br /&gt;
&lt;br /&gt;
| ! More idiomatic Python |&lt;br /&gt;
| ----------------------- |&lt;br /&gt;
|                         |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
    print(values[i])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for value in values:&lt;br /&gt;
    print(value)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Both programs are correct.&lt;br /&gt;
&lt;br /&gt;
The second expresses the programmer&amp;#039;s intention more directly.&lt;br /&gt;
&lt;br /&gt;
== 43. Summary ==&lt;br /&gt;
&lt;br /&gt;
The main mappings introduced in this laboratory are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python                                    |   |         |&lt;br /&gt;
| ------------------------------------------- | - | ------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;{ ... }&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| indentation                                 |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;                                      |   | &amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;                              |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt;                    |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_set&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::tuple&amp;lt;/code&amp;gt;                     |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;tuple&amp;lt;/code&amp;gt;                          |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt;                      |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cin&amp;lt;/code&amp;gt;                       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| indexed iteration                           |   |         |&lt;br /&gt;
| direct iteration / &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| manual search                               |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::pow(a, b)&amp;lt;/code&amp;gt;                 |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;a ** b&amp;lt;/code&amp;gt;                         |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| integer division                            |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| }                                           |   |         |&lt;br /&gt;
&lt;br /&gt;
The central idea to retain from this laboratory is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Python should not be treated merely as C++ with different syntax. Python provides its own abstractions and idioms, and using them generally produces simpler and more readable programs.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 44. Preparation for Lab 2 ==&lt;br /&gt;
&lt;br /&gt;
Before the next laboratory:&lt;br /&gt;
&lt;br /&gt;
# complete all exercises from this laboratory;&lt;br /&gt;
&lt;br /&gt;
# ensure Python 3 is installed;&lt;br /&gt;
&lt;br /&gt;
# create a virtual environment successfully;&lt;br /&gt;
&lt;br /&gt;
# create a Git repository for the semester project;&lt;br /&gt;
&lt;br /&gt;
# become familiar with lists, dictionaries, tuples and sets;&lt;br /&gt;
&lt;br /&gt;
# review the concepts of classes, objects, inheritance and composition from C++.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Lab 2&amp;#039;&amp;#039;&amp;#039;, these concepts will be revisited from the perspective of Python&amp;#039;s object model and Pythonic object-oriented programming.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8360</id>
		<title>Lab 1</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8360"/>
		<updated>2026-09-14T05:24:27Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* 3. Variables and types */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 1 — Python for C/C++ Programmers =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
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++.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
After completing this laboratory, you should be able to:&lt;br /&gt;
&lt;br /&gt;
* understand the basic syntax of Python;&lt;br /&gt;
* recognize the main differences between Python and C/C++;&lt;br /&gt;
* work with Python&amp;#039;s fundamental data types;&lt;br /&gt;
* use conditional statements and loops;&lt;br /&gt;
* work with lists, dictionaries, sets and tuples;&lt;br /&gt;
* define and call functions;&lt;br /&gt;
* understand basic Python object/reference semantics;&lt;br /&gt;
* recognize several common Python idioms;&lt;br /&gt;
* create and execute a simple Python project.&lt;br /&gt;
&lt;br /&gt;
Throughout this laboratory, equivalent or similar C/C++ and Python code will be presented side by side.&lt;br /&gt;
&lt;br /&gt;
**TOC**&lt;br /&gt;
&lt;br /&gt;
== 1. Running a Python program ==&lt;br /&gt;
&lt;br /&gt;
A C/C++ program normally has to be compiled before it is executed. Python programs are usually executed directly by the Python interpreter.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Hello, world!\n&amp;quot;;&lt;br /&gt;
return 0;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(&amp;quot;Hello, world!&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A Python source file normally uses the extension:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be executed using:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unlike C/C++, Python does not require a special &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
However, larger Python programs commonly use:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Program started\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
print(&amp;quot;Program started&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
if **name** == &amp;quot;**main**&amp;quot;:&lt;br /&gt;
main() &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python version is not mandatory, but this pattern will become useful when programs are split into multiple modules.&lt;br /&gt;
&lt;br /&gt;
== 2. Blocks and indentation ==&lt;br /&gt;
&lt;br /&gt;
In C and C++, braces delimit blocks of code.&lt;br /&gt;
&lt;br /&gt;
Python uses &amp;#039;&amp;#039;&amp;#039;indentation&amp;#039;&amp;#039;&amp;#039; instead.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (x &amp;gt; 10) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Large\n&amp;quot;;&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Value: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
    print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
    print(&amp;quot;Value:&amp;quot;, x)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Indentation in Python is part of the language syntax.&lt;br /&gt;
&lt;br /&gt;
The standard convention is &amp;#039;&amp;#039;&amp;#039;4 spaces per indentation level&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
For example, the following code is incorrect:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python will report an &amp;lt;code&amp;gt;IndentationError&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 3. Variables and types ==&lt;br /&gt;
&lt;br /&gt;
C/C++ variables have a declared static type.&lt;br /&gt;
&lt;br /&gt;
Python variables do not require explicit type declarations.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int count = 10;&lt;br /&gt;
double temperature = 23.5;&lt;br /&gt;
bool enabled = true;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
count = 10&lt;br /&gt;
temperature = 23.5&lt;br /&gt;
enabled = True&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python is &amp;#039;&amp;#039;&amp;#039;dynamically typed&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
This means that a variable name can later refer to an object of another type.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
// Not allowed:&lt;br /&gt;
// value = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
value = &amp;quot;hello&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This does &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; mean that Python has no types.&lt;br /&gt;
&lt;br /&gt;
Objects still have types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;typeinfo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; typeid(value).name();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
&lt;br /&gt;
print(type(value))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python will produce:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common basic types ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ type&lt;br /&gt;
! Python equivalent&lt;br /&gt;
! Example&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;42&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt; / &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;3.14&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt;&lt;br /&gt;
| No separate character type&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;A&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;str&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;Hello&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 4. Constants ==&lt;br /&gt;
&lt;br /&gt;
C++ can enforce that a value cannot be modified using &amp;lt;code&amp;gt;const&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python does not enforce constants at language level.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
const double PI = 3.1415926535;&lt;br /&gt;
const int MAX_USERS = 100;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
PI = 3.1415926535&lt;br /&gt;
MAX_USERS = 100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
By convention, Python names that should be treated as constants are written using uppercase letters.&lt;br /&gt;
&lt;br /&gt;
Nothing prevents the programmer from changing them.&lt;br /&gt;
&lt;br /&gt;
== 5. Arithmetic operators ==&lt;br /&gt;
&lt;br /&gt;
Most arithmetic operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
int sum  = a + b;&lt;br /&gt;
int diff = a - b;&lt;br /&gt;
int prod = a * b;&lt;br /&gt;
int div  = a / b;&lt;br /&gt;
int rem  = a % b; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
sum_ = a + b&lt;br /&gt;
diff = a - b&lt;br /&gt;
prod = a * b&lt;br /&gt;
div = a // b&lt;br /&gt;
rem = a % b &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
There is an important difference between &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a / b;   // 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
print(a / b)   # 3.3333333333333335&lt;br /&gt;
print(a // b)  # 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python also provides an exponentiation operator:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;cmath&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double result = std::pow(2, 8); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
result = 2 ** 8 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 6. Printing values ==&lt;br /&gt;
&lt;br /&gt;
Output using &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt; is replaced by the built-in &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
int age = 21;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; is &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; age&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; years old\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
age = 21&lt;br /&gt;
&lt;br /&gt;
print(name, &amp;quot;is&amp;quot;, age, &amp;quot;years old&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python also provides formatted string literals, commonly called &amp;#039;&amp;#039;&amp;#039;f-strings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Temperature: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; temperature&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot; C\n&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Temperature: {temperature} C&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Expressions can appear directly inside an f-string:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Result: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; a + b&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Result: {a + b}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 7. Reading input ==&lt;br /&gt;
&lt;br /&gt;
Console input is performed using &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An important difference is that &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt; &amp;#039;&amp;#039;&amp;#039;always returns a string&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int age;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Age: &amp;quot;;&lt;br /&gt;
std::cin &amp;gt;&amp;gt; age; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = int(input(&amp;quot;Age: &amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Without the conversion:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = input(&amp;quot;Age: &amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; is a string.&lt;br /&gt;
&lt;br /&gt;
Conversions are explicit:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = std::stoi(text);&lt;br /&gt;
double b = std::stod(text);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = int(text)&lt;br /&gt;
b = float(text)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 8. Conditional statements ==&lt;br /&gt;
&lt;br /&gt;
The overall structure is similar, but Python does not use parentheses or braces.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (temperature &amp;gt; 30) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Hot\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else if (temperature &amp;gt; 20) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Warm\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Cold\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if temperature &amp;gt; 30:&lt;br /&gt;
    print(&amp;quot;Hot&amp;quot;)&lt;br /&gt;
elif temperature &amp;gt; 20:&lt;br /&gt;
    print(&amp;quot;Warm&amp;quot;)&lt;br /&gt;
else:&lt;br /&gt;
    print(&amp;quot;Cold&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python uses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
elif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
instead of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
else if&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 9. Boolean operators ==&lt;br /&gt;
&lt;br /&gt;
Python uses words instead of the C/C++ symbolic logical operators.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python         |   |         |&lt;br /&gt;
| ---------------- | - | ------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt;  |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| -                |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;           |   | &amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;  |   |         |&lt;br /&gt;
| -                |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;   |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| }                |   |         |&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (age &amp;gt;= 18 &amp;amp;&amp;amp; enabled) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if (!enabled) {&lt;br /&gt;
...&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if age &amp;gt;= 18 and enabled:&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
if not enabled:&lt;br /&gt;
... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 10. Comparison operators ==&lt;br /&gt;
&lt;br /&gt;
Most comparison operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Operation&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python              |&lt;br /&gt;
| --------------------- |&lt;br /&gt;
| equality              |&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| inequality            |&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| less than             |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                     |&lt;br /&gt;
| greater than          |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                     |&lt;br /&gt;
| less than or equal    |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| greater than or equal |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| }                     |&lt;br /&gt;
&lt;br /&gt;
Python also allows chained comparisons.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (0 &amp;lt;= x &amp;amp;&amp;amp; x &amp;lt; 100) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 0 &amp;lt;= x &amp;lt; 100:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 11. The ternary operator ==&lt;br /&gt;
&lt;br /&gt;
Both languages support conditional expressions.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int maximum = (a &amp;gt; b) ? a : b;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
maximum = a if a &amp;gt; b else b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python&amp;#039;s order is different:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
value_if_true if condition else value_if_false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 12. While loops ==&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;code&amp;gt;while&amp;lt;/code&amp;gt; loop is very similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int i = 0;&lt;br /&gt;
&lt;br /&gt;
while (i &amp;lt; 5) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
i++;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
&lt;br /&gt;
while i &amp;lt; 5:&lt;br /&gt;
print(i)&lt;br /&gt;
i += 1 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not have &amp;lt;code&amp;gt;++&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;--&amp;lt;/code&amp;gt; operators.&lt;br /&gt;
&lt;br /&gt;
Therefore:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i += 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is used instead.&lt;br /&gt;
&lt;br /&gt;
== 13. For loops ==&lt;br /&gt;
&lt;br /&gt;
This is one of the first important conceptual differences.&lt;br /&gt;
&lt;br /&gt;
In C/C++, a &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement commonly controls a numeric index.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement iterates over objects.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 5; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;range(5)&amp;lt;/code&amp;gt; generates values conceptually equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
0, 1, 2, 3, 4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A different starting value can be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 2; i &amp;lt; 10; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(2, 10):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A step can also be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i += 2) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(0, 10, 2):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 14. Iterating over containers ==&lt;br /&gt;
&lt;br /&gt;
Suppose we want to print every element of a vector/list.&lt;br /&gt;
&lt;br /&gt;
An index-based implementation is possible:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; values.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[i] &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
print(values[i]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
However, this is generally &amp;#039;&amp;#039;&amp;#039;not the preferred Python style&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
If the index is not required, iterate directly over the elements.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; value : values) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; value &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for value in values:&lt;br /&gt;
print(value) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The second Python version should normally be preferred.&lt;br /&gt;
&lt;br /&gt;
== 15. When both index and value are required ==&lt;br /&gt;
&lt;br /&gt;
C/C++ programmers frequently use an explicit index.&lt;br /&gt;
&lt;br /&gt;
Python provides &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;std::string&amp;gt; names = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; names.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; names[i]&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
names = [&amp;quot;Ana&amp;quot;, &amp;quot;Bob&amp;quot;, &amp;quot;Carol&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
for i, name in enumerate(names):&lt;br /&gt;
print(i, &amp;quot;:&amp;quot;, name) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This introduces another frequently used Python feature: &amp;#039;&amp;#039;&amp;#039;unpacking&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== 16. Lists ==&lt;br /&gt;
&lt;br /&gt;
The closest commonly used Python equivalent to &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.push_back(40);&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[0];&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values.size(); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
values.append(40)&lt;br /&gt;
&lt;br /&gt;
print(values[0])&lt;br /&gt;
print(len(values)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python lists can contain objects of different types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// A normal std::vector cannot&lt;br /&gt;
// contain unrelated types.&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
10, 20, 30&lt;br /&gt;
}; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [&lt;br /&gt;
10,&lt;br /&gt;
3.14,&lt;br /&gt;
&amp;quot;hello&amp;quot;,&lt;br /&gt;
True&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Although Python allows this, homogeneous collections are generally easier to understand and maintain when the elements conceptually represent the same kind of data.&lt;br /&gt;
&lt;br /&gt;
== 17. Negative indexing ==&lt;br /&gt;
&lt;br /&gt;
Python allows indexing from the end of a sequence.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {10, 20, 30, 40};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 1]; // 40&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 2]; // 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40]&lt;br /&gt;
&lt;br /&gt;
print(v[-1])  # 40&lt;br /&gt;
print(v[-2])  # 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 18. Slicing ==&lt;br /&gt;
&lt;br /&gt;
Python sequences support slicing directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {&lt;br /&gt;
    10, 20, 30, 40, 50&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; part(&lt;br /&gt;
v.begin() + 1,&lt;br /&gt;
v.begin() + 4&lt;br /&gt;
); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40, 50]&lt;br /&gt;
&lt;br /&gt;
part = v[1:4] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The result is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[20, 30, 40]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
General syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
sequence[start:stop:step]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// Requires an explicit loop or&lt;br /&gt;
// another algorithm.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [0, 1, 2, 3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
print(v[1:4])   # [1, 2, 3]&lt;br /&gt;
print(v[:3])    # [0, 1, 2]&lt;br /&gt;
print(v[3:])    # [3, 4, 5]&lt;br /&gt;
print(v[::2])   # [0, 2, 4]&lt;br /&gt;
print(v[::-1])  # [5, 4, 3, 2, 1, 0] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 19. Membership testing ==&lt;br /&gt;
&lt;br /&gt;
Checking whether an element exists in a container is commonly performed using &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
if (std::find(&lt;br /&gt;
values.begin(),&lt;br /&gt;
values.end(),&lt;br /&gt;
20&lt;br /&gt;
) != values.end()) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The opposite test uses &amp;lt;code&amp;gt;not in&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 100 not in values:&lt;br /&gt;
    print(&amp;quot;Not found&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 20. Dictionaries ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt; is similar to a C++ associative container such as &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades;&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10;&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; grades[&amp;quot;Ana&amp;quot;]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {}&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8&lt;br /&gt;
&lt;br /&gt;
print(grades[&amp;quot;Ana&amp;quot;]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A dictionary can also be initialized directly:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades = {&lt;br /&gt;
    {&amp;quot;Ana&amp;quot;, 10},&lt;br /&gt;
    {&amp;quot;Bob&amp;quot;, 8},&lt;br /&gt;
    {&amp;quot;Carol&amp;quot;, 9}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 10,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 8,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 9,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Iterating over keys and values:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; grade&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
    print(name, &amp;quot;:&amp;quot;, grade)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 21. Sets ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; represents a collection containing unique values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.insert(40);&lt;br /&gt;
&lt;br /&gt;
if (20 != values.end()) {&lt;br /&gt;
// conceptually test membership&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = {10, 20, 30}&lt;br /&gt;
&lt;br /&gt;
values.add(40)&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A more correct C++ membership test would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (values.find(20) != values.end()) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets are particularly useful when:&lt;br /&gt;
&lt;br /&gt;
* duplicate values should be removed;&lt;br /&gt;
* fast membership tests are required;&lt;br /&gt;
* mathematical set operations are useful.&lt;br /&gt;
&lt;br /&gt;
== 22. Tuples ==&lt;br /&gt;
&lt;br /&gt;
Tuples are similar to lists, but they are immutable.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::tuple&amp;lt;int, double, std::string&amp;gt; data {&lt;br /&gt;
    10,&lt;br /&gt;
    3.14,&lt;br /&gt;
    &amp;quot;test&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
data = (10, 3.14, &amp;quot;test&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python uses tuples extensively for returning or unpacking multiple values.&lt;br /&gt;
&lt;br /&gt;
== 23. Functions ==&lt;br /&gt;
&lt;br /&gt;
Functions are declared using &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    return a + b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int result = add(2, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def add(a, b):&lt;br /&gt;
return a + b&lt;br /&gt;
&lt;br /&gt;
result = add(2, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not require parameter or return types to be declared.&lt;br /&gt;
&lt;br /&gt;
However, Python supports optional &amp;#039;&amp;#039;&amp;#039;type hints&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
double average(&lt;br /&gt;
    double a,&lt;br /&gt;
    double b&lt;br /&gt;
) {&lt;br /&gt;
    return (a + b) / 2.0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(&lt;br /&gt;
    a: float,&lt;br /&gt;
    b: float&lt;br /&gt;
) -&amp;gt; float:&lt;br /&gt;
    return (a + b) / 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Type hints do not normally enforce types at runtime.&lt;br /&gt;
&lt;br /&gt;
They provide information for:&lt;br /&gt;
&lt;br /&gt;
* the programmer;&lt;br /&gt;
* IDEs;&lt;br /&gt;
* static analysis tools;&lt;br /&gt;
* documentation.&lt;br /&gt;
&lt;br /&gt;
== 24. Default parameters ==&lt;br /&gt;
&lt;br /&gt;
Both C++ and Python support default parameter values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void greet(&lt;br /&gt;
    const std::string&amp;amp; name = &amp;quot;World&amp;quot;&lt;br /&gt;
) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Hello, &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
greet();&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def greet(name=&amp;quot;World&amp;quot;):&lt;br /&gt;
print(f&amp;quot;Hello, {name}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
greet()&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 25. Named arguments ==&lt;br /&gt;
&lt;br /&gt;
Python allows function arguments to be specified by name.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void connect(&lt;br /&gt;
    std::string host,&lt;br /&gt;
    int port,&lt;br /&gt;
    bool secure&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
connect(&amp;quot;server.local&amp;quot;, 443, true); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def connect(host, port, secure):&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
connect(&lt;br /&gt;
host=&amp;quot;server.local&amp;quot;,&lt;br /&gt;
port=443,&lt;br /&gt;
secure=True&lt;br /&gt;
) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This can make calls with several parameters easier to understand.&lt;br /&gt;
&lt;br /&gt;
== 26. Returning multiple values ==&lt;br /&gt;
&lt;br /&gt;
In C++, multiple return values typically require a structure, tuple, pair or output parameters.&lt;br /&gt;
&lt;br /&gt;
Python commonly uses tuples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::pair&amp;lt;int, int&amp;gt; minmax(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    if (a &amp;lt; b)&lt;br /&gt;
        return {a, b};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return {b, a};&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
auto [minimum, maximum] = minmax(10, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def minmax(a, b):&lt;br /&gt;
if a &amp;lt; b:&lt;br /&gt;
return a, b&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return b, a&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
minimum, maximum = minmax(10, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is another example of tuple unpacking.&lt;br /&gt;
&lt;br /&gt;
== 27. Swapping variables ==&lt;br /&gt;
&lt;br /&gt;
A temporary variable is normally used in C.&lt;br /&gt;
&lt;br /&gt;
Python supports unpacking directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int temp = a;&lt;br /&gt;
a = b;&lt;br /&gt;
b = temp;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a, b = b, a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 28. Strings ==&lt;br /&gt;
&lt;br /&gt;
Python strings behave more like C++ &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt; objects than traditional C character arrays.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text.size();&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text[0]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(len(text))&lt;br /&gt;
print(text[0]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
String concatenation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string first = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
std::string second = &amp;quot;World&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::string result =&lt;br /&gt;
first + &amp;quot; &amp;quot; + second; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
first = &amp;quot;Hello&amp;quot;&lt;br /&gt;
second = &amp;quot;World&amp;quot;&lt;br /&gt;
&lt;br /&gt;
result = first + &amp;quot; &amp;quot; + second &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python strings provide many built-in operations:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Usually requires algorithms,&lt;br /&gt;
// explicit transformations,&lt;br /&gt;
// or utility functions. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(text.upper())&lt;br /&gt;
print(text.lower())&lt;br /&gt;
print(text.startswith(&amp;quot;he&amp;quot;))&lt;br /&gt;
print(text.endswith(&amp;quot;lo&amp;quot;))&lt;br /&gt;
print(text.replace(&amp;quot;l&amp;quot;, &amp;quot;x&amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 29. Lists versus arrays: important semantic difference ==&lt;br /&gt;
&lt;br /&gt;
Consider assigning one collection to another.&lt;br /&gt;
&lt;br /&gt;
With normal C++ value semantics, copying a vector produces another vector.&lt;br /&gt;
&lt;br /&gt;
Python assignment behaves differently.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&lt;br /&gt;
b[0] = 100;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a[0];  // 1&lt;br /&gt;
std::cout &amp;lt;&amp;lt; b[0];  // 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
b = a&lt;br /&gt;
&lt;br /&gt;
b[0] = 100&lt;br /&gt;
&lt;br /&gt;
print(a[0])  # 100&lt;br /&gt;
print(b[0])  # 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This happens because:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
does not copy the list.&lt;br /&gt;
&lt;br /&gt;
Both names refer to the &amp;#039;&amp;#039;&amp;#039;same list object&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Conceptually:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
       +-------------+&lt;br /&gt;
a ---&amp;gt; | [1, 2, 3]   |&lt;br /&gt;
       +-------------+&lt;br /&gt;
          ^&lt;br /&gt;
          |&lt;br /&gt;
b --------+&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To explicitly create a shallow copy:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a.copy()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = list(a)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This distinction is extremely important when using Python.&lt;br /&gt;
&lt;br /&gt;
== 30. Equality versus identity ==&lt;br /&gt;
&lt;br /&gt;
Python distinguishes between:&lt;br /&gt;
&lt;br /&gt;
* whether two objects contain equal values;&lt;br /&gt;
* whether two names refer to the exact same object.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; (a == b);  // true&lt;br /&gt;
&lt;br /&gt;
// Address comparison would be needed&lt;br /&gt;
// to determine object identity. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
b = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
print(a == b)  # True&lt;br /&gt;
print(a is b)  # False &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In Python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
==  tests value equality&lt;br /&gt;
is  tests object identity&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not use &amp;lt;code&amp;gt;is&amp;lt;/code&amp;gt; when you intend to compare values.&lt;br /&gt;
&lt;br /&gt;
A common correct use is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if value is None:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 31. None and nullptr ==&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; represents the absence of a value.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Object* ptr = nullptr;&lt;br /&gt;
&lt;br /&gt;
if (ptr == nullptr) {&lt;br /&gt;
...&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = None&lt;br /&gt;
&lt;br /&gt;
if value is None:&lt;br /&gt;
... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 32. Exceptions ==&lt;br /&gt;
&lt;br /&gt;
Exception handling is conceptually similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    int value = std::stoi(text);&lt;br /&gt;
}&lt;br /&gt;
catch (const std::exception&amp;amp; e) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Invalid value\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    value = int(text)&lt;br /&gt;
except ValueError:&lt;br /&gt;
    print(&amp;quot;Invalid value&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python generally uses exceptions rather than special error return values for many failure conditions.&lt;br /&gt;
&lt;br /&gt;
== 33. A first Pythonic transformation ==&lt;br /&gt;
&lt;br /&gt;
Consider creating a vector/list containing the squares of the numbers from 0 to 9.&lt;br /&gt;
&lt;br /&gt;
A direct translation from C++ could look like this:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — direct translation |&lt;br /&gt;
| ----------------------------- |&lt;br /&gt;
|                               |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = []&lt;br /&gt;
&lt;br /&gt;
for i in range(10):&lt;br /&gt;
squares.append(i * i) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python code is correct.&lt;br /&gt;
&lt;br /&gt;
However, Python provides a concise construct called a &amp;#039;&amp;#039;&amp;#039;list comprehension&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — idiomatic |&lt;br /&gt;
| -------------------- |&lt;br /&gt;
|                      |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A condition can also be included.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
if (i % 2 == 0) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
}&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
if i % 2 == 0&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
An important objective of this course is not only to write programs that work in Python, but to learn to write programs in a style appropriate for Python.&lt;br /&gt;
&lt;br /&gt;
== 34. Example: processing student grades ==&lt;br /&gt;
&lt;br /&gt;
Consider the following problem:&lt;br /&gt;
&lt;br /&gt;
* store the grades of several students;&lt;br /&gt;
* compute the average;&lt;br /&gt;
* print the students whose grade is greater than or equal to the average.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;unordered_map&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt; grades = {&lt;br /&gt;
{&amp;quot;Ana&amp;quot;, 9.5},&lt;br /&gt;
{&amp;quot;Bob&amp;quot;, 7.0},&lt;br /&gt;
{&amp;quot;Carol&amp;quot;, 8.5},&lt;br /&gt;
{&amp;quot;Dan&amp;quot;, 6.0}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
double sum = 0.0;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    sum += grade;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
double average = sum / grades.size();&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Average: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; average&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    if (grade &amp;gt;= average) {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
                  &amp;lt;&amp;lt; grade&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
&amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
&amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
&amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
&amp;quot;Dan&amp;quot;: 6.0,&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
average = sum(grades.values()) / len(grades)&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;Average: {average:.2f}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
if grade &amp;gt;= average:&lt;br /&gt;
print(f&amp;quot;{name}: {grade}&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that the Python version uses existing language functionality rather than manually reproducing every operation.&lt;br /&gt;
&lt;br /&gt;
In particular:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sum(grades.values())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
expresses the intention directly.&lt;br /&gt;
&lt;br /&gt;
== 35. Exercise 1 — Translation ==&lt;br /&gt;
&lt;br /&gt;
Translate the following C++ program into Python.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
12, 7, 18, 3, 24, 11&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
int sum = 0;&lt;br /&gt;
int count = 0;&lt;br /&gt;
&lt;br /&gt;
for (int value : values) {&lt;br /&gt;
    if (value % 2 == 0) {&lt;br /&gt;
        sum += value;&lt;br /&gt;
        count++;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Count: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; count&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Sum: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; sum&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Your implementation:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# TODO&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
After implementing a direct translation, try to simplify the Python implementation.&lt;br /&gt;
&lt;br /&gt;
== 36. Exercise 2 — Statistics ==&lt;br /&gt;
&lt;br /&gt;
Write a Python program that:&lt;br /&gt;
&lt;br /&gt;
# reads a sequence of integer values;&lt;br /&gt;
&lt;br /&gt;
# stores them in a list;&lt;br /&gt;
&lt;br /&gt;
# prints the minimum value;&lt;br /&gt;
&lt;br /&gt;
# prints the maximum value;&lt;br /&gt;
&lt;br /&gt;
# prints the average;&lt;br /&gt;
&lt;br /&gt;
# prints how many values are above the average.&lt;br /&gt;
&lt;br /&gt;
The equivalent C++ structure might begin as follows:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values;&lt;br /&gt;
&lt;br /&gt;
// Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
int minimum = ...;&lt;br /&gt;
int maximum = ...;&lt;br /&gt;
double average = ...; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = []&lt;br /&gt;
&lt;br /&gt;
# Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
minimum = ...&lt;br /&gt;
maximum = ...&lt;br /&gt;
average = ... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Useful Python functions include:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
len(...)&lt;br /&gt;
sum(...)&lt;br /&gt;
min(...)&lt;br /&gt;
max(...)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 37. Exercise 3 — Word frequency ==&lt;br /&gt;
&lt;br /&gt;
Given a sentence, count how many times each word occurs.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python is simple and python is powerful&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
should produce information equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python -&amp;gt; 2&lt;br /&gt;
is -&amp;gt; 2&lt;br /&gt;
simple -&amp;gt; 1&lt;br /&gt;
and -&amp;gt; 1&lt;br /&gt;
powerful -&amp;gt; 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A possible C++ data structure and its Python equivalent are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt;&lt;br /&gt;
    frequencies;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
frequencies = {}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sentence.split()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to separate the sentence into words.&lt;br /&gt;
&lt;br /&gt;
== 38. Exercise 4 — Remove duplicates ==&lt;br /&gt;
&lt;br /&gt;
Given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [4, 2, 7, 4, 2, 9, 7, 1]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
create a collection containing every distinct value.&lt;br /&gt;
&lt;br /&gt;
Think about the following C++ and Python data structures:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; unique_values;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
unique_values = set()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Try to solve the problem both:&lt;br /&gt;
&lt;br /&gt;
# using an explicit loop;&lt;br /&gt;
&lt;br /&gt;
# using Python&amp;#039;s built-in functionality.&lt;br /&gt;
&lt;br /&gt;
== 39. Exercise 5 — Small problem ==&lt;br /&gt;
&lt;br /&gt;
Write a program that receives a list of students and their grades and determines:&lt;br /&gt;
&lt;br /&gt;
* the class average;&lt;br /&gt;
* the student with the highest grade;&lt;br /&gt;
* all students who passed;&lt;br /&gt;
* all students whose grade is above the class average.&lt;br /&gt;
&lt;br /&gt;
Suggested representation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt;&lt;br /&gt;
    grades;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
    &amp;quot;Dan&amp;quot;: 4.0,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Avoid using numeric indices unless an index is actually required.&lt;br /&gt;
&lt;br /&gt;
== 40. Creating a project environment ==&lt;br /&gt;
&lt;br /&gt;
Python applications commonly use a &amp;#039;&amp;#039;&amp;#039;virtual environment&amp;#039;&amp;#039;&amp;#039; to isolate project dependencies.&lt;br /&gt;
&lt;br /&gt;
Create a directory for the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir python-project&lt;br /&gt;
cd python-project&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a virtual environment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 -m venv .venv&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Linux/macOS:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source .venv/bin/activate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Windows PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
.venv\Scripts\Activate.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command prompt will normally indicate that the environment is active.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
(.venv) student@computer:~/python-project$&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Packages installed using &amp;lt;code&amp;gt;pip&amp;lt;/code&amp;gt; will now be installed inside this environment.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip install requests&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To inspect installed packages:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip list&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 41. Recommended initial project structure ==&lt;br /&gt;
&lt;br /&gt;
For the first laboratories, the following structure is sufficient:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python-project/&lt;br /&gt;
├── .venv/&lt;br /&gt;
├── main.py&lt;br /&gt;
├── README.md&lt;br /&gt;
└── .gitignore&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A basic &amp;lt;code&amp;gt;.gitignore&amp;lt;/code&amp;gt; should contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.venv/&lt;br /&gt;
__pycache__/&lt;br /&gt;
*.pyc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; commit the virtual environment to Git.&lt;br /&gt;
&lt;br /&gt;
== 42. C/C++ habits to reconsider in Python ==&lt;br /&gt;
&lt;br /&gt;
When transitioning from C/C++ to Python, the following patterns should generally be reconsidered.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ habit&lt;br /&gt;
&lt;br /&gt;
| ! Typical Python approach                |&lt;br /&gt;
| ---------------------------------------- |&lt;br /&gt;
| Iterate using an integer index           |&lt;br /&gt;
| Iterate directly over the objects        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually maintain counters               |&lt;br /&gt;
| Consider &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Search manually through a container      |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually calculate container size        |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;len()&amp;lt;/code&amp;gt;                   |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit temporary variable for swapping |&lt;br /&gt;
| Use tuple unpacking                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit loop for simple transformations |&lt;br /&gt;
| Consider a comprehension                 |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Long output expressions                  |&lt;br /&gt;
| Use f-strings                            |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Copy assumed after assignment            |&lt;br /&gt;
| Remember that names refer to objects     |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Check null using equality                |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;is None&amp;lt;/code&amp;gt;                 |&lt;br /&gt;
| }                                        |&lt;br /&gt;
&lt;br /&gt;
The objective is not simply to translate C/C++ syntax into Python syntax.&lt;br /&gt;
&lt;br /&gt;
Consider:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++ style translated literally&lt;br /&gt;
&lt;br /&gt;
| ! More idiomatic Python |&lt;br /&gt;
| ----------------------- |&lt;br /&gt;
|                         |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
    print(values[i])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for value in values:&lt;br /&gt;
    print(value)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Both programs are correct.&lt;br /&gt;
&lt;br /&gt;
The second expresses the programmer&amp;#039;s intention more directly.&lt;br /&gt;
&lt;br /&gt;
== 43. Summary ==&lt;br /&gt;
&lt;br /&gt;
The main mappings introduced in this laboratory are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python                                    |   |         |&lt;br /&gt;
| ------------------------------------------- | - | ------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;{ ... }&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| indentation                                 |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;                                      |   | &amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;                              |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt;                    |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_set&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::tuple&amp;lt;/code&amp;gt;                     |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;tuple&amp;lt;/code&amp;gt;                          |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt;                      |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cin&amp;lt;/code&amp;gt;                       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| indexed iteration                           |   |         |&lt;br /&gt;
| direct iteration / &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| manual search                               |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::pow(a, b)&amp;lt;/code&amp;gt;                 |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;a ** b&amp;lt;/code&amp;gt;                         |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| integer division                            |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| }                                           |   |         |&lt;br /&gt;
&lt;br /&gt;
The central idea to retain from this laboratory is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Python should not be treated merely as C++ with different syntax. Python provides its own abstractions and idioms, and using them generally produces simpler and more readable programs.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 44. Preparation for Lab 2 ==&lt;br /&gt;
&lt;br /&gt;
Before the next laboratory:&lt;br /&gt;
&lt;br /&gt;
# complete all exercises from this laboratory;&lt;br /&gt;
&lt;br /&gt;
# ensure Python 3 is installed;&lt;br /&gt;
&lt;br /&gt;
# create a virtual environment successfully;&lt;br /&gt;
&lt;br /&gt;
# create a Git repository for the semester project;&lt;br /&gt;
&lt;br /&gt;
# become familiar with lists, dictionaries, tuples and sets;&lt;br /&gt;
&lt;br /&gt;
# review the concepts of classes, objects, inheritance and composition from C++.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Lab 2&amp;#039;&amp;#039;&amp;#039;, these concepts will be revisited from the perspective of Python&amp;#039;s object model and Pythonic object-oriented programming.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8359</id>
		<title>Lab 1</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8359"/>
		<updated>2026-09-14T05:22:06Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Lab 1 — Python for C/C++ Programmers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 1 — Python for C/C++ Programmers =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
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++.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
After completing this laboratory, you should be able to:&lt;br /&gt;
&lt;br /&gt;
* understand the basic syntax of Python;&lt;br /&gt;
* recognize the main differences between Python and C/C++;&lt;br /&gt;
* work with Python&amp;#039;s fundamental data types;&lt;br /&gt;
* use conditional statements and loops;&lt;br /&gt;
* work with lists, dictionaries, sets and tuples;&lt;br /&gt;
* define and call functions;&lt;br /&gt;
* understand basic Python object/reference semantics;&lt;br /&gt;
* recognize several common Python idioms;&lt;br /&gt;
* create and execute a simple Python project.&lt;br /&gt;
&lt;br /&gt;
Throughout this laboratory, equivalent or similar C/C++ and Python code will be presented side by side.&lt;br /&gt;
&lt;br /&gt;
**TOC**&lt;br /&gt;
&lt;br /&gt;
== 1. Running a Python program ==&lt;br /&gt;
&lt;br /&gt;
A C/C++ program normally has to be compiled before it is executed. Python programs are usually executed directly by the Python interpreter.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Hello, world!\n&amp;quot;;&lt;br /&gt;
return 0;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(&amp;quot;Hello, world!&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A Python source file normally uses the extension:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be executed using:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unlike C/C++, Python does not require a special &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
However, larger Python programs commonly use:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Program started\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
print(&amp;quot;Program started&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
if **name** == &amp;quot;**main**&amp;quot;:&lt;br /&gt;
main() &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python version is not mandatory, but this pattern will become useful when programs are split into multiple modules.&lt;br /&gt;
&lt;br /&gt;
== 2. Blocks and indentation ==&lt;br /&gt;
&lt;br /&gt;
In C and C++, braces delimit blocks of code.&lt;br /&gt;
&lt;br /&gt;
Python uses &amp;#039;&amp;#039;&amp;#039;indentation&amp;#039;&amp;#039;&amp;#039; instead.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (x &amp;gt; 10) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Large\n&amp;quot;;&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Value: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
    print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
    print(&amp;quot;Value:&amp;quot;, x)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Indentation in Python is part of the language syntax.&lt;br /&gt;
&lt;br /&gt;
The standard convention is &amp;#039;&amp;#039;&amp;#039;4 spaces per indentation level&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
For example, the following code is incorrect:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python will report an &amp;lt;code&amp;gt;IndentationError&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 3. Variables and types ==&lt;br /&gt;
&lt;br /&gt;
C/C++ variables have a declared static type.&lt;br /&gt;
&lt;br /&gt;
Python variables do not require explicit type declarations.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int count = 10;&lt;br /&gt;
double temperature = 23.5;&lt;br /&gt;
bool enabled = true;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
count = 10&lt;br /&gt;
temperature = 23.5&lt;br /&gt;
enabled = True&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python is &amp;#039;&amp;#039;&amp;#039;dynamically typed&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
This means that a variable name can later refer to an object of another type.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
// Not allowed:&lt;br /&gt;
// value = &amp;quot;hello&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
value = &amp;quot;hello&amp;quot; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This does &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; mean that Python has no types.&lt;br /&gt;
&lt;br /&gt;
Objects still have types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; typeid(value).name(); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
&lt;br /&gt;
print(type(value)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python will produce:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common basic types ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! C/C++ type&lt;br /&gt;
! Python equivalent&lt;br /&gt;
&lt;br /&gt;
| ! Example                                |&lt;br /&gt;
| ---------------------------------------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;                         |&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;                         |&lt;br /&gt;
| &amp;lt;code&amp;gt;42&amp;lt;/code&amp;gt;                          |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt; / &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt;                       |&lt;br /&gt;
| &amp;lt;code&amp;gt;3.14&amp;lt;/code&amp;gt;                        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;                        |&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;                        |&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;    |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt;                        |&lt;br /&gt;
| no separate character type               |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;A&amp;quot;&amp;lt;/code&amp;gt;                         |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt;                 |&lt;br /&gt;
| &amp;lt;code&amp;gt;str&amp;lt;/code&amp;gt;                         |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;Hello&amp;quot;&amp;lt;/code&amp;gt;                     |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;                     |&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;                        |&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;                        |&lt;br /&gt;
| }                                        |&lt;br /&gt;
&lt;br /&gt;
== 4. Constants ==&lt;br /&gt;
&lt;br /&gt;
C++ can enforce that a value cannot be modified using &amp;lt;code&amp;gt;const&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python does not enforce constants at language level.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
const double PI = 3.1415926535;&lt;br /&gt;
const int MAX_USERS = 100;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
PI = 3.1415926535&lt;br /&gt;
MAX_USERS = 100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
By convention, Python names that should be treated as constants are written using uppercase letters.&lt;br /&gt;
&lt;br /&gt;
Nothing prevents the programmer from changing them.&lt;br /&gt;
&lt;br /&gt;
== 5. Arithmetic operators ==&lt;br /&gt;
&lt;br /&gt;
Most arithmetic operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
int sum  = a + b;&lt;br /&gt;
int diff = a - b;&lt;br /&gt;
int prod = a * b;&lt;br /&gt;
int div  = a / b;&lt;br /&gt;
int rem  = a % b; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
sum_ = a + b&lt;br /&gt;
diff = a - b&lt;br /&gt;
prod = a * b&lt;br /&gt;
div = a // b&lt;br /&gt;
rem = a % b &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
There is an important difference between &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a / b;   // 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
print(a / b)   # 3.3333333333333335&lt;br /&gt;
print(a // b)  # 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python also provides an exponentiation operator:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;cmath&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double result = std::pow(2, 8); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
result = 2 ** 8 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 6. Printing values ==&lt;br /&gt;
&lt;br /&gt;
Output using &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt; is replaced by the built-in &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
int age = 21;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; is &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; age&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot; years old\n&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
age = 21&lt;br /&gt;
&lt;br /&gt;
print(name, &amp;quot;is&amp;quot;, age, &amp;quot;years old&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python also provides formatted string literals, commonly called &amp;#039;&amp;#039;&amp;#039;f-strings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Temperature: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; temperature&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot; C\n&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Temperature: {temperature} C&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Expressions can appear directly inside an f-string:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Result: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; a + b&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(f&amp;quot;Result: {a + b}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 7. Reading input ==&lt;br /&gt;
&lt;br /&gt;
Console input is performed using &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An important difference is that &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt; &amp;#039;&amp;#039;&amp;#039;always returns a string&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int age;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Age: &amp;quot;;&lt;br /&gt;
std::cin &amp;gt;&amp;gt; age; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = int(input(&amp;quot;Age: &amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Without the conversion:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
age = input(&amp;quot;Age: &amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; is a string.&lt;br /&gt;
&lt;br /&gt;
Conversions are explicit:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = std::stoi(text);&lt;br /&gt;
double b = std::stod(text);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = int(text)&lt;br /&gt;
b = float(text)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 8. Conditional statements ==&lt;br /&gt;
&lt;br /&gt;
The overall structure is similar, but Python does not use parentheses or braces.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (temperature &amp;gt; 30) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Hot\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else if (temperature &amp;gt; 20) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Warm\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Cold\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if temperature &amp;gt; 30:&lt;br /&gt;
    print(&amp;quot;Hot&amp;quot;)&lt;br /&gt;
elif temperature &amp;gt; 20:&lt;br /&gt;
    print(&amp;quot;Warm&amp;quot;)&lt;br /&gt;
else:&lt;br /&gt;
    print(&amp;quot;Cold&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python uses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
elif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
instead of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
else if&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 9. Boolean operators ==&lt;br /&gt;
&lt;br /&gt;
Python uses words instead of the C/C++ symbolic logical operators.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python         |   |         |&lt;br /&gt;
| ---------------- | - | ------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt;  |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| -                |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;           |   | &amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;  |   |         |&lt;br /&gt;
| -                |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;   |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| }                |   |         |&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (age &amp;gt;= 18 &amp;amp;&amp;amp; enabled) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if (!enabled) {&lt;br /&gt;
...&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if age &amp;gt;= 18 and enabled:&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
if not enabled:&lt;br /&gt;
... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 10. Comparison operators ==&lt;br /&gt;
&lt;br /&gt;
Most comparison operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! Operation&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python              |&lt;br /&gt;
| --------------------- |&lt;br /&gt;
| equality              |&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| inequality            |&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;!=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| less than             |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                     |&lt;br /&gt;
| greater than          |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                     |&lt;br /&gt;
| less than or equal    |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;lt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| -                     |&lt;br /&gt;
| greater than or equal |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;gt;=&amp;lt;/code&amp;gt;       |&lt;br /&gt;
| }                     |&lt;br /&gt;
&lt;br /&gt;
Python also allows chained comparisons.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (0 &amp;lt;= x &amp;amp;&amp;amp; x &amp;lt; 100) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 0 &amp;lt;= x &amp;lt; 100:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 11. The ternary operator ==&lt;br /&gt;
&lt;br /&gt;
Both languages support conditional expressions.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int maximum = (a &amp;gt; b) ? a : b;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
maximum = a if a &amp;gt; b else b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that Python&amp;#039;s order is different:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
value_if_true if condition else value_if_false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 12. While loops ==&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;code&amp;gt;while&amp;lt;/code&amp;gt; loop is very similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int i = 0;&lt;br /&gt;
&lt;br /&gt;
while (i &amp;lt; 5) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
i++;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i = 0&lt;br /&gt;
&lt;br /&gt;
while i &amp;lt; 5:&lt;br /&gt;
print(i)&lt;br /&gt;
i += 1 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not have &amp;lt;code&amp;gt;++&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;--&amp;lt;/code&amp;gt; operators.&lt;br /&gt;
&lt;br /&gt;
Therefore:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
i += 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is used instead.&lt;br /&gt;
&lt;br /&gt;
== 13. For loops ==&lt;br /&gt;
&lt;br /&gt;
This is one of the first important conceptual differences.&lt;br /&gt;
&lt;br /&gt;
In C/C++, a &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement commonly controls a numeric index.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; statement iterates over objects.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 5; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(5):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;range(5)&amp;lt;/code&amp;gt; generates values conceptually equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
0, 1, 2, 3, 4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A different starting value can be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 2; i &amp;lt; 10; i++) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(2, 10):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A step can also be specified:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i += 2) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(0, 10, 2):&lt;br /&gt;
    print(i)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 14. Iterating over containers ==&lt;br /&gt;
&lt;br /&gt;
Suppose we want to print every element of a vector/list.&lt;br /&gt;
&lt;br /&gt;
An index-based implementation is possible:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; values.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[i] &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
print(values[i]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
However, this is generally &amp;#039;&amp;#039;&amp;#039;not the preferred Python style&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
If the index is not required, iterate directly over the elements.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; value : values) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; value &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
for value in values:&lt;br /&gt;
print(value) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The second Python version should normally be preferred.&lt;br /&gt;
&lt;br /&gt;
== 15. When both index and value are required ==&lt;br /&gt;
&lt;br /&gt;
C/C++ programmers frequently use an explicit index.&lt;br /&gt;
&lt;br /&gt;
Python provides &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;std::string&amp;gt; names = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
for (size_t i = 0; i &amp;lt; names.size(); i++) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; i&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
&amp;lt;&amp;lt; names[i]&lt;br /&gt;
&amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
names = [&amp;quot;Ana&amp;quot;, &amp;quot;Bob&amp;quot;, &amp;quot;Carol&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
for i, name in enumerate(names):&lt;br /&gt;
print(i, &amp;quot;:&amp;quot;, name) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This introduces another frequently used Python feature: &amp;#039;&amp;#039;&amp;#039;unpacking&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== 16. Lists ==&lt;br /&gt;
&lt;br /&gt;
The closest commonly used Python equivalent to &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.push_back(40);&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values[0];&lt;br /&gt;
std::cout &amp;lt;&amp;lt; values.size(); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
values.append(40)&lt;br /&gt;
&lt;br /&gt;
print(values[0])&lt;br /&gt;
print(len(values)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python lists can contain objects of different types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// A normal std::vector cannot&lt;br /&gt;
// contain unrelated types.&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
10, 20, 30&lt;br /&gt;
}; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [&lt;br /&gt;
10,&lt;br /&gt;
3.14,&lt;br /&gt;
&amp;quot;hello&amp;quot;,&lt;br /&gt;
True&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Although Python allows this, homogeneous collections are generally easier to understand and maintain when the elements conceptually represent the same kind of data.&lt;br /&gt;
&lt;br /&gt;
== 17. Negative indexing ==&lt;br /&gt;
&lt;br /&gt;
Python allows indexing from the end of a sequence.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {10, 20, 30, 40};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 1]; // 40&lt;br /&gt;
std::cout &amp;lt;&amp;lt; v[v.size() - 2]; // 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40]&lt;br /&gt;
&lt;br /&gt;
print(v[-1])  # 40&lt;br /&gt;
print(v[-2])  # 30 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 18. Slicing ==&lt;br /&gt;
&lt;br /&gt;
Python sequences support slicing directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; v = {&lt;br /&gt;
    10, 20, 30, 40, 50&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; part(&lt;br /&gt;
v.begin() + 1,&lt;br /&gt;
v.begin() + 4&lt;br /&gt;
); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [10, 20, 30, 40, 50]&lt;br /&gt;
&lt;br /&gt;
part = v[1:4] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The result is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
[20, 30, 40]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
General syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
sequence[start:stop:step]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// Requires an explicit loop or&lt;br /&gt;
// another algorithm.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
v = [0, 1, 2, 3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
print(v[1:4])   # [1, 2, 3]&lt;br /&gt;
print(v[:3])    # [0, 1, 2]&lt;br /&gt;
print(v[3:])    # [3, 4, 5]&lt;br /&gt;
print(v[::2])   # [0, 2, 4]&lt;br /&gt;
print(v[::-1])  # [5, 4, 3, 2, 1, 0] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 19. Membership testing ==&lt;br /&gt;
&lt;br /&gt;
Checking whether an element exists in a container is commonly performed using &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {10, 20, 30};&lt;br /&gt;
&lt;br /&gt;
if (std::find(&lt;br /&gt;
values.begin(),&lt;br /&gt;
values.end(),&lt;br /&gt;
20&lt;br /&gt;
) != values.end()) {&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [10, 20, 30]&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The opposite test uses &amp;lt;code&amp;gt;not in&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if 100 not in values:&lt;br /&gt;
    print(&amp;quot;Not found&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 20. Dictionaries ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt; is similar to a C++ associative container such as &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades;&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10;&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; grades[&amp;quot;Ana&amp;quot;]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {}&lt;br /&gt;
&lt;br /&gt;
grades[&amp;quot;Ana&amp;quot;] = 10&lt;br /&gt;
grades[&amp;quot;Bob&amp;quot;] = 8&lt;br /&gt;
&lt;br /&gt;
print(grades[&amp;quot;Ana&amp;quot;]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A dictionary can also be initialized directly:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt; grades = {&lt;br /&gt;
    {&amp;quot;Ana&amp;quot;, 10},&lt;br /&gt;
    {&amp;quot;Bob&amp;quot;, 8},&lt;br /&gt;
    {&amp;quot;Carol&amp;quot;, 9}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 10,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 8,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 9,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Iterating over keys and values:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; grade&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
    print(name, &amp;quot;:&amp;quot;, grade)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 21. Sets ==&lt;br /&gt;
&lt;br /&gt;
A Python &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; represents a collection containing unique values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; values = {&lt;br /&gt;
    10, 20, 30&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
values.insert(40);&lt;br /&gt;
&lt;br /&gt;
if (20 != values.end()) {&lt;br /&gt;
// conceptually test membership&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = {10, 20, 30}&lt;br /&gt;
&lt;br /&gt;
values.add(40)&lt;br /&gt;
&lt;br /&gt;
if 20 in values:&lt;br /&gt;
print(&amp;quot;Found&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A more correct C++ membership test would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (values.find(20) != values.end()) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Found\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets are particularly useful when:&lt;br /&gt;
&lt;br /&gt;
* duplicate values should be removed;&lt;br /&gt;
* fast membership tests are required;&lt;br /&gt;
* mathematical set operations are useful.&lt;br /&gt;
&lt;br /&gt;
== 22. Tuples ==&lt;br /&gt;
&lt;br /&gt;
Tuples are similar to lists, but they are immutable.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::tuple&amp;lt;int, double, std::string&amp;gt; data {&lt;br /&gt;
    10,&lt;br /&gt;
    3.14,&lt;br /&gt;
    &amp;quot;test&amp;quot;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
data = (10, 3.14, &amp;quot;test&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python uses tuples extensively for returning or unpacking multiple values.&lt;br /&gt;
&lt;br /&gt;
== 23. Functions ==&lt;br /&gt;
&lt;br /&gt;
Functions are declared using &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    return a + b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int result = add(2, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def add(a, b):&lt;br /&gt;
return a + b&lt;br /&gt;
&lt;br /&gt;
result = add(2, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python does not require parameter or return types to be declared.&lt;br /&gt;
&lt;br /&gt;
However, Python supports optional &amp;#039;&amp;#039;&amp;#039;type hints&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
double average(&lt;br /&gt;
    double a,&lt;br /&gt;
    double b&lt;br /&gt;
) {&lt;br /&gt;
    return (a + b) / 2.0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def average(&lt;br /&gt;
    a: float,&lt;br /&gt;
    b: float&lt;br /&gt;
) -&amp;gt; float:&lt;br /&gt;
    return (a + b) / 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Type hints do not normally enforce types at runtime.&lt;br /&gt;
&lt;br /&gt;
They provide information for:&lt;br /&gt;
&lt;br /&gt;
* the programmer;&lt;br /&gt;
* IDEs;&lt;br /&gt;
* static analysis tools;&lt;br /&gt;
* documentation.&lt;br /&gt;
&lt;br /&gt;
== 24. Default parameters ==&lt;br /&gt;
&lt;br /&gt;
Both C++ and Python support default parameter values.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void greet(&lt;br /&gt;
    const std::string&amp;amp; name = &amp;quot;World&amp;quot;&lt;br /&gt;
) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Hello, &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; name&lt;br /&gt;
              &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
greet();&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def greet(name=&amp;quot;World&amp;quot;):&lt;br /&gt;
print(f&amp;quot;Hello, {name}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
greet()&lt;br /&gt;
greet(&amp;quot;Alice&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 25. Named arguments ==&lt;br /&gt;
&lt;br /&gt;
Python allows function arguments to be specified by name.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void connect(&lt;br /&gt;
    std::string host,&lt;br /&gt;
    int port,&lt;br /&gt;
    bool secure&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
connect(&amp;quot;server.local&amp;quot;, 443, true); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def connect(host, port, secure):&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
connect(&lt;br /&gt;
host=&amp;quot;server.local&amp;quot;,&lt;br /&gt;
port=443,&lt;br /&gt;
secure=True&lt;br /&gt;
) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This can make calls with several parameters easier to understand.&lt;br /&gt;
&lt;br /&gt;
== 26. Returning multiple values ==&lt;br /&gt;
&lt;br /&gt;
In C++, multiple return values typically require a structure, tuple, pair or output parameters.&lt;br /&gt;
&lt;br /&gt;
Python commonly uses tuples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::pair&amp;lt;int, int&amp;gt; minmax(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    if (a &amp;lt; b)&lt;br /&gt;
        return {a, b};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return {b, a};&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
auto [minimum, maximum] = minmax(10, 3); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def minmax(a, b):&lt;br /&gt;
if a &amp;lt; b:&lt;br /&gt;
return a, b&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return b, a&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
minimum, maximum = minmax(10, 3) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is another example of tuple unpacking.&lt;br /&gt;
&lt;br /&gt;
== 27. Swapping variables ==&lt;br /&gt;
&lt;br /&gt;
A temporary variable is normally used in C.&lt;br /&gt;
&lt;br /&gt;
Python supports unpacking directly.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int temp = a;&lt;br /&gt;
a = b;&lt;br /&gt;
b = temp;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a, b = b, a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 28. Strings ==&lt;br /&gt;
&lt;br /&gt;
Python strings behave more like C++ &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt; objects than traditional C character arrays.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text.size();&lt;br /&gt;
std::cout &amp;lt;&amp;lt; text[0]; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(len(text))&lt;br /&gt;
print(text[0]) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
String concatenation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string first = &amp;quot;Hello&amp;quot;;&lt;br /&gt;
std::string second = &amp;quot;World&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::string result =&lt;br /&gt;
first + &amp;quot; &amp;quot; + second; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
first = &amp;quot;Hello&amp;quot;&lt;br /&gt;
second = &amp;quot;World&amp;quot;&lt;br /&gt;
&lt;br /&gt;
result = first + &amp;quot; &amp;quot; + second &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python strings provide many built-in operations:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::string text = &amp;quot;hello&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Usually requires algorithms,&lt;br /&gt;
// explicit transformations,&lt;br /&gt;
// or utility functions. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(text.upper())&lt;br /&gt;
print(text.lower())&lt;br /&gt;
print(text.startswith(&amp;quot;he&amp;quot;))&lt;br /&gt;
print(text.endswith(&amp;quot;lo&amp;quot;))&lt;br /&gt;
print(text.replace(&amp;quot;l&amp;quot;, &amp;quot;x&amp;quot;)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 29. Lists versus arrays: important semantic difference ==&lt;br /&gt;
&lt;br /&gt;
Consider assigning one collection to another.&lt;br /&gt;
&lt;br /&gt;
With normal C++ value semantics, copying a vector produces another vector.&lt;br /&gt;
&lt;br /&gt;
Python assignment behaves differently.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&lt;br /&gt;
b[0] = 100;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a[0];  // 1&lt;br /&gt;
std::cout &amp;lt;&amp;lt; b[0];  // 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
b = a&lt;br /&gt;
&lt;br /&gt;
b[0] = 100&lt;br /&gt;
&lt;br /&gt;
print(a[0])  # 100&lt;br /&gt;
print(b[0])  # 100 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This happens because:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
does not copy the list.&lt;br /&gt;
&lt;br /&gt;
Both names refer to the &amp;#039;&amp;#039;&amp;#039;same list object&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Conceptually:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
       +-------------+&lt;br /&gt;
a ---&amp;gt; | [1, 2, 3]   |&lt;br /&gt;
       +-------------+&lt;br /&gt;
          ^&lt;br /&gt;
          |&lt;br /&gt;
b --------+&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To explicitly create a shallow copy:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = a;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = a.copy()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
b = list(a)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This distinction is extremely important when using Python.&lt;br /&gt;
&lt;br /&gt;
== 30. Equality versus identity ==&lt;br /&gt;
&lt;br /&gt;
Python distinguishes between:&lt;br /&gt;
&lt;br /&gt;
* whether two objects contain equal values;&lt;br /&gt;
* whether two names refer to the exact same object.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; a = {1, 2, 3};&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; b = {1, 2, 3};&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; (a == b);  // true&lt;br /&gt;
&lt;br /&gt;
// Address comparison would be needed&lt;br /&gt;
// to determine object identity. &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = [1, 2, 3]&lt;br /&gt;
b = [1, 2, 3]&lt;br /&gt;
&lt;br /&gt;
print(a == b)  # True&lt;br /&gt;
print(a is b)  # False &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In Python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
==  tests value equality&lt;br /&gt;
is  tests object identity&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not use &amp;lt;code&amp;gt;is&amp;lt;/code&amp;gt; when you intend to compare values.&lt;br /&gt;
&lt;br /&gt;
A common correct use is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if value is None:&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 31. None and nullptr ==&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; represents the absence of a value.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Object* ptr = nullptr;&lt;br /&gt;
&lt;br /&gt;
if (ptr == nullptr) {&lt;br /&gt;
...&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = None&lt;br /&gt;
&lt;br /&gt;
if value is None:&lt;br /&gt;
... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 32. Exceptions ==&lt;br /&gt;
&lt;br /&gt;
Exception handling is conceptually similar.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    int value = std::stoi(text);&lt;br /&gt;
}&lt;br /&gt;
catch (const std::exception&amp;amp; e) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Invalid value\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
    value = int(text)&lt;br /&gt;
except ValueError:&lt;br /&gt;
    print(&amp;quot;Invalid value&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python generally uses exceptions rather than special error return values for many failure conditions.&lt;br /&gt;
&lt;br /&gt;
== 33. A first Pythonic transformation ==&lt;br /&gt;
&lt;br /&gt;
Consider creating a vector/list containing the squares of the numbers from 0 to 9.&lt;br /&gt;
&lt;br /&gt;
A direct translation from C++ could look like this:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — direct translation |&lt;br /&gt;
| ----------------------------- |&lt;br /&gt;
|                               |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = []&lt;br /&gt;
&lt;br /&gt;
for i in range(10):&lt;br /&gt;
squares.append(i * i) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python code is correct.&lt;br /&gt;
&lt;br /&gt;
However, Python provides a concise construct called a &amp;#039;&amp;#039;&amp;#039;list comprehension&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python — idiomatic |&lt;br /&gt;
| -------------------- |&lt;br /&gt;
|                      |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A condition can also be included.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; squares;&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
if (i % 2 == 0) {&lt;br /&gt;
squares.push_back(i * i);&lt;br /&gt;
}&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
squares = [&lt;br /&gt;
i * i&lt;br /&gt;
for i in range(10)&lt;br /&gt;
if i % 2 == 0&lt;br /&gt;
] &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
An important objective of this course is not only to write programs that work in Python, but to learn to write programs in a style appropriate for Python.&lt;br /&gt;
&lt;br /&gt;
== 34. Example: processing student grades ==&lt;br /&gt;
&lt;br /&gt;
Consider the following problem:&lt;br /&gt;
&lt;br /&gt;
* store the grades of several students;&lt;br /&gt;
* compute the average;&lt;br /&gt;
* print the students whose grade is greater than or equal to the average.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;unordered_map&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt; grades = {&lt;br /&gt;
{&amp;quot;Ana&amp;quot;, 9.5},&lt;br /&gt;
{&amp;quot;Bob&amp;quot;, 7.0},&lt;br /&gt;
{&amp;quot;Carol&amp;quot;, 8.5},&lt;br /&gt;
{&amp;quot;Dan&amp;quot;, 6.0}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
double sum = 0.0;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    sum += grade;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
double average = sum / grades.size();&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Average: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; average&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
for (const auto&amp;amp; [name, grade] : grades) {&lt;br /&gt;
    if (grade &amp;gt;= average) {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; name&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;: &amp;quot;&lt;br /&gt;
                  &amp;lt;&amp;lt; grade&lt;br /&gt;
                  &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
&amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
&amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
&amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
&amp;quot;Dan&amp;quot;: 6.0,&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
average = sum(grades.values()) / len(grades)&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;Average: {average:.2f}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
for name, grade in grades.items():&lt;br /&gt;
if grade &amp;gt;= average:&lt;br /&gt;
print(f&amp;quot;{name}: {grade}&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that the Python version uses existing language functionality rather than manually reproducing every operation.&lt;br /&gt;
&lt;br /&gt;
In particular:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sum(grades.values())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
expresses the intention directly.&lt;br /&gt;
&lt;br /&gt;
== 35. Exercise 1 — Translation ==&lt;br /&gt;
&lt;br /&gt;
Translate the following C++ program into Python.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values = {&lt;br /&gt;
12, 7, 18, 3, 24, 11&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
int sum = 0;&lt;br /&gt;
int count = 0;&lt;br /&gt;
&lt;br /&gt;
for (int value : values) {&lt;br /&gt;
    if (value % 2 == 0) {&lt;br /&gt;
        sum += value;&lt;br /&gt;
        count++;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Count: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; count&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Sum: &amp;quot;&lt;br /&gt;
          &amp;lt;&amp;lt; sum&lt;br /&gt;
          &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Your implementation:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# TODO&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
After implementing a direct translation, try to simplify the Python implementation.&lt;br /&gt;
&lt;br /&gt;
== 36. Exercise 2 — Statistics ==&lt;br /&gt;
&lt;br /&gt;
Write a Python program that:&lt;br /&gt;
&lt;br /&gt;
# reads a sequence of integer values;&lt;br /&gt;
&lt;br /&gt;
# stores them in a list;&lt;br /&gt;
&lt;br /&gt;
# prints the minimum value;&lt;br /&gt;
&lt;br /&gt;
# prints the maximum value;&lt;br /&gt;
&lt;br /&gt;
# prints the average;&lt;br /&gt;
&lt;br /&gt;
# prints how many values are above the average.&lt;br /&gt;
&lt;br /&gt;
The equivalent C++ structure might begin as follows:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;int&amp;gt; values;&lt;br /&gt;
&lt;br /&gt;
// Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
int minimum = ...;&lt;br /&gt;
int maximum = ...;&lt;br /&gt;
double average = ...; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = []&lt;br /&gt;
&lt;br /&gt;
# Read / initialize values.&lt;br /&gt;
&lt;br /&gt;
minimum = ...&lt;br /&gt;
maximum = ...&lt;br /&gt;
average = ... &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Useful Python functions include:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
len(...)&lt;br /&gt;
sum(...)&lt;br /&gt;
min(...)&lt;br /&gt;
max(...)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 37. Exercise 3 — Word frequency ==&lt;br /&gt;
&lt;br /&gt;
Given a sentence, count how many times each word occurs.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python is simple and python is powerful&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
should produce information equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python -&amp;gt; 2&lt;br /&gt;
is -&amp;gt; 2&lt;br /&gt;
simple -&amp;gt; 1&lt;br /&gt;
and -&amp;gt; 1&lt;br /&gt;
powerful -&amp;gt; 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A possible C++ data structure and its Python equivalent are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, int&amp;gt;&lt;br /&gt;
    frequencies;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
frequencies = {}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sentence.split()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to separate the sentence into words.&lt;br /&gt;
&lt;br /&gt;
== 38. Exercise 4 — Remove duplicates ==&lt;br /&gt;
&lt;br /&gt;
Given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
values = [4, 2, 7, 4, 2, 9, 7, 1]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
create a collection containing every distinct value.&lt;br /&gt;
&lt;br /&gt;
Think about the following C++ and Python data structures:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_set&amp;lt;int&amp;gt; unique_values;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
unique_values = set()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Try to solve the problem both:&lt;br /&gt;
&lt;br /&gt;
# using an explicit loop;&lt;br /&gt;
&lt;br /&gt;
# using Python&amp;#039;s built-in functionality.&lt;br /&gt;
&lt;br /&gt;
== 39. Exercise 5 — Small problem ==&lt;br /&gt;
&lt;br /&gt;
Write a program that receives a list of students and their grades and determines:&lt;br /&gt;
&lt;br /&gt;
* the class average;&lt;br /&gt;
* the student with the highest grade;&lt;br /&gt;
* all students who passed;&lt;br /&gt;
* all students whose grade is above the class average.&lt;br /&gt;
&lt;br /&gt;
Suggested representation:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
std::unordered_map&amp;lt;std::string, double&amp;gt;&lt;br /&gt;
    grades;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
grades = {&lt;br /&gt;
    &amp;quot;Ana&amp;quot;: 9.5,&lt;br /&gt;
    &amp;quot;Bob&amp;quot;: 7.0,&lt;br /&gt;
    &amp;quot;Carol&amp;quot;: 8.5,&lt;br /&gt;
    &amp;quot;Dan&amp;quot;: 4.0,&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Avoid using numeric indices unless an index is actually required.&lt;br /&gt;
&lt;br /&gt;
== 40. Creating a project environment ==&lt;br /&gt;
&lt;br /&gt;
Python applications commonly use a &amp;#039;&amp;#039;&amp;#039;virtual environment&amp;#039;&amp;#039;&amp;#039; to isolate project dependencies.&lt;br /&gt;
&lt;br /&gt;
Create a directory for the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir python-project&lt;br /&gt;
cd python-project&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a virtual environment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 -m venv .venv&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Linux/macOS:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source .venv/bin/activate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Activate it on Windows PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
.venv\Scripts\Activate.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command prompt will normally indicate that the environment is active.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
(.venv) student@computer:~/python-project$&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Packages installed using &amp;lt;code&amp;gt;pip&amp;lt;/code&amp;gt; will now be installed inside this environment.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip install requests&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To inspect installed packages:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip list&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 41. Recommended initial project structure ==&lt;br /&gt;
&lt;br /&gt;
For the first laboratories, the following structure is sufficient:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
python-project/&lt;br /&gt;
├── .venv/&lt;br /&gt;
├── main.py&lt;br /&gt;
├── README.md&lt;br /&gt;
└── .gitignore&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A basic &amp;lt;code&amp;gt;.gitignore&amp;lt;/code&amp;gt; should contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.venv/&lt;br /&gt;
__pycache__/&lt;br /&gt;
*.pyc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; commit the virtual environment to Git.&lt;br /&gt;
&lt;br /&gt;
== 42. C/C++ habits to reconsider in Python ==&lt;br /&gt;
&lt;br /&gt;
When transitioning from C/C++ to Python, the following patterns should generally be reconsidered.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
! C/C++ habit&lt;br /&gt;
&lt;br /&gt;
| ! Typical Python approach                |&lt;br /&gt;
| ---------------------------------------- |&lt;br /&gt;
| Iterate using an integer index           |&lt;br /&gt;
| Iterate directly over the objects        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually maintain counters               |&lt;br /&gt;
| Consider &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt;        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Search manually through a container      |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Manually calculate container size        |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;len()&amp;lt;/code&amp;gt;                   |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit temporary variable for swapping |&lt;br /&gt;
| Use tuple unpacking                      |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Explicit loop for simple transformations |&lt;br /&gt;
| Consider a comprehension                 |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Long output expressions                  |&lt;br /&gt;
| Use f-strings                            |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Copy assumed after assignment            |&lt;br /&gt;
| Remember that names refer to objects     |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| Check null using equality                |&lt;br /&gt;
| Use &amp;lt;code&amp;gt;is None&amp;lt;/code&amp;gt;                 |&lt;br /&gt;
| }                                        |&lt;br /&gt;
&lt;br /&gt;
The objective is not simply to translate C/C++ syntax into Python syntax.&lt;br /&gt;
&lt;br /&gt;
Consider:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C++ style translated literally&lt;br /&gt;
&lt;br /&gt;
| ! More idiomatic Python |&lt;br /&gt;
| ----------------------- |&lt;br /&gt;
|                         |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for i in range(len(values)):&lt;br /&gt;
    print(values[i])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for value in values:&lt;br /&gt;
    print(value)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Both programs are correct.&lt;br /&gt;
&lt;br /&gt;
The second expresses the programmer&amp;#039;s intention more directly.&lt;br /&gt;
&lt;br /&gt;
== 43. Summary ==&lt;br /&gt;
&lt;br /&gt;
The main mappings introduced in this laboratory are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python                                    |   |         |&lt;br /&gt;
| ------------------------------------------- | - | ------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;{ ... }&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| indentation                                 |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;       |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;                                      |   | &amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;!&amp;lt;/code&amp;gt;                              |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;not&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::vector&amp;lt;/code&amp;gt;                    |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_map&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt;                           |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::unordered_set&amp;lt;/code&amp;gt;             |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt;                            |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::tuple&amp;lt;/code&amp;gt;                     |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;tuple&amp;lt;/code&amp;gt;                          |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt;                      |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::cin&amp;lt;/code&amp;gt;                       |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;input()&amp;lt;/code&amp;gt;                        |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| indexed iteration                           |   |         |&lt;br /&gt;
| direct iteration / &amp;lt;code&amp;gt;enumerate()&amp;lt;/code&amp;gt; |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| manual search                               |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;in&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::pow(a, b)&amp;lt;/code&amp;gt;                 |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;a ** b&amp;lt;/code&amp;gt;                         |   |         |&lt;br /&gt;
| -                                           |   |         |&lt;br /&gt;
| integer division                            |   |         |&lt;br /&gt;
| &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;                             |   |         |&lt;br /&gt;
| }                                           |   |         |&lt;br /&gt;
&lt;br /&gt;
The central idea to retain from this laboratory is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Python should not be treated merely as C++ with different syntax. Python provides its own abstractions and idioms, and using them generally produces simpler and more readable programs.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 44. Preparation for Lab 2 ==&lt;br /&gt;
&lt;br /&gt;
Before the next laboratory:&lt;br /&gt;
&lt;br /&gt;
# complete all exercises from this laboratory;&lt;br /&gt;
&lt;br /&gt;
# ensure Python 3 is installed;&lt;br /&gt;
&lt;br /&gt;
# create a virtual environment successfully;&lt;br /&gt;
&lt;br /&gt;
# create a Git repository for the semester project;&lt;br /&gt;
&lt;br /&gt;
# become familiar with lists, dictionaries, tuples and sets;&lt;br /&gt;
&lt;br /&gt;
# review the concepts of classes, objects, inheritance and composition from C++.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Lab 2&amp;#039;&amp;#039;&amp;#039;, these concepts will be revisited from the perspective of Python&amp;#039;s object model and Pythonic object-oriented programming.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8358</id>
		<title>Lab 1</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1&amp;diff=8358"/>
		<updated>2026-09-14T05:20:38Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: 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...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Lab 1 — Python for C/C++ Programmers =&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
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++.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
After completing this laboratory, you should be able to:&lt;br /&gt;
&lt;br /&gt;
* understand the basic syntax of Python;&lt;br /&gt;
* recognize the main differences between Python and C/C++;&lt;br /&gt;
* work with Python&amp;#039;s fundamental data types;&lt;br /&gt;
* use conditional statements and loops;&lt;br /&gt;
* work with lists, dictionaries, sets and tuples;&lt;br /&gt;
* define and call functions;&lt;br /&gt;
* understand basic Python object/reference semantics;&lt;br /&gt;
* recognize several common Python idioms;&lt;br /&gt;
* create and execute a simple Python project.&lt;br /&gt;
&lt;br /&gt;
Throughout this laboratory, equivalent or similar C/C++ and Python code will be presented side by side.&lt;br /&gt;
&lt;br /&gt;
**TOC**&lt;br /&gt;
&lt;br /&gt;
== 1. Running a Python program ==&lt;br /&gt;
&lt;br /&gt;
A C/C++ program normally has to be compiled before it is executed. Python programs are usually executed directly by the Python interpreter.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Hello, world!\n&amp;quot;;&lt;br /&gt;
return 0;&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(&amp;quot;Hello, world!&amp;quot;) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A Python source file normally uses the extension:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be executed using:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 hello.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unlike C/C++, Python does not require a special &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
However, larger Python programs commonly use:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | C/C++&lt;br /&gt;
! style=&amp;quot;width:50%;&amp;quot; | Python&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;Program started\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
return 0;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
} &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def main():&lt;br /&gt;
print(&amp;quot;Program started&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
if **name** == &amp;quot;**main**&amp;quot;:&lt;br /&gt;
main() &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Python version is not mandatory, but this pattern will become useful when programs are split into multiple modules.&lt;br /&gt;
&lt;br /&gt;
== 2. Blocks and indentation ==&lt;br /&gt;
&lt;br /&gt;
In C and C++, braces delimit blocks of code.&lt;br /&gt;
&lt;br /&gt;
Python uses &amp;#039;&amp;#039;&amp;#039;indentation&amp;#039;&amp;#039;&amp;#039; instead.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (x &amp;gt; 10) {&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Large\n&amp;quot;;&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;Value: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
    print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
    print(&amp;quot;Value:&amp;quot;, x)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Indentation in Python is part of the language syntax.&lt;br /&gt;
&lt;br /&gt;
The standard convention is &amp;#039;&amp;#039;&amp;#039;4 spaces per indentation level&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
For example, the following code is incorrect:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
if x &amp;gt; 10:&lt;br /&gt;
print(&amp;quot;Large&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python will report an &amp;lt;code&amp;gt;IndentationError&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== 3. Variables and types ==&lt;br /&gt;
&lt;br /&gt;
C/C++ variables have a declared static type.&lt;br /&gt;
&lt;br /&gt;
Python variables do not require explicit type declarations.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int count = 10;&lt;br /&gt;
double temperature = 23.5;&lt;br /&gt;
bool enabled = true;&lt;br /&gt;
std::string name = &amp;quot;Alice&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
count = 10&lt;br /&gt;
temperature = 23.5&lt;br /&gt;
enabled = True&lt;br /&gt;
name = &amp;quot;Alice&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python is &amp;#039;&amp;#039;&amp;#039;dynamically typed&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
This means that a variable name can later refer to an object of another type.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
// Not allowed:&lt;br /&gt;
// value = &amp;quot;hello&amp;quot;; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
value = &amp;quot;hello&amp;quot; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This does &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; mean that Python has no types.&lt;br /&gt;
&lt;br /&gt;
Objects still have types:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int value = 10;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; typeid(value).name(); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
value = 10&lt;br /&gt;
&lt;br /&gt;
print(type(value)) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python will produce:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common basic types ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! C/C++ type&lt;br /&gt;
! Python equivalent&lt;br /&gt;
&lt;br /&gt;
| ! Example                                |&lt;br /&gt;
| ---------------------------------------- |&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;                         |&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;                         |&lt;br /&gt;
| &amp;lt;code&amp;gt;42&amp;lt;/code&amp;gt;                          |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt; / &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; |&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt;                       |&lt;br /&gt;
| &amp;lt;code&amp;gt;3.14&amp;lt;/code&amp;gt;                        |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;                        |&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;                        |&lt;br /&gt;
| &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;    |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt;                        |&lt;br /&gt;
| no separate character type               |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;A&amp;quot;&amp;lt;/code&amp;gt;                         |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| &amp;lt;code&amp;gt;std::string&amp;lt;/code&amp;gt;                 |&lt;br /&gt;
| &amp;lt;code&amp;gt;str&amp;lt;/code&amp;gt;                         |&lt;br /&gt;
| &amp;lt;code&amp;gt;&amp;quot;Hello&amp;quot;&amp;lt;/code&amp;gt;                     |&lt;br /&gt;
| -                                        |&lt;br /&gt;
| &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;                     |&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;                        |&lt;br /&gt;
| &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;                        |&lt;br /&gt;
| }                                        |&lt;br /&gt;
&lt;br /&gt;
== 4. Constants ==&lt;br /&gt;
&lt;br /&gt;
C++ can enforce that a value cannot be modified using &amp;lt;code&amp;gt;const&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python does not enforce constants at language level.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
const double PI = 3.1415926535;&lt;br /&gt;
const int MAX_USERS = 100;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
PI = 3.1415926535&lt;br /&gt;
MAX_USERS = 100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
By convention, Python names that should be treated as constants are written using uppercase letters.&lt;br /&gt;
&lt;br /&gt;
Nothing prevents the programmer from changing them.&lt;br /&gt;
&lt;br /&gt;
== 5. Arithmetic operators ==&lt;br /&gt;
&lt;br /&gt;
Most arithmetic operators are identical.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
int sum  = a + b;&lt;br /&gt;
int diff = a - b;&lt;br /&gt;
int prod = a * b;&lt;br /&gt;
int div  = a / b;&lt;br /&gt;
int rem  = a % b; &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
sum_ = a + b&lt;br /&gt;
diff = a - b&lt;br /&gt;
prod = a * b&lt;br /&gt;
div = a // b&lt;br /&gt;
rem = a % b &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
There is an important difference between &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;//&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 10;&lt;br /&gt;
int b = 3;&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; a / b;   // 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a = 10&lt;br /&gt;
b = 3&lt;br /&gt;
&lt;br /&gt;
print(a / b)   # 3.3333333333333335&lt;br /&gt;
print(a // b)  # 3 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Python also provides an exponentiation operator:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; table-layout:fixed;&amp;quot;&lt;br /&gt;
! C/C++&lt;br /&gt;
&lt;br /&gt;
| ! Python |&lt;br /&gt;
| -------- |&lt;br /&gt;
|          |&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;cmath&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double result = std::pow(2, 8); &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
result = 2 ** 8 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== 6. Printing values ==&lt;br /&gt;
&lt;br /&gt;
Output using &amp;lt;code&amp;gt;std::cout&amp;lt;/code&amp;gt; is replaced by the built-in &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width:100%; tab&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Python_Project&amp;diff=8357</id>
		<title>Python Project</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Python_Project&amp;diff=8357"/>
		<updated>2026-09-14T05:20:27Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: Pagină nouă: Lab 1&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Lab 1]]&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Pagina_principal%C4%83&amp;diff=8356</id>
		<title>Pagina principală</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Pagina_principal%C4%83&amp;diff=8356"/>
		<updated>2026-09-14T05:20:18Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Platforme de aplicatii sau laborator */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Scopul acestei pagini este de a centraliza platformele de laborator dar și toate resursele auxiliare pentru cursurile și laboratoarele ținute în cadrul departamentului de Dispozitive, Circuite și Arhitecturi Electronice. &lt;br /&gt;
&lt;br /&gt;
== Materiale de curs ==&lt;br /&gt;
* [[Programarea Calculatoarelor (curs seria C,F)|Programarea Calculatoarelor]]&lt;br /&gt;
* [[Circuite Integrate Digitale (curs)|Circuite Integrate Digitale]]&lt;br /&gt;
* [[Dispozitive Semiconductoare de Putere (curs)|Dispozitive Semiconductoare de Putere]]&lt;br /&gt;
* [[Structuri de Date și Algoritmi (curs)|Structuri de Date și Algoritmi]]&lt;br /&gt;
* [[Senzori si Circuite de Conditionare a Semnalelor (curs)|Senzori si Circuite de Conditionare a Semnalelor]]&lt;br /&gt;
&amp;lt;!--* [[Programarea Calculatoarelor (curs)|Programarea Calculatoarelor]]--&amp;gt;&lt;br /&gt;
* [[Programare Orientată Obiect (curs)|Programare Orientată Obiect]]&lt;br /&gt;
* [[Tehnici de proiectare VLSI (curs)|Tehnici de proiectare VLSI]]&lt;br /&gt;
&lt;br /&gt;
== Platforme de aplicatii sau laborator ==&lt;br /&gt;
* [[Arhitectura Sistemelor de Calcul]]&lt;br /&gt;
* [[Circuite Integrate Analogice (laborator)|Circuite Integrate Analogice]]&lt;br /&gt;
* [[Circuite Integrate Digitale | Circuite Integrate Digitale]] &amp;lt;&amp;gt; [[Digital Integrated Circuits (app) | Digital Integrated Circuits (app)]]&lt;br /&gt;
* [[Electronica Digitala | Electronica Digitala]]&lt;br /&gt;
* [[Dispozitive Semiconductoare de Putere (laborator)|Dispozitive Semiconductoare de Putere]]&lt;br /&gt;
* [[Dispozitive Electronice(laborator)|Dispozitive Electronice]]&lt;br /&gt;
* [[Circuite Electronice(laborator)|Circuite Electronice]]&lt;br /&gt;
* [[Programare Orientată Obiect (Java)]]&lt;br /&gt;
* [[Programare Orientată pe Obiecte (C++)]]&lt;br /&gt;
* [[Structuri de Date și Algoritmi]]&lt;br /&gt;
* [[Senzori si Circuite de Conditionare a Semnalelor (laborator)|SCCS]]&lt;br /&gt;
* [[Programarea Calculatoarelor (laborator)|Programarea Calculatoarelor]]&lt;br /&gt;
* [[Tehnici de proiectare VLSI (laborator)|Tehnici de proiectare VLSI]]&lt;br /&gt;
* [[Performance analysis and optimization]]&lt;br /&gt;
* [https://users.dcae.pub.ro/~zhascsi/courses/dsd/dsd.html Digital Systems Design Project]&lt;br /&gt;
* [[Reconfigurable Computing]]&lt;br /&gt;
* [[Operating Systems]]&lt;br /&gt;
* [[Python Project]]&lt;br /&gt;
&lt;br /&gt;
== Materiale vechi CID ==&lt;br /&gt;
* [[Circuite Integrate Digitale (2023)]]&lt;br /&gt;
* [[Circuite_Integrate_Digitale_(2021)|Circuite Integrate Digitale (2021)]]&lt;br /&gt;
* [[Circuite integrate digitale (seminar)|Circuite Integrate Digitale]] * [[Digital Integrated Circuits (seminar)|Digital Integrated Circuits]]&lt;br /&gt;
* [[Circuite Integrate Digitale (laborator)|Circuite Integrate Digitale]] * [[Digital Integrated Circuits (lab)|Digital Integrated Circuits]]&lt;br /&gt;
&lt;br /&gt;
== Materiale pentru proiecte ==&lt;br /&gt;
* [[Project SCCS - Senzori si Circuite de Conditionare a Semnalelor]]&lt;br /&gt;
* [[Proiect 1 - Dispozitive si Circuite Electronice|Dispozitive si Circuite Electronice]]&lt;br /&gt;
* [[ACES - Vivado remote run]]&lt;br /&gt;
&lt;br /&gt;
== Regulamente ==&lt;br /&gt;
* [[Regulament protectia muncii]]&lt;br /&gt;
* [[Regulamentul laboratoarelor POO și ASC]]&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Circuite_Integrate_Digitale&amp;diff=8324</id>
		<title>Circuite Integrate Digitale</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Circuite_Integrate_Digitale&amp;diff=8324"/>
		<updated>2026-04-08T11:20:55Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Scopul laboratorului */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Scopul laboratorului ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Laboratorul de circuite integrate digitale vine ca o completare practica a cursului de CID. &lt;br /&gt;
Prin definitie, acesta se va axa pe elemente practice de simulare, sinteza si testare a circuitelor ce sunt prezentate la curs. &lt;br /&gt;
&lt;br /&gt;
Acest laborator se bazeaza pe limbajul &amp;quot;SystemVerilog&amp;quot;, acesta fiind unul din cele 2 limbaje de descriere hardware folosite in industrie la ora actuala (celalalt fiind &amp;quot;VHDL&amp;quot;). &amp;quot;SystemVerilog&amp;quot; vine ca o completare la &amp;quot;Verilog&amp;quot;, introducand numeroase elemente ce ajuta in special testarea circuitelor, dar si unele imbunatatiri pentru procesul de proiectare. &lt;br /&gt;
&lt;br /&gt;
Platformele sunt structurate astfel incat sa contina cateva elemente de teorie minimala, un exemplu de circuit descris in cod Verilog si apoi exercitii pe care sa le rezolvati in ora de aplicatii (simulare/testare pe placa FPGA) si acasa (simulare). &lt;br /&gt;
&lt;br /&gt;
In laborator se va lucra in Vivado, program al companiei AMD (Xilinx), pe o placă de dezvoltare Boolean Board. &lt;br /&gt;
&lt;br /&gt;
Circuitele digitale sunt o parte fundamentala a electronici moderne, cu numeroase oportunitati in industrie, atat in design cat si in verificarea circuitelor.&lt;br /&gt;
&lt;br /&gt;
Pe langa ce veti invata in acest laborator, limbajul ofera si capabilitati mai avansate pentru a descrie mai eficient circuitele dorite, cat si pentru testarea acestora. Sintaxa nu se termina cu ce invatati aici si pentru cei pasionati, nu ezitati sa va contactati cadrul didactic cu intrebari.&lt;br /&gt;
&lt;br /&gt;
Speram ca o sa va placa, veti invata si vi se va parea interesant ce veti vedea in orele de aplicatii ce urmeaza. Spor.&lt;br /&gt;
&lt;br /&gt;
== Tutoriale și documentații ==&lt;br /&gt;
&lt;br /&gt;
Programul Vivado este gratuit (necesita cont, dar este gratis) si poate fi descarcat si instalat urmand tutorialul de aici:&lt;br /&gt;
# [[Tutorial instalare Vivado]]&lt;br /&gt;
# [[Tutorial Vivado]].&lt;br /&gt;
# [[FPGA - Introducere]].&lt;br /&gt;
# [https://www.realdigital.org/doc/02013cd17602c8af749f00561f88ae21 Boolean Board - user manual]&lt;br /&gt;
# [[Boolean Board - Pinout]].&lt;br /&gt;
# [https://www.tulembedded.com/FPGA/ProductsPYNQ-Z2.html Pynq-Z2 - pagina oficiala]&lt;br /&gt;
# [https://dpoauwgwqsy2x.cloudfront.net/Download/pynqz2_user_manual_v1_0.pdf Pynq-Z2 - user manual]&lt;br /&gt;
# [[Pynq-Z2 - Pinout]].&lt;br /&gt;
	&lt;br /&gt;
O lista de link-uri utile poate fi gasita aici:&lt;br /&gt;
# [[Introducere. SystemVerilog HDL]] (Sintaxa [[SystemVerilog]])&lt;br /&gt;
# [https://www.asic-world.com/digital/index.html Asic-world - digital]&lt;br /&gt;
# [https://www.asic-world.com/verilog/index.html Asic-world - verilog]&lt;br /&gt;
# [https://nandgame.com/ Joc online: Nandgame]	&lt;br /&gt;
# [https://circuitverse.org/simulator Simulator/joc online si grafic de circuite: Circuitverse]	&lt;br /&gt;
# [https://falstad.com/circuit/ Simulator online si grafic de circuite (analog+digital): Falstad]&lt;br /&gt;
# [https://github.com/hneemann/Digital Simulator grafic de circuite digitale: hneemann-digital]&lt;br /&gt;
# [https://play.google.com/store/apps/details?id=com.ViacheslavRud.Circuit&amp;amp;hl=en_US&amp;amp;gl=US Joc android - Make it True: Solve the Circuit]&lt;br /&gt;
# [https://www.edaplayground.com/ Simulatoare online (mod text): Edaplayground]&lt;br /&gt;
# [https://wavedrom.com/ Utilitar desenare forme de unda]&lt;br /&gt;
# [https://www.draw.io/ Utilitar desenare diagrame]&lt;br /&gt;
# [[Domenii conexe]]&lt;br /&gt;
&lt;br /&gt;
== Lucrări de laborator ==&lt;br /&gt;
&lt;br /&gt;
# [[CID_aplicatii_1 : Generare de forme de unda]]&lt;br /&gt;
# [[CID_aplicatii_2 : Instantiere si porti logice]]&lt;br /&gt;
# [[CID_aplicatii_3 : Circuite combinationale elementare]]&lt;br /&gt;
# [[CID_aplicatii_4 : Memoria ROM]]&lt;br /&gt;
# [[CID_aplicatii_5 : Exercitii cu circuite combinationale]]&lt;br /&gt;
# CID_aplicatii_6 : Lucrarea 1 - circuite combinationale&lt;br /&gt;
# [[CID_aplicatii_7 : Circuite secventiale elementare]]&lt;br /&gt;
# [[CID_aplicatii_8 : Registre si memorii RAM]]&lt;br /&gt;
# [[CID_aplicatii_9 : Numaratorul]]&lt;br /&gt;
# [[CID_aplicatii_10 : Aplicatii cu numaratoare]]&lt;br /&gt;
# [[CID_aplicatii_11 : Automate finite]]&lt;br /&gt;
# [[CID_aplicatii_12 : Exercitii cu circuite secventiale]]&lt;br /&gt;
# CID_aplicatii_13 : Lucrarea 2 - circuite secventiale&lt;br /&gt;
# [[CID_aplicatii_14 : Circuite digitale complexe]]&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=FPGA_Student_Classroom_tutorial&amp;diff=8294</id>
		<title>FPGA Student Classroom tutorial</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=FPGA_Student_Classroom_tutorial&amp;diff=8294"/>
		<updated>2026-03-17T16:21:43Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* FAQ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== FPGA Student Classroom Access Tutorial ==&lt;br /&gt;
&lt;br /&gt;
This is a guide that aims to show how to connect to the classroom resources remotely, in order to access the software and hardware resources (FPGAs) available at the university. The goal is to create a remote desktop session to one of the computers in the lab. This session can then be used to run software (e.g., Vivado) and to program hardware FPGAs.&lt;br /&gt;
&lt;br /&gt;
Before you can connect to the classroom cluster, you will need to obtain the following from a system administrator:&lt;br /&gt;
&lt;br /&gt;
* A VPN configuration file (e.g., a415-myname.conf)&lt;br /&gt;
* A username and password&lt;br /&gt;
* An assigned workstation and FPGA board (e.g., station07 &amp;amp; pynq07)&lt;br /&gt;
&lt;br /&gt;
The connection process consists of the following steps:&lt;br /&gt;
&lt;br /&gt;
# Connect via VPN to the cluster network.&lt;br /&gt;
# Connect to your workstation via SSH and create a remote desktop (VNC) instance.&lt;br /&gt;
# Connect to the remote desktop instance.&lt;br /&gt;
&lt;br /&gt;
== Step 1 - VPN ==&lt;br /&gt;
&lt;br /&gt;
To connect to the cluster network via VPN, you will need the provided VPN configuration file (e.g., a415-myname.conf) (ask your system administrator if you don&amp;#039;t have it) and a WireGuard VPN client.&lt;br /&gt;
&lt;br /&gt;
For a full guide on how to use the WireGuard VPN, check [https://wiki.dcae.pub.ro/index.php/VPN_Tutorial_-_en this page].&lt;br /&gt;
&lt;br /&gt;
== Step 2 - SSH ==&lt;br /&gt;
&lt;br /&gt;
In order to connect to your workstation via SSH to open the remote desktop instance, you will need a terminal application or a dedicated SSH client.&lt;br /&gt;
&lt;br /&gt;
* **Windows 11** comes with Windows Terminal. Windows 10 users can download the [https://apps.microsoft.com/detail/9n0dx20hk701 Windows Terminal] from the Microsoft Store or use the older Command Prompt application.&lt;br /&gt;
* **macOS** has a pre-installed Terminal application. [https://iterm2.com/ iTerm2] is a popular community alternative with more features.&lt;br /&gt;
&lt;br /&gt;
First, connect to the workstation using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ssh myname@station07&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If this is your first time logging in, you will be prompted to change your initial password:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
(myname@station07) Password: &lt;br /&gt;
Password expired. Change your password now.&lt;br /&gt;
(myname@station07) Current Password: &lt;br /&gt;
(myname@station07) New password: &lt;br /&gt;
(myname@station07) Retype new password:&lt;br /&gt;
&lt;br /&gt;
myname@station07:~$&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Replace `myname` and `station07` with your assigned username and workstation.&lt;br /&gt;
&lt;br /&gt;
Once connected, launch a VNC instance with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
vncserver -localhost no&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If this is your first time starting a VNC instance, you will be prompted to set a password. You will also need to note the **VNC port** and **X display number** from the output. If you forget these numbers, you can retrieve them with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
vncserver -list&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To stop the VNC session, use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
vncserver -kill :3&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Replace `:3` with your actual X display number.&lt;br /&gt;
&lt;br /&gt;
== Step 3 - VNC ==&lt;br /&gt;
&lt;br /&gt;
To connect to your workstation using VNC, you will need a VNC client. **TigerVNC** is a popular choice, though others like RealVNC, TightVNC, and Remmina are also available.&lt;br /&gt;
&lt;br /&gt;
* **Windows:** Download TigerVNC from [https://sourceforge.net/projects/tigervnc/files/stable/1.15.0/ here].&lt;br /&gt;
* **macOS:** Download TigerVNC from [https://tigervnc.macupdate.com/ here].&lt;br /&gt;
* **Linux:** Install TigerVNC via Flatpak:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
flatpak install flathub org.tigervnc.vncviewer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once installed, enter your workstation name followed by a colon and the VNC port (e.g., `station07:5903`).&lt;br /&gt;
&lt;br /&gt;
[[File:Tigervnc.png|Enter your workstation and port here.]]&lt;br /&gt;
&lt;br /&gt;
Click **Connect**, then enter your **VNC session password** (not your account password).&lt;br /&gt;
&lt;br /&gt;
[[File:TigerVNC_password.png|Enter your VNC password here.]]&lt;br /&gt;
&lt;br /&gt;
If successful, you will see your remote desktop.&lt;br /&gt;
&lt;br /&gt;
[[File:GNOME_desktop.png|You are now connected!]]&lt;br /&gt;
&lt;br /&gt;
You can now open a terminal and launch Vivado:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
vivado&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Vivado_open.png|Vivado running on remote desktop.]]&lt;br /&gt;
&lt;br /&gt;
== FAQ ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Q: I cannot connect to my VNC session.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
A: Connect via SSH and check with `vncserver -list`. Ensure the VNC server was started with `-localhost no`.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Q: I cannot connect via SSH; my workstation appears to be powered off.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
A: Some students turn off the stations despite instructions. Contact a system administrator to power it on remotely.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Q: I can connect to VNC, but I see a login screen and an &amp;quot;Authentication error&amp;quot; message.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
A: This is a [https://askubuntu.com/questions/1224957/i-cannot-log-in-a-vnc-session-after-the-screen-locks-authentification-error known bug] in TigerVNC. While connected via VNC, open an SSH session and run&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
loginctl list-sessions&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to see the open sessions. Identify your session and run&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
loginctl unlock-session &amp;lt;session-id&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
replacing session-id with the id of your session.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039; Q: Is there any fix for the login screen bug? &amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
A: Run this command in a SSH connection (different terminal):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gsettings set org.gnome.desktop.session idle-delay 0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=FPGA_Student_Classroom_tutorial&amp;diff=8293</id>
		<title>FPGA Student Classroom tutorial</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=FPGA_Student_Classroom_tutorial&amp;diff=8293"/>
		<updated>2026-03-17T16:21:08Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* FAQ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== FPGA Student Classroom Access Tutorial ==&lt;br /&gt;
&lt;br /&gt;
This is a guide that aims to show how to connect to the classroom resources remotely, in order to access the software and hardware resources (FPGAs) available at the university. The goal is to create a remote desktop session to one of the computers in the lab. This session can then be used to run software (e.g., Vivado) and to program hardware FPGAs.&lt;br /&gt;
&lt;br /&gt;
Before you can connect to the classroom cluster, you will need to obtain the following from a system administrator:&lt;br /&gt;
&lt;br /&gt;
* A VPN configuration file (e.g., a415-myname.conf)&lt;br /&gt;
* A username and password&lt;br /&gt;
* An assigned workstation and FPGA board (e.g., station07 &amp;amp; pynq07)&lt;br /&gt;
&lt;br /&gt;
The connection process consists of the following steps:&lt;br /&gt;
&lt;br /&gt;
# Connect via VPN to the cluster network.&lt;br /&gt;
# Connect to your workstation via SSH and create a remote desktop (VNC) instance.&lt;br /&gt;
# Connect to the remote desktop instance.&lt;br /&gt;
&lt;br /&gt;
== Step 1 - VPN ==&lt;br /&gt;
&lt;br /&gt;
To connect to the cluster network via VPN, you will need the provided VPN configuration file (e.g., a415-myname.conf) (ask your system administrator if you don&amp;#039;t have it) and a WireGuard VPN client.&lt;br /&gt;
&lt;br /&gt;
For a full guide on how to use the WireGuard VPN, check [https://wiki.dcae.pub.ro/index.php/VPN_Tutorial_-_en this page].&lt;br /&gt;
&lt;br /&gt;
== Step 2 - SSH ==&lt;br /&gt;
&lt;br /&gt;
In order to connect to your workstation via SSH to open the remote desktop instance, you will need a terminal application or a dedicated SSH client.&lt;br /&gt;
&lt;br /&gt;
* **Windows 11** comes with Windows Terminal. Windows 10 users can download the [https://apps.microsoft.com/detail/9n0dx20hk701 Windows Terminal] from the Microsoft Store or use the older Command Prompt application.&lt;br /&gt;
* **macOS** has a pre-installed Terminal application. [https://iterm2.com/ iTerm2] is a popular community alternative with more features.&lt;br /&gt;
&lt;br /&gt;
First, connect to the workstation using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ssh myname@station07&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If this is your first time logging in, you will be prompted to change your initial password:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
(myname@station07) Password: &lt;br /&gt;
Password expired. Change your password now.&lt;br /&gt;
(myname@station07) Current Password: &lt;br /&gt;
(myname@station07) New password: &lt;br /&gt;
(myname@station07) Retype new password:&lt;br /&gt;
&lt;br /&gt;
myname@station07:~$&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Replace `myname` and `station07` with your assigned username and workstation.&lt;br /&gt;
&lt;br /&gt;
Once connected, launch a VNC instance with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
vncserver -localhost no&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If this is your first time starting a VNC instance, you will be prompted to set a password. You will also need to note the **VNC port** and **X display number** from the output. If you forget these numbers, you can retrieve them with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
vncserver -list&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To stop the VNC session, use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
vncserver -kill :3&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Replace `:3` with your actual X display number.&lt;br /&gt;
&lt;br /&gt;
== Step 3 - VNC ==&lt;br /&gt;
&lt;br /&gt;
To connect to your workstation using VNC, you will need a VNC client. **TigerVNC** is a popular choice, though others like RealVNC, TightVNC, and Remmina are also available.&lt;br /&gt;
&lt;br /&gt;
* **Windows:** Download TigerVNC from [https://sourceforge.net/projects/tigervnc/files/stable/1.15.0/ here].&lt;br /&gt;
* **macOS:** Download TigerVNC from [https://tigervnc.macupdate.com/ here].&lt;br /&gt;
* **Linux:** Install TigerVNC via Flatpak:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
flatpak install flathub org.tigervnc.vncviewer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once installed, enter your workstation name followed by a colon and the VNC port (e.g., `station07:5903`).&lt;br /&gt;
&lt;br /&gt;
[[File:Tigervnc.png|Enter your workstation and port here.]]&lt;br /&gt;
&lt;br /&gt;
Click **Connect**, then enter your **VNC session password** (not your account password).&lt;br /&gt;
&lt;br /&gt;
[[File:TigerVNC_password.png|Enter your VNC password here.]]&lt;br /&gt;
&lt;br /&gt;
If successful, you will see your remote desktop.&lt;br /&gt;
&lt;br /&gt;
[[File:GNOME_desktop.png|You are now connected!]]&lt;br /&gt;
&lt;br /&gt;
You can now open a terminal and launch Vivado:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
vivado&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Vivado_open.png|Vivado running on remote desktop.]]&lt;br /&gt;
&lt;br /&gt;
== FAQ ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Q: I cannot connect to my VNC session.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
A: Connect via SSH and check with `vncserver -list`. Ensure the VNC server was started with `-localhost no`.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Q: I cannot connect via SSH; my workstation appears to be powered off.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
A: Some students turn off the stations despite instructions. Contact a system administrator to power it on remotely.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Q: I can connect to VNC, but I see a login screen and an &amp;quot;Authentication error&amp;quot; message.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
A: This is a [https://askubuntu.com/questions/1224957/i-cannot-log-in-a-vnc-session-after-the-screen-locks-authentification-error known bug] in TigerVNC. While connected via VNC, open an SSH session and run&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
loginctl list-sessions&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to see the open sessions. Identify your session and run&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
loginctl unlock-session &amp;lt;session-id&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
replacing session-id with the id of your session.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039; Q: Is there any fix for the login screen bug? &amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
A: Run this command in a SSH connection (different terminal):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gsettings set org.gnome.desktop.session idle-delay 0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=8006</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=8006"/>
		<updated>2025-03-25T20:16:39Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
For this lab, we will be using the AMD Vitis tool. In order to access the tool, login into your assigned machine using tigervnc-viewer (please see the [https://wiki.dcae.pub.ro/index.php/FPGA_Student_Classroom_tutorial tutorial].&lt;br /&gt;
&lt;br /&gt;
Once the GUI is opened, open a terminal and type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source /nfs/amd/Vitis/2024.2/settings64.sh&lt;br /&gt;
vitis&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Vitis window looks like this:&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Vitis.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
In order to create a new project, click on Create Component -&amp;gt; Create Empty HLS Component.&lt;br /&gt;
[[Fișier:Vitis hls new prj.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
Follow the steps shown in the Vitis executable. At some point, you will have to pick a name for the top function. Beware that the function name should also appear in the declaration of it.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Name and Location&amp;#039;&amp;#039;&amp;#039; tab, choose a name for your HLS component and a location. In the &amp;#039;&amp;#039;&amp;#039;Configuration File&amp;#039;&amp;#039;&amp;#039;, leave everything as is. In &amp;#039;&amp;#039;&amp;#039;Source Files&amp;#039;&amp;#039;&amp;#039;, write your desired top level function under the textbox labeled &amp;#039;&amp;#039;&amp;#039;Top Function&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:space-between;&amp;quot;&amp;gt;&lt;br /&gt;
[[Fișier:New prj.png|800px]]&lt;br /&gt;
[[Fișier:Source Files.png|800px]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Under the hardware section, select &amp;#039;&amp;#039;&amp;#039;xc7z020iclg400-1l&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
In the summary section, two pieces of information need to be provided:&lt;br /&gt;
* expected clock speed at which the circuit should operate: this constraints the tool to create a circuit that can function at that specific frequency&lt;br /&gt;
* clock uncertainty, how much the clock can vary around the expected value.&lt;br /&gt;
[[Fișier:Summary.png|800px]]&lt;br /&gt;
&lt;br /&gt;
Next step, Settings, lets the user select the target operating frequency and clock uncertainty. For a safe design, we will choose 10ns for operating frequency and 2ns for clock uncertainty.&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Clock summary.png.png|800px]]&lt;br /&gt;
&lt;br /&gt;
Now press next and finish. The project should be generated.&lt;br /&gt;
&lt;br /&gt;
To create new sources, right click on &amp;#039;&amp;#039;&amp;#039;Sources&amp;#039;&amp;#039;&amp;#039;, then &amp;#039;&amp;#039;&amp;#039;New Source File&amp;#039;&amp;#039;&amp;#039;. To add C simulation testbench, right click on &amp;#039;&amp;#039;&amp;#039;Test Bench&amp;#039;&amp;#039;&amp;#039;, then &amp;#039;&amp;#039;&amp;#039;New Test Bench File&amp;#039;&amp;#039;&amp;#039;.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Clock_summary.png.png&amp;diff=8005</id>
		<title>Fișier:Clock summary.png.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Clock_summary.png.png&amp;diff=8005"/>
		<updated>2025-03-25T20:14:24Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;-&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=VPN_Tutorial_-_en&amp;diff=7989</id>
		<title>VPN Tutorial - en</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=VPN_Tutorial_-_en&amp;diff=7989"/>
		<updated>2025-03-10T18:09:05Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Downloading and Installing WireGuard */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== WireGuard VPN Installation and Usage Guide ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;WireGuard&amp;#039;&amp;#039;&amp;#039; is a modern, fast, and secure VPN solution. This guide provides step-by-step instructions on how to install and configure WireGuard on Windows, Linux, and macOS.&lt;br /&gt;
&lt;br /&gt;
To use WireGuard, you need:&lt;br /&gt;
* The WireGuard application installed on your system.&lt;br /&gt;
* A VPN configuration file (e.g., &amp;#039;&amp;#039;&amp;#039;wg-config.conf&amp;#039;&amp;#039;&amp;#039;) provided by your system administrator.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;!!! Each user should request only one configuration file per device and should NOT share it with other users or devices. !!!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Windows Installation ==&lt;br /&gt;
&lt;br /&gt;
=== Downloading the Application ===&lt;br /&gt;
1. Go to the official WireGuard website: [https://www.wireguard.com/install/ https://www.wireguard.com/install/]&lt;br /&gt;
2. Download the **Windows Installer**.&lt;br /&gt;
&lt;br /&gt;
[[File:Wireguard_download_page.png|1500px|Download the WireGuard installer from the official website.]]&lt;br /&gt;
&lt;br /&gt;
=== Installing the Application ===&lt;br /&gt;
1. Double-click the downloaded file (**wireguard-installer.exe**).&lt;br /&gt;
2. Follow the installation instructions.&lt;br /&gt;
3. After installation, open the WireGuard application.&lt;br /&gt;
&lt;br /&gt;
[[File:Wire1.png|Opening WireGuard after installation.]]&lt;br /&gt;
&lt;br /&gt;
=== Importing the Configuration File ===&lt;br /&gt;
1. Open the WireGuard application.&lt;br /&gt;
2. Click the &amp;#039;&amp;#039;&amp;#039;&amp;quot;Import tunnel(s) from file&amp;quot;&amp;#039;&amp;#039;&amp;#039; button.&lt;br /&gt;
3. Select the configuration file (**wg-config.conf**) provided by your system administrator.&lt;br /&gt;
&lt;br /&gt;
[[File:upload_config_file.png|Importing the configuration file into WireGuard.]]&lt;br /&gt;
&lt;br /&gt;
=== Activating the VPN ===&lt;br /&gt;
1. Once the configuration file is loaded, click on the **Activate** button.&lt;br /&gt;
2. If the connection is successful, the status will change to **Active** (green).&lt;br /&gt;
&lt;br /&gt;
[[File:Activare1.png|WireGuard connection activated.]]&lt;br /&gt;
&lt;br /&gt;
== Linux Installation ==&lt;br /&gt;
&lt;br /&gt;
=== Installing WireGuard ===&lt;br /&gt;
1. Open a terminal.&lt;br /&gt;
2. Run the following command to install WireGuard:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo apt install wireguard    # For Debian-based systems (Ubuntu)&lt;br /&gt;
sudo dnf install wireguard    # For Fedora-based systems&lt;br /&gt;
sudo pacman -S wireguard-tools # For Arch Linux&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Importing the Configuration File ===&lt;br /&gt;
1. Move the configuration file to the correct location:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cd ~/Downloads/&lt;br /&gt;
sudo cp wg-config.conf /etc/wireguard/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2. Ensure the correct file permissions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo chmod 600 /etc/wireguard/wg-config.conf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Activating the VPN ===&lt;br /&gt;
To start the VPN connection, run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo wg-quick up wg-config&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To stop the VPN connection, run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo wg-quick down wg-config&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To check the status of the WireGuard connection:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== macOS Installation ==&lt;br /&gt;
&lt;br /&gt;
=== Downloading and Installing WireGuard ===&lt;br /&gt;
1. Open the **App Store**.&lt;br /&gt;
2. Search for **WireGuard**.&lt;br /&gt;
3. Click **Install**.&lt;br /&gt;
&lt;br /&gt;
=== Importing the Configuration File ===&lt;br /&gt;
1. Open the WireGuard application.&lt;br /&gt;
2. Click on **Import tunnel(s) from file**.&lt;br /&gt;
3. Select the configuration file (**wg-config.conf**).&lt;br /&gt;
&lt;br /&gt;
[[File:upload_config_file.png|Importing the configuration file into WireGuard on macOS.]]&lt;br /&gt;
&lt;br /&gt;
=== Activating the VPN ===&lt;br /&gt;
1. Click on the tunnel you imported.&lt;br /&gt;
2. Click the **Activate** button.&lt;br /&gt;
&lt;br /&gt;
Once activated, your internet traffic will be routed through the VPN.&lt;br /&gt;
&lt;br /&gt;
[[File:Activare2.png|WireGuard successfully connected on macOS.]]&lt;br /&gt;
&lt;br /&gt;
== Checking Connection Status ==&lt;br /&gt;
To verify your VPN connection, visit:&lt;br /&gt;
[https://www.whatismyip.com/ https://www.whatismyip.com/]&lt;br /&gt;
&lt;br /&gt;
Your IP address should match the one assigned by the VPN server.&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Q: My WireGuard connection does not work. What should I do?&amp;#039;&amp;#039;&amp;#039;  &lt;br /&gt;
A: Check the following:&lt;br /&gt;
* Ensure that the configuration file is correctly imported.&lt;br /&gt;
* Verify that your internet connection is active.&lt;br /&gt;
* Restart the WireGuard service:&lt;br /&gt;
  &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  sudo wg-quick down wg-config&lt;br /&gt;
  sudo wg-quick up wg-config&lt;br /&gt;
  &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* If the problem persists, contact your system administrator.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Q: I cannot browse the internet after connecting to WireGuard.&amp;#039;&amp;#039;&amp;#039;  &lt;br /&gt;
A: This could be due to incorrect routing. Check with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ip route&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ensure that your VPN server is properly configured.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Q: How do I disconnect from WireGuard?&amp;#039;&amp;#039;&amp;#039;  &lt;br /&gt;
A: Click **Deactivate** in the WireGuard interface or run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo wg-quick down wg-config&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
You have now installed and configured WireGuard on Windows, Linux, and macOS. Always ensure your VPN is active when working remotely for security and privacy.&lt;br /&gt;
&lt;br /&gt;
If you encounter any issues, contact your system administrator for further assistance.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Reconfigurable_Computing&amp;diff=7988</id>
		<title>Reconfigurable Computing</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Reconfigurable_Computing&amp;diff=7988"/>
		<updated>2025-03-09T15:05:24Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Useful resources */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Course sessions =&lt;br /&gt;
&lt;br /&gt;
Courses can be found on Moodle platform.&lt;br /&gt;
&lt;br /&gt;
= Lab sessions =&lt;br /&gt;
&lt;br /&gt;
* [[Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation]]&lt;br /&gt;
* [[Lab 2 - Hardware flow. Vivado. PYNQ framework]]&lt;br /&gt;
&lt;br /&gt;
= Admin =&lt;br /&gt;
* Course - 50p oral exam (to be discussed)&lt;br /&gt;
* Lab&lt;br /&gt;
** 20p assignments (to be discussed)&lt;br /&gt;
** 30p project (to be discussed)&lt;br /&gt;
&lt;br /&gt;
Contact:&lt;br /&gt;
alexandru.ulmamei@upb.ro&lt;br /&gt;
&lt;br /&gt;
= Useful resources =&lt;br /&gt;
&lt;br /&gt;
* [https://docs.amd.com/r/en-US/ug585-zynq-7000-SoC-TRM ZYNQ7000 Technical Reference Manual]&lt;br /&gt;
* [https://docs.amd.com/r/en-US/ug1399-vitis-hls High Level Synthesis User Guide]&lt;br /&gt;
* [https://developer.arm.com/documentation/ihi0022/latest/ ARM AMBA Architecture]&lt;br /&gt;
* [https://cse.usf.edu/~haozheng/teach/cda4253/doc/hls/hls_bluebook_uv.pdf High Level Synthesis Book]&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7987</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7987"/>
		<updated>2025-03-09T13:13:55Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
For this lab, we will be using the AMD Vitis tool. In order to access the tool, login into your assigned machine using tigervnc-viewer (please see the [https://wiki.dcae.pub.ro/index.php/FPGA_Student_Classroom_tutorial tutorial].&lt;br /&gt;
&lt;br /&gt;
Once the GUI is opened, open a terminal and type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source /nfs/amd/Vitis/2024.2/settings64.sh&lt;br /&gt;
vitis&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Vitis window looks like this:&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Vitis.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
In order to create a new project, click on Create Component -&amp;gt; Create Empty HLS Component.&lt;br /&gt;
[[Fișier:Vitis hls new prj.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
Follow the steps shown in the Vitis executable. At some point, you will have to pick a name for the top function. Beware that the function name should also appear in the declaration of it.&lt;br /&gt;
&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Name and Location&amp;#039;&amp;#039;&amp;#039; tab, choose a name for your HLS component and a location. In the &amp;#039;&amp;#039;&amp;#039;Configuration File&amp;#039;&amp;#039;&amp;#039;, leave everything as is. In &amp;#039;&amp;#039;&amp;#039;Source Files&amp;#039;&amp;#039;&amp;#039;, write your desired top level function under the textbox labeled &amp;#039;&amp;#039;&amp;#039;Top Function&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:space-between;&amp;quot;&amp;gt;&lt;br /&gt;
[[Fișier:New prj.png|800px]]&lt;br /&gt;
[[Fișier:Source Files.png|800px]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Under the hardware section, select &amp;#039;&amp;#039;&amp;#039;xc7z020iclg400-1l&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
In the summary section, two pieces of information need to be provided:&lt;br /&gt;
* expected clock speed at which the circuit should operate: this constraints the tool to create a circuit that can function at that specific frequency&lt;br /&gt;
* clock uncertainty, how much the clock can vary around the expected value.&lt;br /&gt;
[[Fișier:Summary.png|800px]]&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Summary.png&amp;diff=7986</id>
		<title>Fișier:Summary.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Summary.png&amp;diff=7986"/>
		<updated>2025-03-09T13:12:10Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7985</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7985"/>
		<updated>2025-03-09T13:09:20Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
For this lab, we will be using the AMD Vitis tool. In order to access the tool, login into your assigned machine using tigervnc-viewer (please see the [https://wiki.dcae.pub.ro/index.php/FPGA_Student_Classroom_tutorial tutorial].&lt;br /&gt;
&lt;br /&gt;
Once the GUI is opened, open a terminal and type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source /nfs/amd/Vitis/2024.2/settings64.sh&lt;br /&gt;
vitis&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Vitis window looks like this:&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Vitis.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
In order to create a new project, click on Create Component -&amp;gt; Create Empty HLS Component.&lt;br /&gt;
[[Fișier:Vitis hls new prj.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
Follow the steps shown in the Vitis executable. At some point, you will have to pick a name for the top function. Beware that the function name should also appear in the declaration of it.&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:space-between;&amp;quot;&amp;gt;&lt;br /&gt;
[[Fișier:New prj.png|800px]]&lt;br /&gt;
[[Fișier:Source Files.png|800px]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;Name and Location&amp;#039;&amp;#039;&amp;#039; tab, choose a name for your HLS component and a location. In the &amp;#039;&amp;#039;&amp;#039;Configuration File&amp;#039;&amp;#039;&amp;#039;, leave everything as is. In &amp;#039;&amp;#039;&amp;#039;Source Files&amp;#039;&amp;#039;&amp;#039;, write your desired top level function under the textbox labeled &amp;#039;&amp;#039;&amp;#039;Top Function&amp;#039;&amp;#039;&amp;#039;.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Source_Files.png&amp;diff=7984</id>
		<title>Fișier:Source Files.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Source_Files.png&amp;diff=7984"/>
		<updated>2025-03-09T13:08:14Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;-&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7983</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7983"/>
		<updated>2025-03-09T13:04:33Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
For this lab, we will be using the AMD Vitis tool. In order to access the tool, login into your assigned machine using tigervnc-viewer (please see the [https://wiki.dcae.pub.ro/index.php/FPGA_Student_Classroom_tutorial tutorial].&lt;br /&gt;
&lt;br /&gt;
Once the GUI is opened, open a terminal and type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source /nfs/amd/Vitis/2024.2/settings64.sh&lt;br /&gt;
vitis&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Vitis window looks like this:&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Vitis.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
In order to create a new project, click on Create Component -&amp;gt; Create Empty HLS Component.&lt;br /&gt;
[[Fișier:Vitis hls new prj.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
Follow the steps shown in the Vitis executable. At some point, you will have to pick a name for the top function. Beware that the function name should also appear in the declaration of it.&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:space-between;&amp;quot;&amp;gt;&lt;br /&gt;
[[Fișier:New prj.png|400px]]&lt;br /&gt;
[[Fișier:New prj.png|400px]]&lt;br /&gt;
[[Fișier:New prj.png|400px]]&lt;br /&gt;
[[Fișier:New prj.png|400px]]&lt;br /&gt;
[[Fișier:New prj.png|400px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7982</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7982"/>
		<updated>2025-03-09T13:03:59Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
For this lab, we will be using the AMD Vitis tool. In order to access the tool, login into your assigned machine using tigervnc-viewer (please see the [https://wiki.dcae.pub.ro/index.php/FPGA_Student_Classroom_tutorial tutorial].&lt;br /&gt;
&lt;br /&gt;
Once the GUI is opened, open a terminal and type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source /nfs/amd/Vitis/2024.2/settings64.sh&lt;br /&gt;
vitis&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Vitis window looks like this:&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Vitis.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
In order to create a new project, click on Create Component -&amp;gt; Create Empty HLS Component.&lt;br /&gt;
[[Fișier:Vitis hls new prj.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
Follow the steps shown in the Vitis executable. At some point, you will have to pick a name for the top function. Beware that the function name should also appear in the declaration of it.&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:space-between;&amp;quot;&amp;gt;&lt;br /&gt;
[[Fișier:New prj.png|800px]]&lt;br /&gt;
[[Fișier:New prj.png|800px]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7981</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7981"/>
		<updated>2025-03-09T13:03:41Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
For this lab, we will be using the AMD Vitis tool. In order to access the tool, login into your assigned machine using tigervnc-viewer (please see the [https://wiki.dcae.pub.ro/index.php/FPGA_Student_Classroom_tutorial tutorial].&lt;br /&gt;
&lt;br /&gt;
Once the GUI is opened, open a terminal and type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source /nfs/amd/Vitis/2024.2/settings64.sh&lt;br /&gt;
vitis&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Vitis window looks like this:&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Vitis.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
In order to create a new project, click on Create Component -&amp;gt; Create Empty HLS Component.&lt;br /&gt;
[[Fișier:Vitis hls new prj.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
Follow the steps shown in the Vitis executable. At some point, you will have to pick a name for the top function. Beware that the function name should also appear in the declaration of it.&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:space-between;&amp;quot;&amp;gt;&lt;br /&gt;
[[Fișier:New prj.png|1200px]]&lt;br /&gt;
[[Fișier:New prj.png|1200px]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7980</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7980"/>
		<updated>2025-03-09T13:03:07Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
For this lab, we will be using the AMD Vitis tool. In order to access the tool, login into your assigned machine using tigervnc-viewer (please see the [https://wiki.dcae.pub.ro/index.php/FPGA_Student_Classroom_tutorial tutorial].&lt;br /&gt;
&lt;br /&gt;
Once the GUI is opened, open a terminal and type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source /nfs/amd/Vitis/2024.2/settings64.sh&lt;br /&gt;
vitis&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Vitis window looks like this:&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Vitis.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
In order to create a new project, click on Create Component -&amp;gt; Create Empty HLS Component.&lt;br /&gt;
[[Fișier:Vitis hls new prj.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
Follow the steps shown in the Vitis executable. At some point, you will have to pick a name for the top function. Beware that the function name should also appear in the declaration of it.&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
[Fișier:New prj.png|1200px]&lt;br /&gt;
[Fișier:New prj.png|1200px]&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7979</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7979"/>
		<updated>2025-03-09T13:02:51Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
For this lab, we will be using the AMD Vitis tool. In order to access the tool, login into your assigned machine using tigervnc-viewer (please see the [https://wiki.dcae.pub.ro/index.php/FPGA_Student_Classroom_tutorial tutorial].&lt;br /&gt;
&lt;br /&gt;
Once the GUI is opened, open a terminal and type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source /nfs/amd/Vitis/2024.2/settings64.sh&lt;br /&gt;
vitis&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Vitis window looks like this:&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Vitis.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
In order to create a new project, click on Create Component -&amp;gt; Create Empty HLS Component.&lt;br /&gt;
[[Fișier:Vitis hls new prj.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
Follow the steps shown in the Vitis executable. At some point, you will have to pick a name for the top function. Beware that the function name should also appear in the declaration of it.&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
[[Fișier:New prj.png|1200px]]&lt;br /&gt;
[[Fișier:New prj.png|1200px]]&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7978</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7978"/>
		<updated>2025-03-09T13:02:28Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
For this lab, we will be using the AMD Vitis tool. In order to access the tool, login into your assigned machine using tigervnc-viewer (please see the [https://wiki.dcae.pub.ro/index.php/FPGA_Student_Classroom_tutorial tutorial].&lt;br /&gt;
&lt;br /&gt;
Once the GUI is opened, open a terminal and type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source /nfs/amd/Vitis/2024.2/settings64.sh&lt;br /&gt;
vitis&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Vitis window looks like this:&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Vitis.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
In order to create a new project, click on Create Component -&amp;gt; Create Empty HLS Component.&lt;br /&gt;
[[Fișier:Vitis hls new prj.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
Follow the steps shown in the Vitis executable. At some point, you will have to pick a name for the top function. Beware that the function name should also appear in the declaration of it.&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
Fișier:New prj.png|1200px&lt;br /&gt;
Fișier:New prj.png|1200px&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7977</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7977"/>
		<updated>2025-03-09T13:02:06Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
For this lab, we will be using the AMD Vitis tool. In order to access the tool, login into your assigned machine using tigervnc-viewer (please see the [https://wiki.dcae.pub.ro/index.php/FPGA_Student_Classroom_tutorial tutorial].&lt;br /&gt;
&lt;br /&gt;
Once the GUI is opened, open a terminal and type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source /nfs/amd/Vitis/2024.2/settings64.sh&lt;br /&gt;
vitis&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Vitis window looks like this:&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Vitis.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
In order to create a new project, click on Create Component -&amp;gt; Create Empty HLS Component.&lt;br /&gt;
[[Fișier:Vitis hls new prj.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
Follow the steps shown in the Vitis executable. At some point, you will have to pick a name for the top function. Beware that the function name should also appear in the declaration of it.&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
Fișier:New prj.png|miniatura&lt;br /&gt;
Fișier:New prj.png|miniatura&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:New_prj.png&amp;diff=7976</id>
		<title>Fișier:New prj.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:New_prj.png&amp;diff=7976"/>
		<updated>2025-03-09T13:00:50Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;-&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7975</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7975"/>
		<updated>2025-03-09T12:55:44Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
For this lab, we will be using the AMD Vitis tool. In order to access the tool, login into your assigned machine using tigervnc-viewer (please see the [https://wiki.dcae.pub.ro/index.php/FPGA_Student_Classroom_tutorial tutorial].&lt;br /&gt;
&lt;br /&gt;
Once the GUI is opened, open a terminal and type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source /nfs/amd/Vitis/2024.2/settings64.sh&lt;br /&gt;
vitis&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Vitis window looks like this:&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Vitis.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
In order to create a new project, click on Create Component -&amp;gt; Create Empty HLS Component.&lt;br /&gt;
[[Fișier:Vitis hls new prj.png|1200px]]&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Vitis_hls_new_prj.png&amp;diff=7974</id>
		<title>Fișier:Vitis hls new prj.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Vitis_hls_new_prj.png&amp;diff=7974"/>
		<updated>2025-03-09T12:55:26Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;-&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7973</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7973"/>
		<updated>2025-03-08T16:16:45Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
For this lab, we will be using the AMD Vitis tool. In order to access the tool, login into your assigned machine using tigervnc-viewer (please see the [https://wiki.dcae.pub.ro/index.php/FPGA_Student_Classroom_tutorial tutorial].&lt;br /&gt;
&lt;br /&gt;
Once the GUI is opened, open a terminal and type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source /nfs/amd/Vitis/2024.2/settings64.sh&lt;br /&gt;
vitis&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Vitis window looks like this:&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Vitis.png|1200px]]&lt;br /&gt;
&lt;br /&gt;
In order to create a new project, click on Create Component -&amp;gt; Create Empty HLS Component.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7972</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7972"/>
		<updated>2025-03-08T16:15:51Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
For this lab, we will be using the AMD Vitis tool. In order to access the tool, login into your assigned machine using tigervnc-viewer (please see the [https://wiki.dcae.pub.ro/index.php/FPGA_Student_Classroom_tutorial tutorial].&lt;br /&gt;
&lt;br /&gt;
Once the GUI is opened, open a terminal and type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source /nfs/amd/Vitis/2024.2/settings64.sh&lt;br /&gt;
vitis&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Vitis window looks like this:&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Vitis.png|1200px]]&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7971</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7971"/>
		<updated>2025-03-08T16:15:43Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
For this lab, we will be using the AMD Vitis tool. In order to access the tool, login into your assigned machine using tigervnc-viewer (please see the [https://wiki.dcae.pub.ro/index.php/FPGA_Student_Classroom_tutorial tutorial].&lt;br /&gt;
&lt;br /&gt;
Once the GUI is opened, open a terminal and type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source /nfs/amd/Vitis/2024.2/settings64.sh&lt;br /&gt;
vitis&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Vitis window looks like this:&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Vitis.png|miniatura]]&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7970</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7970"/>
		<updated>2025-03-08T16:15:31Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
For this lab, we will be using the AMD Vitis tool. In order to access the tool, login into your assigned machine using tigervnc-viewer (please see the [https://wiki.dcae.pub.ro/index.php/FPGA_Student_Classroom_tutorial tutorial].&lt;br /&gt;
&lt;br /&gt;
Once the GUI is opened, open a terminal and type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source /nfs/amd/Vitis/2024.2/settings64.sh&lt;br /&gt;
vitis&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Vitis window looks like this:&lt;br /&gt;
&lt;br /&gt;
[[Fișier:Vitis.png]]&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Vitis.png&amp;diff=7969</id>
		<title>Fișier:Vitis.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Vitis.png&amp;diff=7969"/>
		<updated>2025-03-08T16:15:25Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;-&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7968</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7968"/>
		<updated>2025-03-08T16:12:23Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
For this lab, we will be using the AMD Vitis tool. In order to access the tool, login into your assigned machine using tigervnc-viewer (please see the [https://wiki.dcae.pub.ro/index.php/FPGA_Student_Classroom_tutorial tutorial].&lt;br /&gt;
&lt;br /&gt;
Once the GUI is opened, open a terminal and type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source /nfs/amd/Vitis/2024.2/settings64.sh&lt;br /&gt;
vitis&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7967</id>
		<title>Lab 1 - Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Lab_1_-_Intro_to_High_Level_Synthesis._Simulation._Co-simulation._RTL_generation&amp;diff=7967"/>
		<updated>2025-03-08T16:10:11Z</updated>

		<summary type="html">&lt;p&gt;Andrei.ulmamei: /* Intro to High Level Synthesis. Simulation. Co-simulation. RTL generation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
High-Level Synthesis (HLS) is a design methodology that enables hardware description at a higher level of abstraction, typically using C, C++, or SystemC, rather than traditional register-transfer level (RTL) coding. HLS tools automatically translate high-level specifications into optimized RTL implementations, reducing development time while allowing for &amp;#039;&amp;#039;&amp;#039;design space exploration&amp;#039;&amp;#039;&amp;#039;.&lt;/div&gt;</summary>
		<author><name>Andrei.ulmamei</name></author>
	</entry>
</feed>