Introduction to Python

1.1 Introduction to Python

Python is one of the most popular programming languages used today. It is simple,
easy to learn, and powerful enough for scientific computing, automation,
artificial intelligence, robotics, and engineering applications. Unlike many
traditional programming languages, Python uses clear English-like syntax,
making it an ideal language for beginners.

Mechanical engineers increasingly use Python for engineering calculations,
automation of repetitive tasks, sensor data analysis, CAD/CAE scripting,
robotics, predictive maintenance, and manufacturing analytics.

1.2 Features of Python

Python has the following important features:

  • Simple and easy-to-read syntax.
  • High-level programming language.
  • Interpreted language (no separate compilation required).
  • Free and open-source software.
  • Portable across Windows, Linux, and macOS.
  • Supports object-oriented and procedural programming.
  • Large standard library and third-party packages.
  • Dynamic typing and automatic memory management.
  • Interactive mode for quick testing.
  • Strong community support.

1.3 Applications of Python in Automation and Engineering

Python plays an important role in modern industries.

Industrial Automation

  • PLC and controller communication
  • Industrial IoT (IIoT)
  • Machine monitoring
  • Data logging
  • Robotics
  • Machine vision

Mechanical Engineering

  • Engineering calculations
  • CAD automation
  • CAE/FEA scripting
  • CNC automation
  • Predictive maintenance
  • Quality control
  • Data visualization

1.4 Python Installation

Windows:
1. Download Python from https://www.python.org
2. Run the installer.
3. Select ‘Add Python to PATH’.
4. Click Install Now.
5. Verify using: python –version

Linux:
Install using your package manager (e.g., sudo apt install python3).
Verify using: python3 –version

macOS:
Download from python.org or install using Homebrew (brew install python).

Popular IDEs: IDLE, Thonny, Visual Studio Code, PyCharm, Jupyter Notebook.

1.5 Python Syntax and Keywords

Python syntax defines the rules for writing programs.
• Indentation is mandatory.
• Keywords are reserved words with predefined meanings.
• Variable names are case-sensitive.
• Comments begin with #.

Common Keywords:

  • if, else, elif, for, while, break, continue, pass, True, False, None, and, or, not, def, return, import

Example:
if temperature > 70:
print(‘Warning’)

1.6 Variables and Data Types

A variable is a named memory location used to store data.
Example:
speed = 1200
temperature = 35.5
machine = ‘Lathe’

Data Type Description Example
int Whole numbers 100
float Decimal numbers 25.75
str Text ‘Motor’
bool Logical values True
list Collection [10,20]

1.7 Operators and Expressions

Arithmetic: +  –  *  /  //  %  **

Relational: ==  !=  >  <  >=  <=

Logical: and  or  not

Assignment: =  +=  -=  *=  /=

Expression Example:
stress = force / area

1.8 Input and Output Operations

Input is obtained using input().
Output is displayed using print().

Example:
name = input(“Enter Name: “)
print(“Welcome”, name)

Engineering Example:
force = float(input(“Enter Force: “))
area = float(input(“Enter Area: “))
stress = force / area
print(“Stress =”, stress)

1.9 Simple Python Programs for Automation

Area of Rectangle

length=float(input(‘Length: ‘))
width=float(input(‘Width: ‘))
print(‘Area =’, length*width)

Motor Power

voltage=float(input(‘Voltage: ‘))
current=float(input(‘Current: ‘))
print(‘Power =’, voltage*current)

Machine Temperature

temp=float(input(‘Temperature: ‘))
if temp>70:
print(‘Warning: Overheating’)
else:
print(‘Normal’)

RPM to RPS

rpm=float(input(‘RPM: ‘))
print(‘RPS =’, rpm/60)

Unit Summary

This unit introduced Python, its features, engineering applications, installation, syntax, variables, data types, operators, input/output operations, and simple automation programs.

Key Terms

Python, IDE, Variable, Data Type, Keyword, Operator, Expression, Automation, PLC, CAD, CAE, Robotics, IIoT.

Review Questions

  1. Define Python.
  2. Explain the features of Python.
  3. Describe applications of Python in automation and engineering.
  4. Explain Python installation on Windows.
  5. What are Python keywords? Give examples.
  6. What is a variable? Explain data types.
  7. Explain Python operators with examples.
  8. Differentiate input() and print().
  9. Write a program to calculate motor power.
  10. Write a program to monitor machine temperature.

Leave a Comment

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