# This is a comment. Python ignores comments.
# You can write comments to explain your code.
# Print "Hello, World!" to the console
print("Hello, World!")
Hello, World!
Here's what this code does:
Lines that start with # are comments. They are not executed and are used to provide explanations to human readers.
print("Hello, World!") is the code that prints the text "Hello, World!" to the console. In Python, print() is a built-in function used to display text or values on the screen.
Script Mode of Python Programming¶
- Script mode in Python refers to writing Python code in a text file (typically with a .py extension) and then executing that file as a script. This is a common way to create Python programs that perform specific tasks or solve particular problems. Here's a step-by-step tutorial on how to write and execute Python scripts:
Step 1: Set Up Your Development Environment
Before you start, ensure you have Python installed on your computer. If not, download it from the official website.
You can write Python scripts using any text editor, but using an integrated development environment (IDE) like Visual Studio Code, PyCharm, or IDLE can make your coding experience more efficient.
Step 2: Write Your Python Script
- Open your chosen text editor or IDE and create a new file. Then, write your Python code in this file. Here's a simple example of a Python script that calculates the square of a number:
# This is a Python script to calculate the square of a number
# Input: Get a number from the user
number = float(input("Enter a number: "))
# Calculate the square
square = number ** 2
# Output: Display the result
print(f"The square of {number} is {square}")
Enter a number: 2 The square of 2.0 is 4.0
In this script:
- We take user input using the input() function.
- We calculate the square of the number entered by the user.
- We use the print() function to display the result.
Step 3: Save Your Python Script
- Save your Python script with a .py file extension. For example, you can save it as square_calculator.py.
Step 4: Run Your Python Script
Now that your script is saved, open your command prompt or terminal and navigate to the directory where your script is located.
To run the script, use the python command followed by the name of your script file:
python square_calculator.py
- Your script will execute, and you should see the program prompting you for input and then displaying the calculated square.
Identifiers in Python¶
- A Python identifier is a name used to identify a variable, function, class, module or other object.
Rules for Python Identifiers:¶
Must Begin with a Letter (a-z, A-Z) or an Underscore (_):
- Valid: my_variable, _private_variable
- Invalid: 123_variable, @special_var
Can Contain Letters, Digits (0-9), or Underscores and not allow punctuation characters such as @, $, and % etc:
- Valid: my_var2, count_123
- Invalid: my-var, my.var
Case-Sensitive:
- Python treats identifiers as case-sensitive, meaning myVar and myvar are different identifiers.
Cannot Be a Reserved Word:
- Python has reserved words (also known as keywords) that cannot be used as identifiers because they have special meanings in the language.
- For example, if, else, while, class, etc., are reserved words.
Naming conventions for Python identifiers¶
Python Class Names Start with an Uppercase Letter and All Other Identifiers Start with a Lowercase Letter:
- Class names in Python conventionally start with an uppercase letter to distinguish them from other identifiers. For example, MyClass or BankAccount.
- dentifiers other than class names typically start with a lowercase letter. This includes variables, functions, and module names. For example, my_variable, calculate_total_amount.
Starting an Identifier with a Single Leading Underscore Indicates It's Private:
- When an identifier starts with a single leading underscore (e.g., _private_var), it is considered a convention to indicate that the identifier is intended for internal use within a module or class and should not be accessed directly from outside.
Starting an Identifier with Two Leading Underscores Indicates Strongly Private:
- When an identifier starts with two leading underscores (e.g., __strongly_private_var), it indicates a stronger level of privacy. In this case, the identifier's name is "mangled" to make it less likely to clash with subclasses or other classes.
If the Identifier Also Ends with Two Trailing Underscores, It's a Language-Defined Special Name:
- Identifiers that start and end with double underscores (e.g.,
__init__
) are reserved for special methods or attributes defined by the Python language. These are often referred to as "magic methods" and have specific meanings in Python, such as__init__
for constructors and__str__
for string representations.
- Identifiers that start and end with double underscores (e.g.,
User Input in Python¶
- In python we are able to ask the user for input.
- Python stops executing when it comes to the input() function, and continues when the user has given some input.
# Get user input and store it in a variable
user_input = input("Enter your name: ")
# Display the user's input
print("Hello, " + user_input + "!")
Enter your name: Raj Hello, Raj!
Printing to the Screen in Python¶
- In Python, you can print text and variables to the screen or console using the print() function. This function is one of the most fundamental ways to display output from your Python programs.
- Basic Printing:To print text or strings to the screen, simply pass the text as an argument to the print() function. For example:
print("Hello, World!")
Hello, World!