Python Basics: Indexing, Slicing, String Alignment, List Comprehensions, Variable Arguments, and NumPy Linear Algebra
This article introduces essential Python techniques—including list indexing and slicing, string alignment methods, list and dictionary comprehensions, handling variable-length arguments, and using NumPy for linear‑algebra operations—providing clear code examples and explanations for each concept.
Python is one of the most popular programming languages (TIOBE Index April 2022). It is easy to learn and versatile, suitable for web, desktop, game development, and scripting.
Python’s list indexing and slicing are powerful tools. Positive and negative indices can access any element, and slices can extract sub‑lists.
<code>a_list = [100, 200, 300, 400, 500, 600]
print(a_list[0]) # 100
print(a_list[1]) # 200
print(a_list[2]) # 300
print(a_list[-1]) # 600
print(a_list[-3]) # 400
b_list = a_list[1:3] # [200, 300]
c_list = a_list[4:] # [500, 600]
d_list = a_list[-4:-1] # [200, 300, 400]
e_list = a_list[-1:] # [600]
</code>String formatting methods such as str.ljust , str.rjust , str.center , and digit_str.zfill enable left, right, center alignment, and zero‑padding.
<code>new_str = 'Help!'.center(10, '#')
print(new_str) # ##Help!###
new_str = '750'.rjust(6, '0')
print(new_str) # 000750
</code>List and dictionary comprehensions provide concise syntax for generating new collections. For example, squaring each element of a list:
<code>b_list = [i * i for i in a_list]
</code>And converting a list of tuples into a dictionary:
<code>vals_list = [('pi', 3.14), ('phi', 1.618)]
my_dict = {i[0]: i[1] for i in vals_list}
</code>Variable‑length argument lists ( *args ) and keyword arguments ( **kwargs ) let functions accept arbitrary numbers of positional and named parameters.
<code>def my_var_func(*args):
print('The number of args is', len(args))
for item in args:
print(item)
def pr_named_vals(**kwargs):
for k in kwargs:
print(k, ':', kwargs[k])
</code>These can be combined:
<code>def pr_vals_2(*args, **kwargs):
for i in args:
print(i)
for k in kwargs:
print(k, ':', kwargs[k])
pr_vals_2(1, 2, 3, -4, a=100, b=200)
</code>NumPy provides efficient linear‑algebra operations. The numpy.dot function computes dot products for vectors and matrices.
<code>import numpy as np
A = np.ones(5)
B = np.arange(5)
print(np.dot(A, A)) # 5.0
print(np.dot(A, B)) # 10.0
print(np.dot(B, B)) # 30
A = np.arange(6).reshape(2,3)
B = np.arange(6).reshape(3,2)
C = np.dot(A, B)
print('Dot product:\n', C) # [[10 13]
# [28 40]]
</code>The article concludes with a disclaimer that the content is collected from the internet and the original author retains copyright.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
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.