Python Syntax – Main Points

by | Apr 11, 2024 | Python | 0 comments


Python syntax refers to the rules and conventions that dictate how Python code is written and structured. Here are some fundamental aspects of Python syntax:

Statements and Indentation:

Python uses indentation to define blocks of code, such as loops, conditionals, and function definitions. Indentation is typically four spaces, but consistency is more important than the actual number of spaces.

Statements are typically written one per line, but you can use a semicolon (;) to write multiple statements on a single line.

Comments:

Comments start with the # character and extend to the end of the line. They are used to document code and are ignored by the Python interpreter.

#symbol is used for comment in python. The keyboard should is largely ‘Ctrl + /’, however in idle uses ‘Alt + 3’ to comment.

x = 10 # this is a commment

Variables and Data Types:

Variables are created by assigning a value to a name. Variable names can contain letters, numbers, and underscores but must start with a letter or underscore.

Python supports various data types, including integers, floats, strings, booleans, lists, tuples, dictionaries, and more.

It is possible to assign multiple variable to multiple values respectively in python.

x, y, z = 1, 2.3, "hello"
print(x, y, z)

Another form of multiple assignment is

x = y = z = 1
Multiple assignment makes swapping variable values very easy in python.

x, y = 1, 2
x, y = y, x

Variable naming conventions in Python:

1. Rules:

  • Start with a letter or underscore (_): Variable names must begin with a letter (a-z, A-Z) or an underscore. Numbers cannot be the first character.
  • Alphanumeric and underscores: The rest of the variable name can contain letters, numbers, and underscores.
  • Case-sensitive: Python is case-sensitive. So, ageAge, and AGE are considered different variables.

2. Best Practices:

  • Descriptive: Choose names that clearly reflect the variable’s purpose. For example, customer_name is better than x.
  • Lowercase with underscores: The most common convention is to use lowercase letters separated by underscores (e.g., total_costis_admin).
  • Avoid reserved words: Don’t use words that have special meanings in Python (like iffordef). These are called keywords and cannot be used as variable names.

3. Examples:

  • Good: user_inputcalculation_resultin_stock
  • Bad: x (unclear), userName (mixed case), 1stPlace (starts with a number)

4. Additional Tips:

  • Long names: For complex variable names, consider using abbreviations or prefixes (e.g., api_keynum_items).
  • Constants: If a variable’s value won’t change, use uppercase letters with underscores (e.g., PI = 3.14159).

Comprehensive list of operators in Python:

  1. Arithmetic Operators:
    • Addition: +
    • Subtraction: -
    • Multiplication: *
    • Division: /
    • Floor Division (integer division): //
    • Modulus (remainder): %
    • Exponentiation: **
  2. Comparison Operators:
    • Equal to: ==
    • Not equal to: !=
    • Greater than: >
    • Less than: <
    • Greater than or equal to: >=
    • Less than or equal to: <=
  3. Assignment Operators:
    • Assign value: =
    • Add and assign: +=
    • Subtract and assign: -=
    • Multiply and assign: *=
    • Divide and assign: /=
    • Floor divide and assign: //=
    • Modulus and assign: %=
    • Exponentiate and assign: **=
  4. Logical Operators:
    • Logical AND: and
    • Logical OR: or
    • Logical NOT: not
  5. Identity Operators:
    • is: Returns True if both operands are the same object.
    • is not: Returns True if both operands are not the same object.
  6. Membership Operators:
    • in: Returns True if a value is present in a sequence (e.g., string, list, tuple).
    • not in: Returns True if a value is not present in a sequence.
  7. Bitwise Operators:
    • Bitwise AND: &
    • Bitwise OR: |
    • Bitwise XOR: ^
    • Bitwise NOT (Complement): ~
    • Left Shift: <<
    • Right Shift: >>
  8. Unary Operators:
    • Unary plus: +
    • Unary minus: -
  9. Ternary Operator:
    • x if condition else y: Returns x if the condition is True, otherwise y.

Written by HintsToday Team

Related Posts

Python Dictionary in detail

What is Dictionary in Python? First of All it is not sequential like List. It is an non-sequential, unordered, redundant and mutable collection as key:value pairs. Keys are always unique but values need not be unique. You use the key to access the corresponding value....

read more

Get the latest news

Subscribe to our Newsletter

0 Comments

Submit a Comment

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