Python Basics : A beginner's Guide

ยท

9 min read

Whether you are new to programming or a seasoned tech enthusiast, you've likely heard of Python. While some may think it's a type of snake, others recognize it as a powerful computer language.

Python Programming Language

In this comprehensive article, we will explore the basics of Python and delve into its various applications and use cases. Get ready to enhance your knowledge of this powerful programming language!

What is Python?

Python is a high-level, general-purpose programming language. It was created by Guido van Rossum, and released on 20th February 1991.

Python is among the most popular programming languages used in the tech space.

[Fig 2] Top Programming Languages of 2024: A Compilation of Key Statistics

In the next section, we will explore the features that make Python a popular and powerful language.

Features of Python

  1. Simple syntax: Uses English-like statements that are much easier to read and understand.

  2. High-level Language: Python is a high-level language, which means it abstracts low-level details like memory management and hardware interactions, allowing developers to focus on solving problems.

  3. Interpreted and Interactive: The code is executed line by line by an interpreter allowing quick development and testing as code changes can be immediately executed and tested interactively.

  4. Multi-paradigm: Python supports multiple programming paradigms, including procedural, object-oriented, and functional programming styles. This flexibility allows developers to choose the best approach for their projects.

  5. Dynamic Typing: Python is dynamically typed, meaning you don't need to declare the type of variables explicitly. This flexibility simplifies coding.

Uses of Python

Python is a versatile programming language that is used in various domains. It has many applications, including:

  1. Web Development: Python is a popular programming language that is extensively utilized for developing web applications and frameworks. Frameworks like Django and Flask are popular choices for building web applications.

  2. Data Science and Machine Learning: for data science, machine learning, and artificial intelligence applications. Libraries like NumPy, pandas, scikit-learn, TensorFlow, and PyTorch are extensively used for data manipulation, analysis, visualization, and machine learning tasks.

  3. Data analysis: Libraries like SciPy, Matplotlib, and SymPy provide tools for scientific computing and visualization.

  4. Game Development: Used for building game engines, developing game logic, and scripting game behaviour. Libraries like Pygame provide tools for developing 2D games.

  5. Desktop GUI Applications: Python can be used for developing desktop applications with graphical user interfaces (GUIs) using frameworks like Tkinter, PyQt, and wxPython.

  6. Web Scraping: Python is widely used for extracting data from websites. Libraries like BeautifulSoup and Scrapy provide tools for web scraping and data extraction.

  7. Automation and Scripting: Python is used for automating repetitive tasks, writing scripts for system administration, network automation, and task scheduling.

  8. Education: It is used for teaching programming concepts, algorithms, data structures, and problem-solving techniques.

Advantages of Python

  1. Easy to Learn: Emphasizes simplicity and readability, making it an excellent choice for beginners. Its straightforward syntax also makes it much easier to read.

  2. Cross Platform: Python is available on various platforms, including Windows, macOS, and Linux, making it a portable language that can run on different operating systems without modification.

  3. Has a large ecosystem: Python has large and vibrant libraries, frameworks, and tools that extend its capabilities. Popular libraries like NumPy, pandas, TensorFlow, and Django provide powerful tools for data manipulation, machine learning, web development, and more.

  4. Versatility and Applications: Python is a versatile language that can be used for a wide range of applications, including web development, data analysis, machine learning, artificial intelligence, scientific computing, automation, and scripting. Its extensive use across diverse domains makes it a valuable skill for developers and organizations.

  5. Integration and Interoperability: Python seamlessly integrates with other programming languages and technologies, allowing developers to leverage existing code and libraries written in languages like C/C++, Java, and .NET. This interoperability enhances code reusability and interoperability in mixed-technology environments.

  6. Has a large community of users - This makes it easy to find useful resources for learning and troubleshooting and streamlines contributions and collaboration in development.

Disadvantages of Python:

Despite its many advantages, Python also has some limitations and drawbacks that developers should be aware of:

  1. Memory Consumption: Python's dynamic typing can lead to higher memory consumption compared to statically typed languages. This can be a concern for memory-constrained environments or applications requiring efficient memory utilization.

  2. Mobile Development: it is less commonly used for mobile app development. While frameworks like Kivy and BeeWare enable mobile development with Python, the ecosystem and tooling for mobile development may not be as mature as those for other languages like Java or Swift.

  3. Less Speed: Python's execution speed is slower because it is interpreted compared to compiled languages like C/C++. This can be a disadvantage for applications requiring high performance, real-time processing, or low-level system programming.

With all that clear, let's dive into the process of installing and running Python on your machine.

Installation guide

Installing Python on your machine is a quick and streamlined process.

To install Python on Windows, head over to python.org and click on the Download button

Run the installation wizard and click on install

On Linux or MacOs, open your terminal and type

sudo apt-get install python
  • You will then be prompted to provide your password

  • Once installed, open your terminal window and type

python --version

โ€‹ then press enter

This should show the Python version installed on your machine

Congratulations ๐ŸŽ‰๐ŸŽ‰๐ŸŽ‰ now that Python is installed, let us write our first Python code.

Writing your first Python program

Open your code editor, if you do not have it installed you can use MuEditor (a lightweight beginner-friendly Python code editor).

Create a file named hello.py, copy and paste the snippet below

print("Hello World")

now save it and run

Great job! You have successfully written your first Python code. If you have experience with other programming languages and prefer to run your code within an integrated development environment (IDE), let us explore how to configure our most popular IDE (VSCode) for Python development

Configuring VSCode for Python Development

  • Head over to code.visualstudio.com and install VSCode

  • You will then be required to configure some extensions, below is a brief video tutorial on how to achieve that seamlessly

Now that you have a development environment where you can be able to craft your pieces of code, you might want to learn more about some of the basic concepts of python

Basics of Python

Did you know that Python, just like English, has its own set of rules that govern the way it is spoken and written? In English, we call this Grammar

In programming, we refer to this as syntax.

a) Syntax

Syntax in programming refers to the rules and structure governing the combination of symbols, keywords, and expressions used to create valid instructions or statements in a programming language. It defines the correct arrangement of elements within the code to convey meaning and perform specific tasks.

Introduction to Python Syntax - Pi My Life Up

b) Variables

Variables are used to store information to be manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves.

in Python, we declare variables using the following syntax

myName = "John"

c) Data Types

In Python, data types represent the type of data that can be stored and manipulated in variables

Here are some commonly used data types in Python:

  1. Integer (int): Integers are whole numbers without any decimal points. They can be positive, negative, or zero.

     age = 5
     length = 20
    
  2. Float (float): Floats represent real numbers and can include a decimal point.

     pi = 3.14
     price = 19.99
    
  3. String (str): Strings are sequences of characters enclosed within single, double, or triple quotes.

     name = 'John'
     message = "Hello, world!"
    
  4. Boolean (bool): Booleans represent truth values, True or False.

     is_valid = True
     is_open = False
    
  5. List: Lists are ordered collections of items, which can be of different data types. They are mutable, meaning their elements can be changed.

     codenumbers = [1, 2, 3, 4, 5]
     names = ['Alice', 'Bob', 'Charlie']
    
  6. Tuple: Tuples are similar to lists but are immutable, meaning their elements cannot be changed after creation.

     coordinates = (10, 20)
     rgb_values = (255, 128, 0)
    
  7. Dictionary (dict): Dictionaries are unordered collections of key-value pairs. Each key is unique and associated with a value.

     codeperson = {'name': 'Alice', 'age': 30, 'city': 'New York'}
    
  8. Set: Sets are unordered collections of unique elements. They are useful for performing mathematical set operations like union, intersection, etc.

     unique_numbers = {1, 2, 3, 4, 5}
     vowels = {'a', 'e', 'i', 'o', 'u'}
    

d) Conditionals

Conditional Statements in Python perform different actions depending on whether a specific condition evaluates to true or false. Conditional Statements are handled by IF-ELIF-ELSE statements and MATCH-CASE statements in Python.

Example of an IF-ELIF-ELSE statement in Python:

# Define a variable
x = 10
# Check if x is greater than 0
if x > 0:
    print("x is positive")
elif x == 0:  # Check if x is equal to 0
    print("x is zero")
else:  # If none of the above conditions are met
    print("x is negative")

In this example:

  • If the value of x is greater than 0, it prints "x is positive".

  • If the value of x is equal to 0, it prints "x is zero".

  • If the value of x is not greater than 0 and not equal to 0, it prints "x is negative".

This illustrates how IF-ELIF-ELSE statements work in Python to perform different actions based on specific conditions.

e) Functions

A function is a reusable block of code that executes a specific functionality when it is called.

A function can contain parameters - Parameters in Python are variables that are used to pass information into functions.

When calling a function we use arguments - arguments refer to the actual values that are passed into a function when it is called.

# Define a function to add any 2 numbers
def add_numbers(a, b):
    # Calculate the sum of a and b
    # a and b are parameters
    sum_result = a + b
    # Return the sum
    return sum_result

# Call the function with arguments 5 and 3
result = add_numbers(5, 3)
# Print the result
print("The sum is:", result)
# Output: The sum is: 8

Conclusion

In conclusion, Python stands as a versatile and powerful programming language accessible to beginners and experts alike. With its simple syntax, extensive libraries, and diverse applications across various domains, Python remains a top choice for developers, educators, and tech enthusiasts worldwide. Whether you're delving into web development, data science, machine learning, or automation, Python offers the tools and resources to turn your ideas into reality.

If you're looking to elevate your Python skills to the next level, we've got you covered! Here are some top-notch resources to help you master Python

Additional Resources

  1. Python for Beginners โ€“ Full Course [Programming Tutorial] - FreeCodeCamp Youtube Tutorial

  2. Python Tutorial (W3Schools) - Offers interactive step-by-step guide for beginners

  3. CS50โ€™s Introduction to Programming with Python - Offered by Harvard

  4. Python For Beginners - Offered by Microsoft via Microsoft Learn

Reach us

X(Twitter)- x.com/dsc_uoe

LinkedIn - linkedin.com/company/gdsc-uoe

GitHub - github.com/gdsc-uoe

Instagram - instagram.com/dsc_uoe

Discord - discord.gg/MTQA8PDXyU

ย