Basics of Syntax and Data Types in Python

Published by Mateusz Kędziora on

img LgWlXjgl6KxmAZyVIpCg1e2j

Python is a general-purpose programming language that is especially popular among beginner programmers due to its simple syntax and ease of learning. In this article, we will look at the basics of Python syntax and various data types that we may encounter when writing programs.

Variables and data types:

In Python, we can store various types of data such as numbers, text, lists, or dictionaries. Examples of some basic data types are:

  • Numbers: Python supports integer, float, and complex numbers. Examples:
x = 10 # integer
y = 3.14 # float
z = 3 + 4j # complex
  • Text: Text in Python is represented by strings. We can create them using single or double quotes. Examples:
s = "This is a string"
t = 'This is also a string'
  • Lists: Lists are sequences of objects that can contain elements of different types. We create them using square brackets and separate elements with commas. Examples:
a = [1, 2, 3] # list of integers
b = ["a", "b", "c"] # list of strings
c = [1, "two", 3.0] # list of different data types
  • Dictionaries: Dictionaries are a type of data structure that stores key-value pairs. We create them using curly brackets and specify key-value pairs with a colon. Examples:
d = {'a': 1, 'b': 2} # dictionary with integer values
e = {'a': 'A', 'b': 'B'} # dictionary with string values
f = {'a': 1, 'b': 'B'} # dictionary with mixed data types

Arithmetic and logical operators:

Python has standard arithmetic operators such as addition (+), subtraction (-), multiplication (*), and division (/). We can also calculate the remainder of a division using the modulo (%) operator. Examples:

x = 10
y = 3

# addition
z = x + y
print(z) # 13

# subtraction
z = x - y
print(z) # 7

# multiplication
z = x * y
print(z) # 30

# division
z = x / y
print(z) # 3.3333333333333335

# modulo
z = x % y
print(z) # 1

For logical operations, we have comparison operators (==, !=, >, <, >=, <=) and logical operators (and, or, not). Examples:

x = 10
y = 3

# equal
z = x == y
print(z) # False

# not equal
z = x != y
print(z) # True

# greater
z = x > y
print(z) # True

# less
z = x < y
print(z) # False

# greater or equal
z = x >= y
print(z) # True

# less or equal
z = x <= y
print(z) # False

# and
z = (x > y) and (x < y)
print(z) # False

# or
z = (x > y) or (x < y)
print(z) # True

# not
z = not (x > y)
print(z) # False

Conditional statements and loops:

In Python, we can use conditional statements such as if, elif, and else to perform different actions depending on a condition. Example:

x = 10
y = 3

if x > y:
    print("x is greater than y")
elif x < y:
    print("x is less than y")
else:
    print("x is equal to y")

We can also use loops such as for and while to perform a given action multiple times. Examples:

# for loop
for i in range(5):
    print(i)

# while loop
i = 0
while i < 5:
    print(i)
    i += 1

To summarize, in this article we learned the basics of Python syntax and various data types that we may encounter when writing programs. We also learned about basic arithmetic and logical operators and conditional statements and loops, which allow us to control the flow of the program.

Text generated using chat.openai.com.

Categories: Python

Mateusz Kędziora

Jestem uczniem technikum a prowadzeniem bloga zajmuje się hobbystycznie.

Leave a Reply

Avatar placeholder

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