Python Common Commands Reference

Python, a popular high-level programming language, offers a wide range of commands and functionalities that are essential for developing robust and efficient applications. In this article, we’ll delve into a reference of commonly used Python commands, providing brief descriptions and examples of their usage.

1. Basic Commands

  • print(): Used to display text or variable values on the console.
pythonprint("Hello, World!")  # Outputs: Hello, World!

  • input(): Allows users to input data, typically in string format.
pythonuser_input = input("Enter your name: ")  # Prompts the user to enter a name
print("Hello, " + user_input + "!") # Outputs: Hello, [user_input]!

2. Data Types and Variables

  • Integers (int), Floats (float), Strings (str), Lists (list), Tuples (tuple), Dictionaries (dict), Sets (set): These are Python’s fundamental data types.
python# Examples of data types
num_int = 10 # Integer
num_float = 3.14 # Float
name = "Alice" # String
my_list = [1, 2, 3] # List
my_tuple = (4, 5, 6) # Tuple
person = {"name": "Bob", "age": 25} # Dictionary
unique_items = {7, 8, 9} # Set

3. Control Flow Statements

  • if, elif, else: Used for conditional execution based on specific conditions.
pythonx = 5
if x > 0:
print("x is positive")
elif x < 0:
print("x is negative")
else:
print("x is zero") # Outputs: x is positive

  • for: Used for iterating over sequences (like lists or strings).
pythonfor i in range(5):
print(i) # Outputs: 0, 1, 2, 3, 4

  • while: Executes a block of code repeatedly while a condition is true.
pythoncount = 0
while count < 5:
print(count)
count += 1 # Outputs: 0, 1, 2, 3, 4

4. Functions

  • Functions are blocks of code that perform a specific task and can be reused throughout a program.
pythondef greet(name):
return "Hello, " + name + "!"

print(greet("Eve")) # Outputs: Hello, Eve!

5. Modules and Libraries

  • Python has a vast ecosystem of modules and libraries that extend its functionality. Commonly used ones include:

    • math: Provides various mathematical functions and constants.
    • os: Allows interaction with the operating system.
    • numpy: A library for numerical computation.
    • pandas: A library for data analysis and manipulation.
    • requests: A library for making HTTP requests.

6. File Operations

  • Python provides commands for reading, writing, and managing files.
python# Writing to a file
with open("example.txt", "w") as file:
file.write("Hello, World!")

# Reading from a file
with open("example.txt", "r") as file:
content = file.read()
print(content) # Outputs: Hello, World!

7. Error Handling

  • try-except: Used for handling errors and exceptions gracefully.
pythontry:
result = 10 / 0 # This will raise a ZeroDivisionError
except ZeroDivisionError:
print("Cannot divide by zero") # Outputs: Cannot divide by zero

This reference covers only a fraction of Python’s capabilities, but it provides a solid foundation for understanding the commonly used commands and functionalities in Python programming. As you progress in your Python journey, you’ll discover more advanced commands and libraries that will help you build even more complex and powerful applications.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *