Python is a high-level language used in various types of projects. Suitable for most computer platforms, Python is considered by many professionals to be an excellent tool for getting started in computer programming. Here are the basic concepts needed to write code in Python.
My 1st programme
Usually, the "Hello World!" program is used as an example to introduce students to the syntax of a programming language like Python. It is a basic program, which just displays the phrase "Hello World!" on the screen, before terminating. This first program in Python is created with a single line of code: just select print ("Hello World!"). Indeed, the Python language has a predefined print function that displays the data, text or characters provided to it.
Basic functions and operators
In programming, functions represent a series of instructions that can be reused by the developer to avoid having to write the same code several times in his program. The "print" function can be used to display data of any type. With the "input" function, we will be able to read an input. The "len" function returns the string Long, which contains the number of characters required for a string or the number of bytes required to store a variable. The latter function is useful in calculating the size of compound data. There is also the "str" function which is used to convert data to a string, regardless of type.
Operators are signs or symbols that allow an operation to be performed. The Python programming language has several operators. Arithmetic operators are used to perform mathematical calculations (addition, subtraction, multiplication, etc.) between variables containing numerical values.
With string operators, we can manipulate data of type str (strings) and at the same time variables that contain this type of data. The Python language offers two string operators:
- the concatenation operator to join two strings end to end to have a third and new string,
- the repeat operator to repeat a string a certain number of times.
In Python, there is also a simple assignment operator, the "=" symbol, used to assign a value to a variable. There are also compound assignment operators which are used to perform two operations in a row, namely a calculation operation and an assignment operation. Other operators are proposed by the language such as comparison, identity, logical, binary or membership operators.
The variables in Python can be thought of as memory locations used to store a value which is given a name for better identification. To access the contents of a variable (using the "print" function, for example), the variable must first be initialised by assigning a value to it. When you try to use an uninitialised variable, the program stops and the Python interpreter produces what is called a runtime error. With variables, you can store different types of values, whether they are numbers, strings, booleans, etc.
Testing allows the developer to avoid errors in the code he is writing. The test functions allow the execution of various instructions taking into account a logical condition. In fact, to be sure of one's code, it must be tested on every possible use case of the program. As this is difficult to achieve with complex programs, it is recommended that the code be broken down into functions of reasonable size which are tested independently. The tests carried out for each function are unit tests.
In Python, the "if...elif...else" condition (for "if, else if, else") is a complete conditional structure that allows you to run multiple tests taking into account the desired number of cases.
Most of the time, a computer program will be used to manipulate data. With the Python language, there are several predefined data types, the most popular being numbers and strings.
The Python language distinguishes three types of numerical data:
- the integer type (int) representing integers, regardless of their size,
- the float type for numbers with a decimal part,
- the complex type for complex numbers with an imaginary part named J.
In Python, the string is represented by the str type, which is a sequence of characters delimited by apostrophes or inverted commas. The bool type is also used for booleans, which are data types that can take the values True or False. List data represents a collection of elements (numbers, strings, etc.) separated by commas and enclosed in square brackets.
In programming, loops facilitate the repetition of a sequence of instructions according to certain conditions. For example, there are "while" and "for" loops.
With the "while" loop, a series of instructions is executed that obey a given condition. In general, this happens in three steps: the initialization of the iteration variable (i), the testing of the variable and the updating of the variable.
Also used to repeat a block of instructions, the "for" loop is generally used to iterate over a predefined sequence of values. The sequences can be of different types.
Apart from the predefined functions of Python, the programming language also gives the user the possibility to define his own functions that will be available and usable only in his space. You can write your own Python functions for programs that repeatedly use the same set of instructions. This avoids having to rewrite these instructions every time. Functions are also very useful for simplifying your code, especially when it has to be shared with other developers.
The keyword "def" is used to introduce the definition of a new function in Python. In the code, the keyword is accompanied by the name of the function, parentheses in which a list of parameters will be provided, and a colon ":" to end the line. The name of the function must obey the general rules for names in Python, notably: start with a letter or an underscore and be made up of only classic alphanumeric characters.
Sometimes developers need to represent more complex data than just numbers, strings or lists. In this case, the dictionary is the data structure to use with the Python language. It allows data to be stored in key-value pairs. Dictionaries are defined by braces at the beginning and end. The key-value pair contains a colon between the key and the value, and a comma at the end. Each dictionary is composed of unique keys.
These different notions are part of the basics of programming with the Python language. The manipulation of variables, data types and functions must be understood and mastered by all those who wish to pursue a career in data science or web development. With the Jedha training, learners can take their first step into these promising sectors, opting for a course programme that adapts to their learning pace.