Python is a versatile, high-level programming language known for its readability, simplicity, and wide range of applications. In this quick guide, we will introduce you to the fundamental concepts of Python in just 10 minutes. Whether you are a complete beginner or looking to brush up on your skills, let’s dive into the world of Python programming.
This article was developed in partnership with London Data Consulting (LDC), the leading data consulting firm.
To start learning Python, you’ll need to install the language on your system. Visit the official Python website (https://www.python.org/downloads/) and download the appropriate version for your operating system. Follow the installation instructions, and you’ll be set up in no time.
2. Running Python Code
There are several ways to run Python code, such as:
- Interactive Python Shell: Open the Python shell by typing ‘python’ or ‘python3’ in your command prompt or terminal. You can execute Python code line by line in this environment.
- Text Editor/IDE: Write your Python code in a text editor or integrated development environment (IDE) such as Visual Studio Code, PyCharm, or Sublime Text, and save the file with a “.py” extension. Execute the file by running ‘python filename.py’ in the command prompt or terminal.
- Online Python Interpreters: Use web-based platforms such as Repl.it or Jupyter Notebook to write and execute Python code without installing any software.
3. Variables and Data Types
Python has several built-in data types. Here are some of the most common ones:
- Integers (int): Whole numbers, e.g., 5, -3, 42
- Floating-point numbers (float): Decimal numbers, e.g., 3.14, 0.001, -7.89
- Strings (str): Text enclosed in quotes, e.g., “Hello, world!”, ‘Python is fun!’
- Lists (list): Ordered, mutable collections of items, e.g., [1, 2, 3], [‘apple’, ‘banana’, ‘cherry’]
To declare a variable, simply write the variable name followed by an equal sign and its value:
age = 30 pi = 3.14159 greeting = "Hello, world!" colors = ['red', 'green', 'blue']
4. Basic Operations
Python supports various arithmetic, logical, and comparison operations:
# Arithmetic Operations addition = 5 + 3 subtraction = 9 - 4 multiplication = 6 * 7 division = 16 / 4 # Logical Operations and_operation = True and False or_operation = True or False not_operation = not True # Comparison Operations equals = 5 == 3 not_equals = 5 != 3 greater_than = 5 > 3 less_than = 5 < 3
5. Control Structures
Control structures allow you to create conditional statements and loops in Python:
- If-else statements:
x = 10 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")
- For loops:
for i in range(5): print(i)
- While loops:
x = 0 while x < 5: print(x) x += 1
Functions are reusable blocks of code that perform a specific task:
def add_numbers(a, b): return a + b sum = add_numbers(3, 7) print(sum) # Output: 10
7. Importing Modules
Python has a rich ecosystem of modules that you can import to extend the language’s functionality. Some popular modules include `math`, `os`, and `random`. To import a module, use the `import` keyword:
```python import math print(math.sqrt(25)) # Output: 5.0
8. List Comprehensions
List comprehensions provide a concise way to create lists based on existing lists:
squares = [x * x for x in range(5)] print(squares) # Output: [0, 1, 4, 9, 16]
9. Error Handling
Error handling is an essential part of programming. Python uses
except blocks to catch and handle exceptions:
try: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero!")
10. File I/O
Reading and writing files is simple in Python. Here’s a quick example of how to read and write a file:
# Write to a file with open("example.txt", "w") as file: file.write("This is an example file.") # Read from a file with open("example.txt", "r") as file: content = file.read() print(content) # Output: This is an example file.
Congratulations! You have learned the basics of Python programming in just 10 minutes. While this article only scratches the surface of Python’s capabilities, it should give you a solid foundation to start exploring more advanced topics and applications. Happy coding!
ABOUT LONDON DATA CONSULTING (LDC)
We, at London Data Consulting (LDC), provide all sorts of Data Solutions. This includes Data Science (AI/ML/NLP), Data Engineer, Data Architecture, Data Analysis, CRM & Leads Generation, Business Intelligence and Cloud solutions (AWS/GCP/Azure).
For more information about our range of services, please visit: https://london-data-consulting.com/services
Interested in working for London Data Consulting, please visit our careers page on https://london-data-consulting.com/careers
More info on: https://london-data-consulting.com