Python

P
PythonHub
Progress 0%

Python

  • Home
  • History of Python
  • Applications of Python
  • Introduction To Python
    • What is Python
    • Character Set
    • Tokens in Python
    • Python Execution Mode
    • Variable And Identifiers
    • Data Types in Python
    • Operators And Expressions
    • Constants in Python
    • Assignment Statement
    • Input / Output in Python
    • Simple Python Scripts
    • Namespace in Python
    • šŸ“ Assignments
  • Operators in Python
    • Arithmetic Operators
    • Assignment Operators
    • Shorthand Assignment Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Special Operators
    • šŸ“ Assignments
  • Input Output in Python
    • Accept Input
    • Output Formatting
    • šŸ“ Assignments
  • Conditional Statement
    • Decision Making
    • if Statement
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • Short Hand IF-ELSE
    • šŸ“ Assignments
  • Loops
    • Introduction to Loops
    • While Loop
    • Nested While Loop
    • šŸ“ While Loop Assignments
    • For Loop
    • For Loop Examples
    • Nested For Loop
    • Nested For Loop Examples
    • Infinite While Loops
    • Infinite For Loops
    • Break, Continue and Else in Loops
    • Difference Between For and While Loop
    • For Each Loop
    • šŸ“ For Each Assignments
    • šŸ“ All Loops Assignments
  • List
    • List in Python
    • Access List Elements
    • List Functions
    • Iterate (Loop) List
    • List Comprehension
    • šŸ“ Assignments
  • Tuple
    • Tuple in Python
    • Access Tuple Elements
    • Tuple Functions
    • Iterate (Loop) Tuple
    • Unpack Tuple
    • Tuple Comprehension
    • šŸ“ Assignments
  • Set
    • Set in Python
    • Access Set Elements
    • Set Methods
    • Iterate (Loop) Set
    • Pack/Unpack Set
    • Set Comprehension
    • šŸ“ Assignments
  • Dictionary
    • Dictionary
    • Access Dictionary Items
    • Dictionary Methods
    • Iterate (Loop) Dictionary
    • Formatting Dictionaries
    • Nested Dictionaries
    • Dictionary Comprehension
    • šŸ“ Assignments
  • Diff List Tuple Set Dictionary
    • List vs Tuple
    • List vs Set
    • List vs Dictionary
    • Tuple vs Set
    • Tuple vs Dictionary
    • Dictionary vs Set
    • šŸ“ Assignments
  • Exception
    • Error vs Exception
    • Exception Handling
    • Types of Exception
    • User Defined Exception
    • Logging Exception
    • šŸ“ Assignments
  • Functions
    • Introduction to Functions
    • Modular Programming
    • Types of Functions
    • Inbuilt Functions
    • Need For User-Defined Function
    • Elements of User Defined Function
    • Function Arguments
    • Nesting of Functions
    • Recursion
    • Global Local and Non Local
    • Python Lambda Functions
    • šŸ“ Assignments
  • Python Module
    • Introduction to Module
    • Inbuilt Modules in Python
    • User Defined Module
    • šŸ“ Assignments
  • File Handling
    • Introduction to Files
    • Create File
    • Read Files
    • Write to File
    • Rename File
    • Copy File
    • Move File
    • List Files in Directory
    • Binary Files
    • Zipping and Unzipping Files
    • šŸ“ Assignments
  • Strings
    • Basics of Strings
    • String Special Operators
    • String Formatting Operators
    • String Methods
    • šŸ“ Assignments
  • Regular Expressions
  • Python OOPS
    • Basics of Object Oriented
    • What are Classes and Objects?
    • Creating Class and Object
    • OOP vs Procedural Programming
    • Difference Between Classes and Objects
    • Constructors
    • Destructor
    • Built Class Methods and Attributes
    • Class and Instance Variables
    • Inheritance in Python
    • Single Inheritance
    • Multiple Inheritance
    • Multilevel Inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance
    • Abstraction
    • Method Overriding
    • Abstract Method
    • Interfaces in Python
    • Abstract Class vs Interface
    • Public, Private and Protected
    • Overloading vs Overriding
    • Inheritance vs Composition
    • Encapsulation
    • Polymorphism
    • Inner Classes
    • šŸ“ Assignments
  • Advanced Python
    • Iterator in Python
    • Generator in Python
    • Decorator in Python
    • Type Hints in Python
    • Async/Await Programming
    • Dataclasses in Python
    • Context Managers in Python
    • Property Decorator in Python
    • __slots__ in Python
    • Enums in Python
    • Walrus Operator in Python
    • Match-Case in Python
    • šŸ“ Assignments
  • Python Standard Library
    • Collections Module
    • Itertools Module
    • Functools Module
    • Datetime Module
    • JSON Module
    • OS Module
    • Sys Module
    • Random Module
    • Math Module
    • šŸ“ Assignments
  • Python Testing
    • Unit Testing in Python
    • Pytest Framework
    • Mocking in Python
    • šŸ“ Assignments
  • Python Best Practices
    • PEP 8 Style Guide
    • Docstrings in Python
    • Logging in Python
    • Code Optimization Tips
    • Debugging Techniques
    • šŸ“ Assignments
  • MySQL Database in Python
    • Introduction to MySQL with Python
    • DBMS vs File System
    • Connecting to MySQL Database
    • Create Database in MySQL
    • Create Table in MySQL
    • Insert Data in MySQL
    • Insert Multiple Rows
    • Select Data from MySQL
    • WHERE Clause in MySQL
    • Update Data in MySQL
    • Delete Data from MySQL
    • Parameterized Queries
    • Transaction Management
    • Error Handling
    • Connection Pooling
    • MySQL Drivers Guide
    • Joins in MySQL
    • Aggregation Functions
    • Backup MySQL Database
    • Best Practices
    • šŸ“ Assignments
  • MySQL Database Operations
    • SELECT Statement
    • MySQL Operators
    • DDL Statements
    • DML Statements
    • Subqueries
    • JOIN Operations
    • Aggregation
    • Case Study
    • šŸ“ Assignments
  • Graphics in Python
  • Threads in Python
    • Introduction to Threads
    • Process vs Threads
    • Concurrent Programming & GIL
    • Uses of Threads
    • Creating Threads
    • Single Tasking
    • Multi Tasking
    • Thread Synchronization
    • šŸ“ Assignments
  • Interview Questions & Answers
  • Python Case Studies
  • Multiple Choice Questions
  • šŸ“ Practice Problems
Get in Touch
  • tech2dsm@gmail.com

© Sankalan Data Tech

Python Language Interactive Tutorial

šŸš€ Python Simple Scripts: A Beginner's Guide

Python Simple Scripts - Beginner's Guide

Writing simple Python scripts is the first step to becoming a Python programmer.

Created by Sankalan Data Tech Team Verified
Data Engineers, Analysts, Scientists & Trainers
We are a team of Python developers, data engineers and data scientists with years of real-world experience. We have built production applications. We have tackled complex problems. We have helped teams ship better code. What drives us? Teaching — especially that moment when things finally click. Our tutorials focus on practical examples and honest guidance. Whether you are just starting out or leveling up we are here to make Python genuinely useful.
šŸ“‘ On this page:
  • What are Python Scripts?
  • Your First Python Script
  • Scripts with Variables
  • Scripts with Input/Output
  • Scripts with Conditionals
  • Scripts with Loops
  • Scripts with Functions
  • Practical Script Examples
  • Try It Yourself
  • Quick Quiz
  • FAQ
Share this tutorial:
Twitter LinkedIn Facebook WhatsApp Reddit Telegram Email Copy Link
šŸ“š What You'll Learn Here
  • What Python scripts are — understanding scripts vs programs
  • Your first script — "Hello, World!" and beyond
  • Scripts with variables — storing and manipulating data
  • Scripts with input/output — interacting with users
  • Scripts with conditionals — making decisions
  • Scripts with loops — repeating actions
  • Scripts with functions — organizing code
  • Practical examples — real-world scripts
  • Hands-on practice with the interactive editor

What are Python Scripts?

A Python script is a file containing Python code that can be executed to perform a specific task. Unlike a full-fledged program, a script is typically smaller, focused on a single purpose, and designed to be run from the command line or an editor.

Think of scripts as recipes — they contain step-by-step instructions that Python follows to produce a result. Writing scripts is the best way to learn Python because you can start small and gradually build more complex programs.

# A simple Python script
print("Hello, Python!")

1. Your First Python Script

Every Python journey starts with the classic "Hello, World!" script. It's simple but teaches you the essential structure of a Python program.

# Hello, World! - Your First Python Script
print("Hello, World!")

# Output:
# Hello, World!

# A slightly more interesting version
name = "Python"
print(f"Hello, {name}!")

# Output:
# Hello, Python!

2. Scripts with Variables

Variables store data that can be used and manipulated throughout your script. They are the building blocks of any Python program.

# Script with variables
name = "Alice"
age = 25
height = 5.6
is_student = True

print(f"Name: {name}")
print(f"Age: {age}")
print(f"Height: {height} feet")
print(f"Is student? {is_student}")

# Output:
# Name: Alice
# Age: 25
# Height: 5.6 feet
# Is student? True

3. Scripts with Input/Output

Interactive scripts use input() to get data from users and print() to display results.

# Interactive script
name = input("What's your name? ")
age = int(input("How old are you? "))

print(f"Hello, {name}!")
print(f"You will be {age + 1} years old next year!")

# Output:
# What's your name? (user types: Bob)
# How old are you? (user types: 30)
# Hello, Bob!
# You will be 31 years old next year!

4. Scripts with Conditionals

Conditionals (if, elif, else) allow scripts to make decisions based on conditions.

# Script with conditionals
age = int(input("Enter your age: "))

if age >= 18:
    print("You are an adult.")
elif age >= 13:
    print("You are a teenager.")
else:
    print("You are a child.")

# Output:
# Enter your age: 16
# You are a teenager.

5. Scripts with Loops

Loops (for and while) allow scripts to repeat actions multiple times.

# Script with for loop
print("Counting to 5:")
for i in range(1, 6):
    print(i)

# Script with while loop
count = 0
print("Counting down from 3:")
while count < 3:
    print(3 - count)
    count += 1

# Output:
# Counting to 5:
# 1
# 2
# 3
# 4
# 5
# Counting down from 3:
# 3
# 2
# 1

6. Scripts with Functions

Functions help organize code into reusable blocks. They make scripts more readable and maintainable.

# Script with functions
def greet(name):
    return f"Hello, {name}!"

def calculate_area(length, width):
    return length * width

# Main script
person = "Charlie"
print(greet(person))

length = 5
width = 3
area = calculate_area(length, width)
print(f"Area of rectangle: {area}")

# Output:
# Hello, Charlie!
# Area of rectangle: 15

7. Practical Script Examples

Here are some practical scripts you can create to solve everyday problems.

a. Temperature Converter

# Temperature Converter
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C is equal to {fahrenheit}°F")

b. Simple Calculator

# Simple Calculator
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
operator = input("Enter operator (+, -, *, /): ")

if operator == '+':
    result = a + b
elif operator == '-':
    result = a - b
elif operator == '*':
    result = a * b
elif operator == '/':
    result = a / b
else:
    result = "Invalid operator"

print(f"Result: {result}")

c. Guess the Number Game

# Guess the Number Game
import random

secret = random.randint(1, 10)
print("Guess a number between 1 and 10")

while True:
    guess = int(input("Your guess: "))
    if guess == secret:
        print("Correct! You win!")
        break
    elif guess < secret:
        print("Too low!")
    else:
        print("Too high!")

Try It Yourself!

Experiment with Python scripts directly in your browser. Modify the code and see the results in real time.

Loading Pyodide... 0%
Python Code Editor
========================================
WELCOME TO PYTHON SCRIPTS
========================================

1. HELLO WORLD
Hello, Python!

2. VARIABLES
Name: Alice, Age: 25

3. INPUT/OUTPUT
Try changing the code below!

4. CONDITIONALS
7 is odd

5. LOOPS
Counting to 5:
1 2 3 4 5

6. FUNCTIONS
Hello, Python Learner!

āœ… You're writing Python scripts!
šŸ†

šŸŽ‰ You've Mastered Python Simple Scripts!

You understand variables, input/output, conditionals, loops, functions, and practical script examples. You're now ready to write your own Python scripts!

Quick Quiz – Test Your Knowledge

Let's see what you've learned about Python simple scripts:

1. What is the correct way to print "Hello" in Python?
2. Which keyword is used to define a function in Python?
3. What does this code do: for i in range(3): print(i)?

Frequently Asked Questions

šŸ¤” What's the difference between a script and a program? ā–¼

A script is typically smaller, focused on a single task, and interpreted. A program is larger, more complex, and may be compiled. In Python, the terms are often used interchangeably.

šŸ”§ How do I run a Python script? ā–¼

You can run a Python script from the command line using python script_name.py or from an IDE like VS Code, PyCharm, or Jupyter Notebook.

šŸ“‚ What is the if __name__ == "__main__" block? ā–¼

This block allows a script to be run directly or imported as a module. Code inside this block only runs when the script is executed directly, not when imported.

šŸ“Š What's the best way to learn Python scripting? ā–¼

The best way is to start with simple scripts like "Hello, World!", then gradually add variables, conditionals, loops, and functions. Practice regularly and build small projects.

šŸ“š Where to Go From Here

Now that you understand Python simple scripts, here are some related topics to explore:

šŸ“ Variables & Identifiers

Learn about variable naming rules

šŸ“„ Input/Output

Master input() and print()

šŸŽÆ Decision Making

Learn if-else statements

šŸ“– Related Tutorials
  • Variables and Identifiers
  • Input/Output in Python
  • Decision Making
  • Introduction to Loops
Previous: Input/Output Next: Namespace →