Introduction to Software Systems
  • About
  • Introduction
  • Software Engineering
    • Software System
    • Software Product Development
    • Computer Networks
  • Terminal Programming
    • BASH - Basic Commands
    • BASH - Conditions and Loops
    • Worked-out Examples
    • Practice Questions
  • Databases
    • Structured Databases
      • SQL Queries
      • Worked-out Example
    • Unstructured Databases
      • NoSQL Queries
      • Worked-out Example
  • Object Oriented Programming
    • Python - Introduction
    • Python - Basic Concepts
    • Python - Inbuilt Datastructures
    • Python - Conditions and Loops
    • Python - Lambda, Functions, Class/Objects
    • Worked-out Examples
  • WEB TECHNOLOGIES
    • HTML
    • CSS
    • Native JavaScript - Basics
    • Native JavaScript - Conditional Statements and Loops
    • Native JavaScript - Data Structures
    • JavaScript - Scope, Functions, Type Conversion
Powered by GitBook
On this page
  • Key Features of Python
  • Applications of Python
  • Getting Started with Python
  • Installation
  • Python Version Check
  • Python Indentation
  1. Object Oriented Programming

Python - Introduction

This page provides some introduction about Python Programming Language.

Python is a high-level, general-purpose programming language that has gained immense popularity since its creation by Guido van Rossum in the late 1980s and its official release in 1991. Known for its simplicity and readability, Python is designed to emphasize code clarity, making it an excellent choice for both beginners and experienced programmers alike.

Key Features of Python

  • Readability: Python's syntax closely resembles English, which makes it easier to understand and write code. This focus on readability allows developers to express concepts in fewer lines of code compared to other programming languages.

  • Versatility: Python is a multi-paradigm language that supports procedural, object-oriented, and functional programming styles. This flexibility enables developers to choose the approach that best suits their project needs.

  • Interpreted Language: As an interpreted language, Python executes code line by line, which simplifies debugging and allows for rapid prototyping. This feature is particularly beneficial for beginners who are learning programming concepts.

  • Extensive Libraries: Python comes with a comprehensive standard library that supports various tasks, from web development to data analysis and machine learning. There are also numerous third-party libraries available that extend its functionality even further.

  • Cross-Platform Compatibility: Python can run on various operating systems, including Windows, macOS, and Linux, making it highly portable.

Applications of Python

Python's versatility allows it to be used in a wide range of applications:

  • Web Development: Frameworks like Django and Flask enable developers to build robust web applications efficiently.

  • Data Science and Machine Learning: Libraries such as Pandas, NumPy, and TensorFlow make Python a popular choice for data analysis and machine learning projects.

  • Automation and Scripting: Python is often used for automating repetitive tasks and system scripting due to its ease of use.

  • Game Development: Tools like Pygame allow developers to create games using Python.

  • Scientific Computing: Python is widely used in scientific research for simulations and complex calculations due to libraries like SciPy.

Getting Started with Python

To begin programming in Python, you can download the latest version from the official Python website. You can write Python code using a simple text editor or an Integrated Development Environment (IDE) like PyCharm or Visual Studio Code. Here’s a basic example of a Python program:

print("Hello, World!")

This simple program outputs "Hello, World!" to the console, demonstrating how easy it is to get started with Python.

Installation

  • Run sudo apt install python3 to install python in Ubuntu.im

Python Version Check

To check the version of Python, you can use the following methods:

  1. On the command line: Open a terminal or command prompt and run:

python --version

Or for Python 3 specifically: This will display the installed Python version

python3 --version
  1. You can use the sys module to check the version within a Python script: This will print detailed version information

import sys
print(sys.version)
  1. For more concise version info in a script: This returns a named tuple with the major, minor, and micro version numbers

import sys
print(sys.version_info)
  1. To check just the major version (to determine if it's Python 2 or 3): This will print 2 for Python 2.x or 3 for Python 3.x

import sys
print(sys.version_info)
  1. On Windows: Open Command Prompt or PowerShell and run:

python --version
  1. On macOS or Linux - Open Terminal and run:

python3 --version

Remember that on some systems, python may refer to Python 2 while python3 refers to Python 3. These methods will help you determine which version of Python is installed and running on your system.

To clear screen in terminal use below script

import os
os.system('cls' if os.name == 'nt' else 'clear')

Use exit() to exit python terminal and switch to Linux terminal.

Python Indentation

Python uses indentation to define code blocks and structure. Here are some key details about indentation in Python:

  1. Purpose: Indentation is used to indicate which statements belong to a particular block of code, such as inside a function, loop, or conditional statement

  2. Syntax requirement: Unlike many other programming languages, indentation in Python is not just for readability - it's a syntactical requirement

  3. Standard practice: The recommended indentation is 4 spaces per level, as specified in PEP 8, the official Python style guide

  4. Consistency: The number of spaces must be uniform within a block of code

  5. First line: The first line of a Python script cannot be indented

  6. Spaces vs. tabs: It's preferred to use spaces instead of tabs for indentation. Mixing tabs and spaces can lead to errors

  7. Minimum requirement: While 4 spaces are recommended, a minimum of one space is needed to indent a statement

  8. Nested blocks: Each nested block should be indented one level further than its parent block

  9. Editor support: Many Python-supporting editors and IDEs automatically insert the correct indentation when you press the Tab key

  10. Error prevention: Proper indentation helps prevent IndentationErrors, which occur when indentation is inconsistent or incorrect

  11. Code readability: Besides being a syntactical requirement, proper indentation significantly improves code readability and maintainability

Remember, while Python allows flexibility in the number of spaces used for indentation, it's crucial to maintain consistency within your codebase to avoid errors and improve readability.

Here are some basic Python script examples demonstrating proper indentation:

  1. If statement:

x = 10
if x > 5:
    print("x is greater than 5")
    print("This is still part of the if block")
print("This is outside the if block")
  1. For loop:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)
    print("I like this fruit")
print("Loop is finished")
  1. Function definition:

def greet(name):
    print("Hello,", name)
    print("How are you today?")

greet("Alice")
print("Function call completed")
  1. Nested structures:

for i in range(3):
    print("Outer loop iteration:", i)
    for j in range(2):
        print("  Inner loop iteration:", j)
    print("Outer loop iteration finished")
  1. If-elif-else statement:

score = 85

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
else:
    print("Grade: D")
  1. While loop:

count = 0
while count < 5:
    print("Count is:", count)
    count += 1
print("While loop finished")
  1. Class definition:

class Dog:
    def __init__(self, name):
        self.name = name
    
    def bark(self):
        print(self.name, "says: Woof!")

my_dog = Dog("Buddy")
my_dog.bark()
  1. Try-except block:

try:
    x = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero")
finally:
    print("This always executes")

These examples demonstrate how indentation is used to define code blocks in various Python structures. Remember to maintain consistent indentation (preferably 4 spaces) for each level to ensure your code runs correctly and is easily readable.

PreviousWorked-out ExampleNextPython - Basic Concepts

Last updated 3 months ago

Download Python from here -

Beginners Guide for Python -

https://www.python.org/downloads/
https://wiki.python.org/moin/BeginnersGuide/Download