Date
Nov. 21st, 2024
 
2024年 10月 21日

Post: Python : Tips for Beginners

Python : Tips for Beginners

Published 12:09 Sep 06, 2015.

Created by @ezra. Categorized in #Programming, and tagged as #Python.

Source format: Markdown

Table of Content

Here are some useful tips and tricks in Python for beginners to the language who's coming from a traditional programming language like C or Java, and I assume you are using Python 3.

Swapping Elements

Here is the way that you're probably used to swap elements:

a = 1
b = 2
tmp = a
a = b
b = tmp

But here's a better way:

a = 1
b = 2
a, b = b, a

Initializing Lists

Sometime you just need a list of some integers set to zero. So you may do something like this:

arr = []
for _ in range(5):
    arr.append(0)

Elegantly:

arr = [0] * 5

But, do note that this will create a shallow copy if you are working with a list within a list.

For example:

arr = [[0]] * 5
# [[0], [0], [0], [0], [0]]
arr[0][0] = 1
# [[1], [1], [1], [1], [1]]

String building

Now, you're going to need to print strings.

tom = "boy"
lucy = "girl"
string = "Tom is a " + tom + " and Lucy is a " + lucy + ".";

Ugh, how messy. Do this instead:

tom = "boy"
lucy = "girl"
string = "Tom is a {0} and Lucy is a {1}.".format(tom, lucy)

List Comprehensions

Then, suppose you have a list like this:

arr = [1, 2, 3, 4, 5]

And you want to double each element in that list:

for i in range(len(arr)):
    arr[i] = arr[i] * 2
# [2, 4, 6, 8, 10]

Well, I can't say it's wrong, but I do have a cleaner way:

arr = [elem * 2 for elem in arr]

Returning tuples

To make life easier, Python allows you to return multiple elements (which is called tuples) in a function.

def getTwoNumber():
    return 1, 2

first, second = getTwoNumber()

And you can use an underscore if you don't need all of the elements returned, like so:

_, second = getTwoNumber()

Accessing Dictionaries

dict = {}
keys = [1, 2, 9]

dict = {i: 100 for i in keys}

for i in range(10):
    print("value for key {} is {}".format(i, dict.get(i, "empty")))

Nifty, huh?

Pinned Message
HOTODOGO
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
开发、设计与写作皆为所长。
热爱苹果、钟情色彩。
随时恭候 垂询