Python Introduction and Basic Syntax Tutorial
This tutorial introduces Python, explains how to install it, and covers fundamental syntax including comments, variables, data types, string manipulation, collections, control flow, functions, modules, exception handling, file operations, classes, comprehensions, and common utilities, providing clear code examples for each concept.
1. Python Overview Python is an interpreted language that supports multiple programming paradigms such as object‑oriented, imperative, functional, and procedural styles, emphasizing readability and simplicity.
2. Installing Python Visit the official website https://www.python.org/downloads/ to download and install the latest version.
3. Basic Syntax
Comments
# Single‑line comment
"""This is a single‑line comment"""
print("Hello, World!") # Multi‑line comment
"""
This is a multi‑line comment
"""
print("Hello, World!")Variables and Data Types
name = "Alice"
age = 30
print(name, age) # 输出 Alice 30 number = 10
floating_point = 3.14
boolean = True
string = "Hello, World!"
print(number, floating_point, boolean, string) # 输出 10 3.14 True Hello, World!String Operations
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name) # 输出 John Doe age = 30
print(f"My name is {first_name} {last_name} and I am {age} years old.") # 输出 My name is John Doe and I am 30 years old.Lists
numbers = [1, 2, 3, 4, 5]
print(numbers) # 输出 [1, 2, 3, 4, 5] numbers.append(6)
print(numbers) # 输出 [1, 2, 3, 4, 5, 6]Tuples
point = (1, 2, 3)
print(point) # 输出 (1, 2, 3)Dictionaries
person = {"name": "Alice", "age": 30}
print(person) # 输出 {'name': 'Alice', 'age': 30}Conditional Statements
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are not an adult yet.")
# 输出 You are an adult.Loops
for i in range(5):
print(i) # 输出 0 1 2 3 4 count = 0
while count < 5:
print(count)
count += 1 # 输出 0 1 2 3 4Function Definition
def greet(name):
print(f"Hello, {name}!")
greet("Alice") # 输出 Hello, Alice!Module Import
import math
print(math.sqrt(16)) # 输出 4.0Exception Handling
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero.") # 输出 Cannot divide by zero.File Operations
with open("example.txt", "w") as file:
file.write("Hello, World!")
with open("example.txt", "r") as file:
content = file.read()
print(content) # 输出 Hello, World!Classes and Objects
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"My name is {self.name} and I am {self.age} years old.")
alice = Person("Alice", 30)
alice.introduce() # 输出 My name is Alice and I am 30 years old.List and Dictionary Comprehensions
squares = [x**2 for x in range(5)]
print(squares) # 输出 [0, 1, 4, 9, 16] square_dict = {x: x**2 for x in range(5)}
print(square_dict) # 输出 {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}Sets
unique_chars = {char for char in "hello"}
print(unique_chars) # 输出 {'h', 'e', 'l', 'o'}Sorting
numbers = [3, 1, 4, 1, 5, 9]
sorted_numbers = sorted(numbers)
print(sorted_numbers) # 输出 [1, 1, 3, 4, 5, 9]Filtering
numbers = [1, 2, 3, 4, 5]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens) # 输出 [2, 4]Mapping
numbers = [1, 2, 3]
squared = list(map(lambda x: x**2, numbers))
print(squared) # 输出 [1, 4, 9]Enumeration
for i, val in enumerate(['a', 'b', 'c']):
print(i, val) # 输出 0 a 1 b 2 c4. Summary By covering installation, basic syntax, data structures, control flow, functions, modules, error handling, file I/O, object‑oriented programming, and common utilities, this guide provides a comprehensive foundation for beginners to start developing Python applications efficiently.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.