Basic Syntax and Data Types

Python Syntax

Python programs are made up of statements, which are executed one after the other. Statements can be simple, like assigning a value to a variable, or more complex, like calling a function. Here are some examples of simple Python statements:


            x = 42
            y = "hello world"
        

In Python, statements are usually written one per line, and the end of a statement is marked by a newline character. However, you can also write multiple statements on the same line by separating them with a semicolon (;).

Python uses indentation to indicate blocks of code that should be executed together. Indentation is usually done using four spaces, although some programmers prefer to use tabs instead. Here is an example of a block of code that is indented correctly:


            if x > 10:
                print("x is greater than 10")
            else:
                print("x is less than or equal to 10")
        

In this example, the if statement and the else statement are both indented with four spaces.

Data Types

Python has several built-in data types, including:

  • Numbers (integers, floating-point numbers)
  • Strings
  • Booleans
  • Lists
  • Tuples
  • Sets
  • Dictionaries

Numbers

Numbers in Python can be integers (whole numbers) or floating-point numbers (numbers with decimal points). Here are some examples:


            x = 42      #this is an example of an integer
            y = 3.14    #this is an example of a float
        

You can perform arithmetic operations on numbers in Python, including addition, subtraction, multiplication, and division.

Strings

Strings in Python are sequences of characters. They are enclosed in single or double quotes. Here are some examples:


            x = "hello"
            y = 'world'
        

You can concatenate strings using the '+' operator, and you can repeat a string using the '*' operator.

Booleans

Booleans in Python are either True or False. They are used in conditional statements and other places where you need to represent a true/false value. Here are some examples:


            x = True
            y = False
        

You can perform boolean operations on booleans in Python, including 'and', 'or', and 'not'.

Lists

Lists in Python are ordered sequences of values. They are enclosed in square brackets, with each value separated by a comma. Here are some examples:


            x = [1, 2, 3]
            y = ["apple", "banana", "cherry"]
        

You can access individual elements of a list using indexing, and you can perform operations on lists, including adding elements, removing elements, and sorting the list.

Tuples

Tuples in Python are similar to lists, but they are immutable, which means that once you create a tuple, you can't modify it. Tuples are enclosed in parentheses, with each value separated by a comma. Here are some examples:


            x = (1, 2, 3)
            y = ("apple", "banana", "cherry")
        

You can access individual elements of a tuple using indexing, but you can't modify the tuple itself.

Sets

Sets in Python are unordered collections of unique values. They are enclosed in curly braces, with each value separated by a comma. Here are some examples:


            x = {1, 2, 3}
            y = {"apple", "banana", "cherry"}                        
        

You can perform set operations on sets, including union, intersection, and difference.

Dictionaries

Dictionaries in Python are collections of key-value pairs. Each key is associated with a value. Dictionaries are enclosed in curly braces, with each key-value pair separated by a colon and each pair separated by a comma. Here are some examples:


            x = {"name": "John", "age": 30}
            y = {"apple": 2, "banana": 3, "cherry": 4}  
        

You can access individual values of a dictionary using the key, and you can perform operations on dictionaries, including adding new key-value pairs, modifying existing key-value pairs, and deleting key-value pairs.

Conclusion

We had just covered the basics of Python syntax and data types. We looked at how Python programs are made up of statements and how indentation is used to indicate blocks of code. We also looked at several built-in data types in Python, including numbers, strings, booleans, lists, tuples, sets, and dictionaries. With this knowledge, you should be able to start writing simple Python programs.