Table of contents
Variables & data literals
Variables: Variables are used to store values that can be used throughout the program. In Python, variables do not have predefined types - a variable can store a number, string or any other data type. Some examples of variable declaration in Python:
name = "John" # String
age = 30 # Integer
gpa = 3.6 # Float
is_approved = True # Boolean
Data Literals: Data literals are values that represent data of a particular type. Some examples of data literals in Python are:
Strings:
'Hello'
"Hello"
Integers:
10
0
-5
Floats:
10.5
0.0
-3.14
Booleans:
True
False
Lists:
[1, 2, 3]
['a', 'b', 'c']
Expressions
Expressions: Expressions are combinations of variables, literals and operators that evaluate to a value. Some examples of expressions in Python:
1 + 2 # Addition
age - 5 # Subtraction
name * 2 # String repetition
5 % 3 # Modulus
gpa > 3.0 # Comparison
Complex expressions: in Python can be created using:
Arithmetic operators (+, -, , /, %, *)
Comparison operators (==, !=, >, <, >=, <= )
Logical operators (and, or, not)
Boolean operators (True, False)
Function calls
Indexing (lists, strings)
An example of a complex expression:
(num1 + num2 * 3) % 5 > 1 and len(name) != 0
This expression contains:
Arithmetic operators (+, *)
Comparison operator (>)
Logical operator (and)
Boolean literals (True, False)
Long expressions: in Python are expressions that span multiple lines. They can be achieved using:
Parentheses ()
Square brackets []
Curly braces {}
Example:
result = (num1 +
num2 *
3) % 5
Here the expression num1 + num2 * 3
is split across multiple lines using parentheses.
Another example:
result = [x * x
for x in range(10)
if x % 2 == 0]
Here the list comprehension is split across multiple lines using square brackets.
Long expressions make complex logic more readable by breaking it down into multiple lines. However, overly long expressions can become difficult to understand and maintain. So it's best to split complex logic into multiple, simpler expressions wherever possible.
Comments
Comments and triple-quoted strings have the following roles in Python:
Comments: Comments are lines in the code that are ignored by the Python interpreter. They are used to add notes or explanations to the code to make it more readable for human programmers.
There are two types of comments in Python:
- Line comments - Use # symbol. Everything after # till the end of the line is ignored. Example:
# This is a comment
print("Hello")
- Multi-line comments - Use triple quote ''' or """ symbols. Everything between the triple quotes is ignored. Example:
"""This is a
multi-line
comment"""
print("Hello")
Comments are useful to:
Explain what a particular section of code is doing
Disable a piece of code temporarily
Add notes or reminders for future reference
Triple quoted strings: Triple quoted strings are string literals that span multiple lines. They use either ''' or """ as delimiters. Example:
para = """This is a
multi-line
string"""
message = '''Hello,
how are you?'''
Triple-quoted strings are useful when you have a string that contains line breaks or spans multiple lines. They allow strings to maintain line breaks, whitespace and format.
So in summary, comments are used to add notes to the code for humans to read, while triple-quoted strings are actual string literals that span multiple lines and maintain line breaks. Both serve different purposes but use similar triple quote syntax.
Statements
A statement is a line of code that performs an action in Python. Statements can be:
Assignment statements
Expression statements
Function calls
Conditional statements (if, for, while)
Class definitions
import statements
Some examples of statements in Python:
x = 5 # Assignment statement
print("Hello") # Expression statement
sum(1, 2, 3) # Function call
if x > 2: # Conditional statement
print(x)
class Dog: # Class definition
pass
import math # Import statement
Statements are made up of the following fundamental elements:
Variables - Used to store values
Expressions - Evaluate to a value
Literals - Represent data of a particular type (strings, integers, floats, booleans)
Operators - Perform operations on variables and literals (+, -, *, /, ==, !=)
Keywords - Have special meaning in Python (if, for, def, class, import)
When these elements are combined according to the syntax of Python, they form statements that perform actions.
For example:
x = 5 # Assignment statement
result = x + y * 2 # expression used in assign statement
So in summary, statements are lines of code that perform an action in Python. They are formed by combining variables, expressions, literals, operators and keywords according to Python's syntax.
Disclaim: This article is created with Rix AI. I ask the questions in order to learn Python in a logic manner. It is not so easy to ask the right questions to create these kind of articles but I encourage you to ask any questions below maybe I can answer. Enjoy learning. ๐