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:
Contents
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,
age
,Age
, andAGE
are considered different variables.
2. Best Practices:
- Descriptive: Choose names that clearly reflect the variable’s purpose. For example,
customer_name
is better thanx
. - Lowercase with underscores: The most common convention is to use lowercase letters separated by underscores (e.g.,
total_cost
,is_admin
). - Avoid reserved words: Don’t use words that have special meanings in Python (like
if
,for
,def
). These are called keywords and cannot be used as variable names.
3. Examples:
- Good:
user_input
,calculation_result
,in_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_key
,num_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:
- Arithmetic Operators:
- Addition:
+
- Subtraction:
-
- Multiplication:
*
- Division:
/
- Floor Division (integer division):
//
- Modulus (remainder):
%
- Exponentiation:
**
- Addition:
- Comparison Operators:
- Equal to:
==
- Not equal to:
!=
- Greater than:
>
- Less than:
<
- Greater than or equal to:
>=
- Less than or equal to:
<=
- Equal to:
- 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:
**=
- Assign value:
- Logical Operators:
- Logical AND:
and
- Logical OR:
or
- Logical NOT:
not
- Logical AND:
- Identity Operators:
is
: ReturnsTrue
if both operands are the same object.is not
: ReturnsTrue
if both operands are not the same object.
- Membership Operators:
in
: ReturnsTrue
if a value is present in a sequence (e.g., string, list, tuple).not in
: ReturnsTrue
if a value is not present in a sequence.
- Bitwise Operators:
- Bitwise AND:
&
- Bitwise OR:
|
- Bitwise XOR:
^
- Bitwise NOT (Complement):
~
- Left Shift:
<<
- Right Shift:
>>
- Bitwise AND:
- Unary Operators:
- Unary plus:
+
- Unary minus:
-
- Unary plus:
- Ternary Operator:
x if condition else y
: Returnsx
if the condition isTrue
, otherwisey
.
0 Comments