Theory of Intelligence
PYTHON PROGRAMMING•Foundations•Syntax Basics•2.1 Statements, Indentation & Comments

2.1.1 Statements

Subtopics: Statements, Indentation, Comments and Docstrings.

2.1.1 Statements

The following Python program demonstrates the simplicity and readability of the language:

PYTHON
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.

PYTHON
# 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:

PYTHON
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 age and Age are considered two different variables.
  • Python's reserved keywords, such as for, while, if, etc., cannot be used as variable names.

Tidbits

lightbulb

Always use meaningful names for variables to make your code easier to understand. For example, use age instead of a.

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

lightbulb

It'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. The input() 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:
PYTHON
name = input("Enter your name:")
  • Output: Use the print() function to display information on the screen. The print() function takes one or more arguments and displays them. For example:
PYTHON
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

PYTHON
# Example: Handling integer input
user_age = int(input("Enter your age: "))
print("Your age is:", user_age)

Float Inputs

PYTHON
# Example: Handling float input
user_height = float(input("Enter your height in meters: "))
print("Your height is:", user_height, "meter")

Discuss theoretical concepts, mathematical formulas, and architectural foundations.

Supports inline code with `backticks`
No comments yet. Be the first to start the discussion!