Getting Started

Syntax and Structure

Python syntax defines the rules for writing valid Python code, including statements, indentation, comments, variables, data types, and operators.

Python Statements

A Python program consists of a sequence of statements. Each statement performs an action and is typically written on its own line.

Example:

x = 5
print(x)

Unlike JavaScript, Python does not use semicolons to end statements—lines are terminated simply by a newline character. Indentation is critical in Python and defines the structure of code blocks.

Comments in Python

Comments are used to document code and prevent execution of specific lines.

Single-line comment:

pythonCopyEdit # This is a comment

Alternatively, multi-line strings (""") are sometimes used as comments, especially for documentation.

Variables and Constants

Variables store data values and are created by assignment. Python does not require declaring the variable type explicitly.

Declaring Variables:

pythonCopyEditname = "Alice"
age = 30

Python does not have a built-in constant type, but by convention, uppercase names indicate constants:

pythonCopyEditPI = 3.14159  # Treated as a constant by convention

Variable Naming Rules

  • Must begin with a letter or underscore (_).

  • Cannot use reserved Python keywords.

  • Case-sensitive (my_var and My_Var are different).

Data Types in Python

Python is dynamically typed, so variable types are determined at runtime.

Python has several built-in primitive types:

  • String"Hello"

  • Integer42

  • Float3.14

  • BooleanTrue, False

  • None – Represents an absence of value (similar to null in JavaScript)

Example:

pythonCopyEditname = "Alice"
score = 98.5
is_active = True

Non primitive data types include:

  • List – Ordered collection of values.

  • Tuple – Immutable ordered collection.

  • Dictionary – Collection of key-value pairs.

  • Set – Unordered collection of unique elements.

  • Function – Reusable block of code.

Example:

pythonCopyEditperson = {"name": "Alice", "age": 30}
numbers = [1, 2, 3, 4]

Operators

Python provides a wide range of operators:

Arithmetic Operators

Used for basic math operations: +, -, *, /, //, %, **

Comparison Operators

Used to compare values: ==, !=, >, <, >=, <=

Logical Operators

Used to combine conditions: and, or, not

Conclusion

This section introduced Python's syntax, variables, data types, and operators. In the next section, we’ll dive into control flow structures like conditionals and loops to write more dynamic and powerful programs.

Python Statements

A Python program consists of a sequence of statements. Each statement performs an action and is typically written on its own line.

Example:

x = 5
print(x)

Unlike JavaScript, Python does not use semicolons to end statements—lines are terminated simply by a newline character. Indentation is critical in Python and defines the structure of code blocks.

Comments in Python

Comments are used to document code and prevent execution of specific lines.

Single-line comment:

pythonCopyEdit # This is a comment

Alternatively, multi-line strings (""") are sometimes used as comments, especially for documentation.

Variables and Constants

Variables store data values and are created by assignment. Python does not require declaring the variable type explicitly.

Declaring Variables:

pythonCopyEditname = "Alice"
age = 30

Python does not have a built-in constant type, but by convention, uppercase names indicate constants:

pythonCopyEditPI = 3.14159  # Treated as a constant by convention

Variable Naming Rules

  • Must begin with a letter or underscore (_).

  • Cannot use reserved Python keywords.

  • Case-sensitive (my_var and My_Var are different).

Data Types in Python

Python is dynamically typed, so variable types are determined at runtime.

Python has several built-in primitive types:

  • String"Hello"

  • Integer42

  • Float3.14

  • BooleanTrue, False

  • None – Represents an absence of value (similar to null in JavaScript)

Example:

pythonCopyEditname = "Alice"
score = 98.5
is_active = True

Non primitive data types include:

  • List – Ordered collection of values.

  • Tuple – Immutable ordered collection.

  • Dictionary – Collection of key-value pairs.

  • Set – Unordered collection of unique elements.

  • Function – Reusable block of code.

Example:

pythonCopyEditperson = {"name": "Alice", "age": 30}
numbers = [1, 2, 3, 4]

Operators

Python provides a wide range of operators:

Arithmetic Operators

Used for basic math operations: +, -, *, /, //, %, **

Comparison Operators

Used to compare values: ==, !=, >, <, >=, <=

Logical Operators

Used to combine conditions: and, or, not

Conclusion

This section introduced Python's syntax, variables, data types, and operators. In the next section, we’ll dive into control flow structures like conditionals and loops to write more dynamic and powerful programs.

Python Statements

A Python program consists of a sequence of statements. Each statement performs an action and is typically written on its own line.

Example:

x = 5
print(x)

Unlike JavaScript, Python does not use semicolons to end statements—lines are terminated simply by a newline character. Indentation is critical in Python and defines the structure of code blocks.

Comments in Python

Comments are used to document code and prevent execution of specific lines.

Single-line comment:

pythonCopyEdit # This is a comment

Alternatively, multi-line strings (""") are sometimes used as comments, especially for documentation.

Variables and Constants

Variables store data values and are created by assignment. Python does not require declaring the variable type explicitly.

Declaring Variables:

pythonCopyEditname = "Alice"
age = 30

Python does not have a built-in constant type, but by convention, uppercase names indicate constants:

pythonCopyEditPI = 3.14159  # Treated as a constant by convention

Variable Naming Rules

  • Must begin with a letter or underscore (_).

  • Cannot use reserved Python keywords.

  • Case-sensitive (my_var and My_Var are different).

Data Types in Python

Python is dynamically typed, so variable types are determined at runtime.

Python has several built-in primitive types:

  • String"Hello"

  • Integer42

  • Float3.14

  • BooleanTrue, False

  • None – Represents an absence of value (similar to null in JavaScript)

Example:

pythonCopyEditname = "Alice"
score = 98.5
is_active = True

Non primitive data types include:

  • List – Ordered collection of values.

  • Tuple – Immutable ordered collection.

  • Dictionary – Collection of key-value pairs.

  • Set – Unordered collection of unique elements.

  • Function – Reusable block of code.

Example:

pythonCopyEditperson = {"name": "Alice", "age": 30}
numbers = [1, 2, 3, 4]

Operators

Python provides a wide range of operators:

Arithmetic Operators

Used for basic math operations: +, -, *, /, //, %, **

Comparison Operators

Used to compare values: ==, !=, >, <, >=, <=

Logical Operators

Used to combine conditions: and, or, not

Conclusion

This section introduced Python's syntax, variables, data types, and operators. In the next section, we’ll dive into control flow structures like conditionals and loops to write more dynamic and powerful programs.

© Layer Docs

Made by Alejandro · Powered by Framer

Create a free website with Framer, the website builder loved by startups, designers and agencies.