Introduction to Python : From Simple Calculator to Basic Programming
Photo By Hitesh Choudhary on Unsplash
Introduction to Python Python is one of the most popular and easy-to-understand programming languages, both for beginners and experienced developers. With its simple and flexible syntax, along with extensive documentation, Python has become the top choice for various purposes.
The Python language can be used for basic programming, web development, and even artificial intelligence. In this article, we will provide an informal introduction to Python, starting from how to use the interpreter, manipulating numbers, text, and lists, up to creating simple programs.
When running Python, we interact through an interpreter. In interactive mode, every command typed after the prompt symbol (>>>) is immediately processed, and the result is displayed instantly.
Python also has a secondary prompt (…) which is usually used when typing multi-line commands, such as nested statements or loops.
One of the conveniences offered in Python’s documentation is the presence of a copy button on code examples. With this feature, we can copy the provided commands without the output, and then paste them directly into the Python interpreter to run them.
In addition, Python also supports comments, marked with the # symbol. Comments can be written at the beginning of a line or after an expression, with the purpose of providing additional explanation so that the code is easier to understand.
It is important to note that a # character inside quotation marks is not a comment, but rather part of a string.
One of the first features beginners can try is using Python as a calculator. By writing simple arithmetic expressions, Python will display the calculation results.
Basic operators such as +, -, *, and / can be used right away. For example, 2 + 2 will produce 4, while 50 – 5*6 will return 20.
Interestingly, Python always produces a floating-point number (float) when performing division with the / operator. However, if we want the result of the division to be an integer, we can use the // operator.
There is also the % operator to get the remainder of a division, as well as ** to perform exponentiation. With these combinations, Python functions not only as a basic calculator but also as a fairly complete mathematical tool.
Moreover, Python supports various numeric data types, ranging from integers (int), floating-point numbers (float), high-precision decimals (Decimal), fractions (Fraction), to complex numbers (complex). This flexibility makes Python very useful for scientific and financial calculations.
Besides numbers, Python is also very powerful in manipulating text, or strings. Strings can be written with single quotes (‘…’) or double quotes (“…”).
For longer text, we can use triple quotes (“””…””” or ”’…”’). Additionally, Python provides special characters such as \n for a new line or \t for a tab.
Python also supports raw strings, which start with the letter r, useful to prevent special character interpretation, for example when writing file paths in Windows.
Strings in Python can be concatenated using the + operator or multiplied with * to create repetition. Furthermore, we can extract specific parts of a string using indexing.
The first index starts at 0, while negative indices are used to count from the end. Not only that, Python also supports slicing to obtain substrings, such as word[0:2] which returns the first two characters.
Another important thing is that strings in Python are immutable, meaning they cannot be changed. If we want to modify a string, we must create a new one based on certain parts of the old string.
In addition to strings, Python has a very flexible data type called a list. A list is used to store a collection of values in a single variable, written with square brackets, for example [1, 4, 9, 16, 25].
Just like strings, lists can be indexed, sliced, and even concatenated. The difference is that lists are mutable, meaning their contents can be changed at any time.
We can add new elements to a list using the .append() method, or remove some elements using slicing.
Lists also support nesting, meaning we can place a list inside another list. This makes it possible to store more complex data, such as a list containing both names and numbers.
In addition, Python provides the built-in function len() to count the number of elements in a list. This flexibility is what makes lists one of the most important data structures in Python.
After understanding numbers, text, and lists, we can start writing simple programs. One classic example is displaying the Fibonacci sequence. By using a while loop, Python will keep running a block of code as long as the condition is true.
In the Fibonacci example, we use variables a and b as the starting values, then calculate the repeated sums until a certain limit is reached. Python also supports multiple assignment, allowing us to assign two variables at once in a single line of code, such as a, b = 0, 1.
Indentation (spacing) is also an important rule in Python. Unlike other languages that use curly braces, Python uses indentation to indicate blocks of code. Therefore, every line within a loop or function must be consistently indented.
To display results, we use the print() function. This function can print multiple arguments at once and automatically adds spaces between them. With the additional end parameter, we can also change how Python ends the output, for example, replacing a new line with a comma.
Through this brief overview, we have seen how Python can be used for simple tasks such as a calculator, text manipulation, and even creating small programs like the Fibonacci sequence.
Python’s flexibility makes it not only beginner-friendly but also powerful enough for large projects. With this basic understanding, the next step is to deepen your knowledge of data structures, functions, and even object-oriented programming concepts.
Python is not just a tool but a gateway into the vast world of programming. If we keep practicing, this simple language will open up great opportunities across various fields of modern technology.
Source : Pengantar Informal tentang Python