Programming Basics
The same basic building blocks of programming, shown side by side in Java, Python and C.
Last updated: September 23, 2026
Declaring a variable
A variable stores a value under a name, so you can access it again later in the code.
Javaint number = 5;Pythonnumber = 5Cint number = 5;Printing text
Displays text or the result of a calculation on the screen, handy for testing and following what the program is doing.
JavaSystem.out.println("Hello");Pythonprint("Hello")Cprintf("Hello");If condition
Runs a block of code only if a certain condition is true, otherwise it's skipped.
Javaif (number > 0) { ... }Pythonif number > 0:Cif (number > 0) { ... }For loop
Repeats a block of code a fixed number of times, for example exactly ten times.
Javafor (int i = 0; i < 10; i++) { ... }Pythonfor i in range(10):Cfor (int i = 0; i < 10; i++) { ... }While loop
Repeats a block of code for as long as a condition stays true, the number of runs isn't fixed in advance.
Javawhile (number > 0) { ... }Pythonwhile number > 0:Cwhile (number > 0) { ... }Defining a function
Groups several instructions under their own name, which you can then reuse later with a single call.
Javaint add(int a, int b) { return a + b; }Pythondef add(a, b): return a + bCint add(int a, int b) { return a + b; }List or array
Stores several values one after another under a shared name, instead of creating a separate variable for each value.
Javaint[] numbers = {1, 2, 3};Pythonnumbers = [1, 2, 3]Cint numbers[] = {1, 2, 3};Comment
Text in the code that the program ignores, useful for explanations to yourself or others.
Java// single line commentPython# single line commentC// single line commentData types and type conversion
Every value has a data type, for example a whole number or a piece of text. If two types don't match, you need to convert one first.
intA whole number with no decimal places, for example 5floatA number with decimal places, for example 5.0strText, also called a string, for example "5"Python: int("5")Converts the text "5" into the number 5Operators
Symbols you use to combine values or compare them with each other.
+ − * /Addition, subtraction, multiplication, division%Remainder of a division, for example 7 % 2 gives 1== and !=Checks whether two values are equal or not equal> and <Checks whether a value is greater or smaller than anotherWorking with text
Text can be treated as a chain of individual characters, each character has a position, starting at 0.
text[0]The first character of a text, called indexingtext[0:3]The first three characters, called slicingtext.lower()Converts the text to lowercasetext.replace("a","b")Replaces every "a" in the text with a "b"len(text)Returns the number of characters in the textASCII, characters and escape sequences
Internally, every character is just a number. ASCII defines which number belongs to which character.
ASCIIAssigns fixed number values to letters, digits and symbols, capital A for example stands for 65Python: chr(65)Converts the number 65 into the character "A"Python: ord("A")Converts the character "A" back into the number 65\n and \tEscape sequences for a line break and a tab inside a textErrors while programming
An error message is no reason to panic, it almost always names the line and the type of problem. The most dangerous error shows no message at all though.
SyntaxErrorThe code breaks the grammar of the language, often a missing bracket or colonNameErrorA name is used that doesn't exist yet, often a typo in a variable nameIndentationErrorThe indentation is wrong, in Python indentation decides what belongs to which code blockSemantic errorThe code runs without any error, but produces a wrong result, for example because + was used instead of −, no error message, so it's the hardest to findTipCheck the line number named in the error message first, that's almost always where the problem isHow a loop with a condition runs
This is how a typical for or while loop with a check runs, step by step.
Deeper by language
A few extra quirks per language, beyond the plain comparison.
Python
Java
C
ASCII table
All printable ASCII characters with their number code. You don't need to memorize this table, it's just here for reference whenever you need a code.
| Character | Code | Character | Code | Character | Code | Character | Code |
|---|---|---|---|---|---|---|---|
| Space | 32 | ! | 33 | " | 34 | # | 35 |
| $ | 36 | % | 37 | & | 38 | ' | 39 |
| ( | 40 | ) | 41 | * | 42 | + | 43 |
| , | 44 | - | 45 | . | 46 | / | 47 |
| 0 | 48 | 1 | 49 | 2 | 50 | 3 | 51 |
| 4 | 52 | 5 | 53 | 6 | 54 | 7 | 55 |
| 8 | 56 | 9 | 57 | : | 58 | ; | 59 |
| < | 60 | = | 61 | > | 62 | ? | 63 |
| @ | 64 | A | 65 | B | 66 | C | 67 |
| D | 68 | E | 69 | F | 70 | G | 71 |
| H | 72 | I | 73 | J | 74 | K | 75 |
| L | 76 | M | 77 | N | 78 | O | 79 |
| P | 80 | Q | 81 | R | 82 | S | 83 |
| T | 84 | U | 85 | V | 86 | W | 87 |
| X | 88 | Y | 89 | Z | 90 | [ | 91 |
| \ | 92 | ] | 93 | ^ | 94 | _ | 95 |
| ` | 96 | a | 97 | b | 98 | c | 99 |
| d | 100 | e | 101 | f | 102 | g | 103 |
| h | 104 | i | 105 | j | 106 | k | 107 |
| l | 108 | m | 109 | n | 110 | o | 111 |
| p | 112 | q | 113 | r | 114 | s | 115 |
| t | 116 | u | 117 | v | 118 | w | 119 |
| x | 120 | y | 121 | z | 122 | { | 123 |
| | | 124 | } | 125 | ~ | 126 |