products:ict:python:basic_syntax_and_data_types

Python's basic syntax and data types.

## Python Basic Syntax:

Python is known for its clean and readable syntax. Here are some fundamental aspects of Python's syntax:

1. Indentation: Python uses indentation (whitespace) to define blocks of code, like loops and functions, instead of using braces `{}` as in many other programming languages.

2. Comments: You can add comments using the `#` symbol. Comments are ignored by the interpreter and are useful for adding explanations to your code.

# This is a comment

3. Print Statements: The `print()` function is used to output text or variables to the console.

print(“Hello, world!”)

## Python Data Types:

Python supports various data types that allow you to represent and manipulate different kinds of values. Some common data types include:

1. Numbers: Python supports integers and floating-point numbers for numerical operations.

integer_num = 10

float_num = 3.14

2. Strings: Strings are used to represent text. They can be enclosed in either single (`'`) or double (`“`) quotes.

my_string = “Hello, Python!”

3. Lists: Lists are ordered collections of items, and they can contain elements of different types.

my_list = [1, 2, “three”, 4.0]

4. Tuples: Tuples are similar to lists, but they are immutable (cannot be modified after creation).

my_tuple = (1, 2, 3)

5. Dictionaries: Dictionaries store key-value pairs and are often used to represent mappings.

my_dict = {“name”: “John”, “age”: 30, “city”: “New York”}

6. Sets: Sets are unordered collections of unique elements.

my_set = {1, 2, 3, 3, 4}

7. Booleans: Booleans represent either `True` or `False` values and are used for logical operations.

is_true = True

is_false = False

## Variables and Assignment:

You can assign values to variables using the `=` operator. Variable names can contain letters, numbers, and underscores, but they cannot start with a number.

x = 5

name = “Alice”

Remember, Python is dynamically typed, which means you don't need to declare the data type of a variable explicitly.

products/ict/python/basic_syntax_and_data_types.txt · Last modified: 2023/08/31 20:46 by wikiadmin