Python - Short Introduction
This page is a comprehensive guide to Python Scripting with some applied samples related to cloud programming.
What is Python
Python is a versatile, high-level programming language known for its readability and simplicity. It's widely used for various applications, including web development, data analysis, scientific computing, automation, and more.
Getting Started: Running Python Code
- Install Python: Download and install Python from the official website (https://www.python.org/downloads/).
- Open a terminal or command prompt.
- Type
python
orpython3
(depending on your system) to start the Python interpreter. - You'll see the Python prompt (
>>>
) where you can type and run Python code.
Basic Concepts
- Variables: Store data using variables.
name = "John"
age = 25 - Print: Display output using
print()
function.print("Hello, " + name)
Data Types:
- Strings: Text data enclosed in quotes (
'
or"
). - Numbers: Integers and floating-point numbers.
- Lists: Ordered collections of items.
- Dictionaries: Key-value pairs.
- Booleans:
True
orFalse
values.
Conditional Statements:
Use if
, elif
, and else
for conditional execution.
if age < 18:
print("You're a minor.")
else:
print("You're an adult.")
Loops:
for
Loop: Iterate over a sequence.fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print("I like", fruit)while
Loop: Execute code while a condition is true.count = 1
while count <= 5:
print("Count:", count)
count += 1
Functions: Define and use functions to encapsulate code.
def greet(name):
print("Hello,", name + "!")
greet("Alice")
Scripting
- Create a new file with a
.py
extension, e.g.,myscript.py
. - Write your Python code in the script file.
Running Scripts:
- Open a terminal or command prompt.
- Navigate to the directory containing your script.
- Run the script:
python myscript.py
orpython3 myscript.py
.
Examples
Hello World:
print("Hello, World!")
Simple Calculator:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
sum = num1 + num2
print("Sum:", sum)File Backup Script:
import shutil
import os
source_dir = "/path/to/source"
backup_dir = "/path/to/backup"
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
backup_file = f"backup_{timestamp}.tar.gz"
shutil.make_archive(os.path.join(backup_dir, backup_file), "gztar", source_dir)
print(f"Backup saved as {backup_file}")
As you progress in Python
, you can explore more advanced topics such as object-oriented programming, file handling, libraries, frameworks, and more.
Python's extensive community and rich ecosystem of libraries make it a fantastic language to learn and apply to a wide range of projects.
✅ Resources
- 👉 Python CheatSheet
- 👉 Deploy Projects using your own cloud provider
- 👉 Get Deployment Support from
experts