3. Basic Python syntax

Página 3

Python is a high-level, interpreted, scripting, imperative, object-oriented, functional, dynamically typed, strong programming language. It was released by Guido van Rossum in 1991. The Python language has a clear and readable syntax that makes it easy for beginners to learn programming.

Indentation

Python uses indentation to delimit blocks of code. Unlike other languages ​​that use specific keys or keywords for this. Indentation in Python isn't just for aesthetics, it's a functional part of the language. See example:

if 5 > 2:
  print("Five is greater than two!")

If you don't indent correctly, Python will throw an error.

Comments

Python comments begin with the hash character, #, and extend to the end of the physical line. A comment can appear at the beginning of a line or after white space or code, but not within a string literal. A hash character inside a string literal is just a hash character. See example:

# This is a comment
print("Hello World!")

Variables

In Python, variables are created when you assign them a value:

x = 5
y = "Hello World!"

Python is dynamically typed, which means you can change the type of a variable in your code:

x = 4 # x is of type int
x = "Sally" # x is now of type str

Data Types

Python has the following data types built in by default, in these categories:

  • Text Type: str
  • Numeric Types: int, float, complex
  • Sequence Types: list, tuple, range
  • Mapping Type: dict
  • Set Types: set, frozenset
  • Boolean Type: bool
  • Binary Types: bytes, bytearray, memoryview

Strings

Python strings are enclosed in either single quotes or double quotes.

print("Hello")
print('Hello')

Multi-line strings:

a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incident
ut labore et dolore magna aliqua."""
print(a)

Operators

Python divides operators into several types:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical operators
  • Identity Operators
  • Association Operators
  • Bitwise Operators

In short, the basic syntax of Python is very easy to learn and use, especially for newcomers to programming. With its use of indentation and its clear structure, you can write readable and easy-to-understand Python code.

Now answer the exercise about the content:

Who released the Python programming language and in what year?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

44. Control Structures in Python

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text