Learn Python, Microsoft 365 and Google Workspace
Connect with me: Youtube | LinkedIn | WhatsApp Channel | Web | Facebook | Twitter
web development
, data science
, machine learning
, and automation
.PyCharm
, or even a simple text editor like Notepad
.Important: Python source code files always use the
.py
extension.
Example #1:
message = 'Python is fun'
# print the string message
print(message)
Output:
Python is fun
Example #2:
# Print a string:
print("Hello, World!")
# Print a number:
print(10)
# Print a variable:
x = 5
print(x)
# Print multiple objects on the same line:
print("Hello", "World")
# Print multiple objects on separate lines:
print("Hello")
print("World")
# Print with a custom separator:
print("Hello", "World", sep=", ")
# Print with a custom ending character:
print("Hello", "World", end="!")
See also:
Comments in Python are non-executable lines of code and ignored by the Python interpreter when the code is executed.
# This is a single-line comment
print("Hello, World!")
Purpose: Variables provide a way to store and manage data that can be used and manipulated throughout a program. They make programs more flexible and allow for dynamic data storage.
Assignment statement: in Python is used to assign a value to a variable. Its primary purpose is to store and manage data within a program.
Imagine variables as labeled boxes:
Example #3: Storing a name
name = "Muhammad Hamza"
print(name)
Example #4: Tracking a score:
score = 0
score = score + 10 # adds 10 to the score
print(score)
Example #5: Remembering a favorite color
favorite_color = "blue" #stores "blue" in variable
print(favorite_color)
Example #6: Calculating the area of a rectangle
length = 10
width = 5
# calculates the area
area = length * width
print(area)
Key Points:
See also:
Example #7:
name = "Ahmad"
greeting = 'Hello, world!'
favorite_song = "Let It Go" # Double quotes can also contain single quotes
Example #8:
age = 25
num_pets = 3
lucky_number = 7
Example #9:
height = 1.70 # meters
price = 19.99
distance = 3.14159 # Pi
Example #10:
is_hungry = True
is_raining = False
has_finished = True
print(type(name)) # Output: <class 'str'>
print(type(age)) # Output: <class 'int'>
print(type(height)) # Output: <class 'float'>
print(type(is_hungry)) # Output: <class 'bool'>
See also:
1. Arithmetic Operators:
+
(addition), -
(subtraction), *
(multiplication), /
(division), //
(floor division), %
(modulo), **
(exponentiation)Example #11:
result = 10 + 5 # Addition
difference = 15 - 7 # Subtraction
product = 4 * 6 # Multiplication
quotient = 12 / 3 # Division
integer_quotient = 17 // 4 # Floor division
remainder = 25 % 4 # Modulo
square = 5 ** 2 # Exponentiation
2. Comparison Operators:
==
(equal to), !=
(not equal to), >
(greater than), <
(less than), >=
(greater than or equal to), <=
(less than or equal to)Example #12:
is_equal = 7 == 7 # True
is_greater = 12 > 9 # True
is_less_or_equal = 5 <= 5 # True
Input in Python:
input()
function:
name = input("Enter your name: ")
age = int(input("Enter your age: ")) # Convert string input to integer
Output in Python:
print()
function:
print("Hello, world!")
print("Your name is", name, "and you are", age, "years old.")
print("The answer is:", 42)
The if statement in MATLAB is a conditional statement that allows you to execute a block of code only if a certain condition is met.
Make decisions using if
, elif
, and else
statements.
The general syntax of the if statement is as follows:
if condition
statements
The condition
can be any logical expression. If the condition is evaluated to true
, the block of statements
is executed. Otherwise, the block of statements
is skipped.
Here is a simple example of an if statement in MATLAB:
Example #14:
x = 10
if x > 5:
print('x is greater than 5.')
This code will print the message x is greater than 5.
to the console.
You can also use elif
statements to check for multiple conditions. The general syntax of the elif statement is as follows:
elif condition
statements
end
If the condition
for the if statement is evaluated to false
, the python interpreter will check the condition
for the first elif statement. If the condition for the elif statement is evaluated to true
, the corresponding block of statements
is executed. Otherwise, the python interpreter will check the condition
for the next elif statement, and so on.
Here is an example of an if statement with an elif statement:
Example #15:
x = 3
if x > 5:
print('x is greater than 5.')
elif x < 5:
print('x is less than 5.')
This code will print the message “x is less than 5.” to the console.
You can also use an else statement to check for all other conditions. The general syntax of the else statement is as follows:
else
statements
end
If all of the conditions for the if and elseif statements are evaluated to false
, the block of statements
in the else
statement is executed.
Here is an example of an if statement with an elif
statement and an else
statement:
Example #16:
x = 2
if x > 5:
print('x is greater than 5.')
elif x == 5:
print('x is equal to 5.')
else:
print('x is less than 5.')
This code will print the message “x is less than 5.” to the console.
for
and while
loops.Example #17:
for i in range(5):
print(i)
Example #18:
for i in range(5):
print("Python")
See also:
Example #19: Counting Up to a Number:
count = 1 # Start counting at 1
while count <= 10: # Keep counting as long as we're less than or equal to 10
print(count) # Print the current number
count += 1 # Add 1 to the count for the next round
Which of the following is the correct syntax for the print statement in Python?
Which of the following statements will print the value of the variable x?
What will be the output of the following code?
print("Hello, world!")
What is the purpose of the sep argument in the print function?
What is the purpose of the end argument in the print function?
What is the primary purpose of comments in Python code?
Which of the following is the correct syntax for a single-line comment in Python?
What is the difference between == and = in Python?
Which operator checks if two values are greater than or equal to each other?
What is the result of 5 <= 5?
What is the output of x = 10; x //= 3?
Which operator is used to raise a number to a power?
What is the output of 45 // 7?
What is the difference between == and = in Python?
What is the result of the expression 3 + 5 * 2 in Python?
Which data type is used to represent decimal numbers in Python?
Which of the following is an example of a boolean value in Python?
Which scalar data type is used to represent textual data in Python?
What is the default type of a numerical literal without a decimal point in Python?
Which of the following is NOT a scalar data type in Python?
What is the output of type(42)?
What is the result of 3 + 4.5?
How do you create a string in Python?
What is the result of 5 > 3 and not (2 == 2)?
What is the correct syntax for a for loop in Python?
What will be the output of the following code?
for i in range(5):
print(i * 2)
What is the output of the following code?
count = 0
while count < 3:
print(count)
count += 1
Which of the following correctly represents the syntax of an if statement in Python?
What is the purpose of the else block in an if statement?
What will be the output of the following code?
x = 10
y = 5
if x < y:
print("x is greater than y")
Control Flow: