Day 2: Python syntax basics, variables, data types

Programming

Day 2: Python syntax basics, variables, data types

Python syntax basics

Python has a simple and clear syntax . Here are some basic syntax rules:
1. Use indentation to define blocks of code. Typically 4 spaces or 1 tab per level.
2. Comments begin with the # symbol. Anything after # is ignored by the Python interpreter.
3. Multiline comments can be created using triple quotes: «»»Comment»»» or '''Comment'''.
4. Instructions that are on the same line are separated by a semicolon ;.

Variables

A variable is a named place in memory where a value is stored. In Python, variables do not require a type declaration. To create a variable, it is enough to assign a value to it using the operator =:

x = 5 name = "John" 

Data types

Python supports various data types:
1. Numbers:
• Integers (int): x = 5
• Real numbers (float): y = 3.14
• Complex numbers (complex): z = 2 + 3j
2. Strings (str): sequence of characters enclosed in single or double quotes: name = 'John' or name = «John»
3. Boolean values ​​(bool): have the value True or False. Typically used to test conditions.

Determining the type of a variable

To find out the type of a variable, use the type() function:

 x = 5 print( type(x)) # <class 'int'> 

Type conversion

You can convert data types using the functions int(), float(), str(), and bool() :

x = 5.5 y = int(x) # y will equal 5 (int) 

Now you are familiar with the basic syntax Python, variables and basic data types.
In the next lesson we will study operators and branching.

Оцените статью
Xrust.com
Добавить комментарий