2.1.1 Statements
The following Python program demonstrates the simplicity and readability of the language:
print("This is my first page")
In this example, the print function is utilized to output the message enclosed in double quotation marks. This illustrates Python's straightforward syntax, where function like print is used to perform actions such as displaying text.
Python Comments
Lines that are not executed by the Python interpreter. They are used to provide explanations or notes for the code. Single-line comments start with the # symbol while multi-line comments can be created using triple quotes (""") at the beginning and the end as shown below.
# This is a single-line comment
print("K2 is the second-highest mountain in the world")
"""
This is a multi-line comment.
It can span multiple lines.
"""
print("Edhi Foundation is the largest volunteer ambulance network.")
Variables, Data Types and Input/Output
Variable
A variable is a storage container in a computer's memory, that allows storage, retrieval and manipulation of data. The value of a variable can change throughout the execution of a program:
age = 71
print("Ahmad lived for", age, "years")
age = 60
print("Iqbal lived for", age, "years")
Variable Naming Rules in Python
Variable names in Python must adhere to the following rules:
- The name must begin with a letter (a-z, A-Z) or an underscore (
_). - Subsequent characters can include letters, digits (0-9), or underscores (
_). - Variable names are case-sensitive, meaning
ageandAgeare considered two different variables. - Python's reserved keywords, such as
for,while,if, etc., cannot be used as variable names.
Tidbits
lightbulbAlways use meaningful names for variables to make your code easier to understand. For example, use
ageinstead ofa.
Creating Different Types of Variables
In Python, you can create variables of different types to store various kinds of data. Here are some common types of variables:
- Integer (int): Stores whole numbers. Example:
age = 17 - Floating-point (float): Stores decimal numbers. Example:
price = 19.99 - String (str): Stores text. Example:
name = "Ali" - Boolean (bool): Stores True or False. Example:
is_student = True
Tidbits
lightbulbIt's a good practice to use lowercase letters for variable names and underscores to separate words in variable names (e.g.,
student_name).
Input and Output Operations
Input and output operations allow you to interact with the user. You can ask the user to enter data (input) and display information to the user (output).
- Input: Use the
input()function to get user input. Theinput()function displays a message on the screen and waits for the user to type something and press Enter. The text entered by the user is then stored in a variable. For example:
name = input("Enter your name:")
- Output: Use the
print()function to display information on the screen. Theprint()function takes one or more arguments and displays them. For example:
print("Hello," + name + "!")
Handling Integer and Float Inputs
To handle numeric inputs, you typically use the int() or float() functions to convert input strings to integers or floating-point numbers, respectively.
Integer Inputs
# Example: Handling integer input
user_age = int(input("Enter your age: "))
print("Your age is:", user_age)
Float Inputs
# Example: Handling float input
user_height = float(input("Enter your height in meters: "))
print("Your height is:", user_height, "meter")