Computer

Python, the basic syntax

The reason for the success of Python is to be found in its versatility, ease of development and learning, and at the same time, programming power. Python is a programming language high-level, interpreted, object-oriented and with dynamic semantics, suitable for rapid application development.

The simple syntax and easy to learn emphasizes readability and reduces the cost of maintaining programs. Modules and packages encourage modular programming and code reuse. Furthermore, the Python interpreter and the extensive standard library are available in both source and binary form and can be distributed freely.

Python can be used to automate tasks, perform calculations, create user interfaces, website backends, access databases, download information from the Internet, etc. It’s a easy language to learn and write: perfect for beginner programmers, but equally useful for experienced professionals. But let’s start from the basics.

  • 1. Syntax in Python

    Beautiful is better than ugly.

    Explicit is better than implicit.

    Simple is better than complex.

    Complex is better than complicated.

    Readability is important.

    These five principles, taken from The Zen of Pythona collection of 20 aphorisms published by Tim Peters which have influenced and continue to influence the development of the language, make it clear how in Python the form is as important as the syntax.

    In programming languages, “syntax” refers to the rules that specify the correct combined sequence of symbols which can be used to form a properly structured program.

  • 2. Identifiers

    I names used to identify variables, functions, classes and modules must begin with a letter or an underscore (_), and can then contain letters, digits and underscore characters.

    Python is a programming language case sensitivein the sense that i uppercase and lowercase characters are considered as different letters. Thus, “test” and “Test” are interpreted differently.

    In Python there are conventions related to the naming of identifiers:

    • Class names begin with an uppercase letter, all others with a lowercase letter;
    • An identifier that begins with an underscore indicates that it is a private identifier;
    • An identifier that begins with two underscores indicates that it is a strongly private identifier;
    • If the identifier begins and ends with two underscores then it is a special name defined in the language.
  • 3. Indentation

    One of the peculiarities of Python concerns the fact that to delimit blocks of code, instead of using common parentheses, indentation is used. THE whitespace characters present at the beginning of a logical line determine the level of indentation of the line and therefore, the grouping of instructions to which it belongs. There logical priority of the operations to be performed is determined by the number of spaces present at the beginning of the line containing the command.

    Getting the indent wrong generates a IndentationErrorand according to the indications provided by the guidelines, the indentation must be done using 4 spaces.

  • 4. Multiline instructions

    In Python every command is written on a single line and does not require particular term characters (other languages ​​require, for example, semicolons).

    The interpreter treats each line as an entire statement; however, you can use one backslash (backslash) or triple quotes to explicitly indicate that the instruction continues on the next line.

    The instructions contained in parentheses (), {} or () do not require any characters to continue on subsequent lines.

  • 5. Quotation marks

    The quotation marks, or superscripts, are used for define character strings. Python accepts single quotes (‘) and double quotes (“) without distinction, as long as you use the same ones for the beginning and end of the string. Also, as seen above, triple quotes (”’ or “””) are used to define a string across multiple lines.

  • 6. Comments and documentation

    Before writing a new piece of code, it’s a good idea to write a line or two describing what it does. THE comments they are written by doing them precede by ahashtag (#) which is not part of the string literal and ends at the end of the physical line. All characters after the pound sign up to the end of the line are part of the comment e I don’t know how to interpret Python.

    Comments must be written on the same indentation level of the code you are commenting on. It’s better avoid comments along the same lines of the code.

    The docstring they are similar to comments but, unlike them, they are more specific. Furthermore, they are preserved at runtime so that the programmer can inspect them. A docstring begins and ends with three single or double quotation marks.

  • 7. Variables

    Another feature of Python is the dynamic typing of variablesin the sense that they do not have to be declared in advance, but only at the time of code execution just written, in runtime. Python has no command to declare a variable that is created when you first assign a value to it.

    The variables they can change type after they have been set simply by assigning them a different value.

    Il casting in programming it is the conversion of data from one type to another. We may have a variable that is assigned the value “123,” even though it looks like a number, as the quotes suggest, it is actually a string.

    If I wanted to treat this “123” as a number, and add another to it, I would first have to turn it into an integer. To convert a string or a float to an integer, use the int() function.

    There is a casting function for each type of data, widely used are, for example, str(), float(), list() o dict().

    You can get the data type of a variable with the type() function.

    To know more: What can you do with Python

Leave a Reply

Your email address will not be published. Required fields are marked *