Python Tutorial Overview

24 篇文章 0 订阅

Learn Python - Free Interactive Python Tutorial fast overview

Python Tutorial more detailed

Python - Files I/O file i/o

JSON format JSON

python official documentation json — JSON encoder and decoder — Python 3.10.0 documentation

tutorial short Python JSON: Read, Write, Parse JSON (With Examples)

for python:

dump ==> for file, plural form #write

dumps ==> singular form, for a single variable

load ==> for file, #read 

loads ==> singular form

tutorial study notes

1. sort

Sorting HOW TO — Python 3.10.0 documentation

list.sort() vs. sorted()

sorted returns a new sorted object, list.sort sort in place and return None

dictionary unlike map

==> no sort available

2. string

2.1 basic string op examples

s_cpy = "Hey there! what should this string be?"
# Length should be 20
s = s_cpy[:20]
print("Length of s = %d" % len(s))

# First occurrence of "a" should be at index 8
s = s_cpy[(s_cpy.index('a') - 8):] #rememeber, all in terms of index, no offset necessary
print("The first occurrence of the letter a = %d" % s.index("a"))

# Number of a's should be 2
s = s_cpy;
while s.count('a') != 2:
    s += 'a'
print("a occurs %d times" % s.count("a"))

s = s_cpy
# Slicing the string into bits
print("The first five characters are '%s'" % s[:5]) # 0 to 4
print("The next five characters are '%s'" % s[5:10]) # 5 to 9
print("The thirteenth character is '%s'" % s[14]) 
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end

# Convert everything to uppercase
print("String in uppercase: %s" % s.upper())

# Convert everything to lowercase
print("String in lowercase: %s" % s.lower())

# Check how a string starts
if s.startswith("Str"):
    print("String starts with 'Str'. Good!")

# Check how a string ends
if s.endswith("ome!"):
    print("String ends with 'ome!'. Good!")

# Split the string into three separate strings,
# each containing only a word
print("Split the words of the string: %s" % s.split(" ")[:3])

 2.2 string format and float

.format()

Python String Format Cookbook – mkaz.blog

%f

string - How to format a floating number to fixed width in Python - Stack Overflow

the formatting for .format() actually carriers over to print( % )

2.3 + vs. append

list.append() returns nothing

+ (concatenation) returns the resulting seq.

e.g.

the same goes for .insert(), the return type is NoneType

3. Boolean

Python distinguishes strictly between expressions and statements. In particular, assignment statements don't have values, and so can't be used in conditionals.

A statement is evaulated as true if one of the following is correct: 

1. The "True" boolean variable is given, or calculated using an expression, such as an arithmetic comparison. 

2. An object which is not considered "empty" is passed.

Here are some examples for objects which are considered as empty: 

1. An empty string: "" 

2. An empty list: [] 

3. The number zero: 0 

4. The false boolean variable: False

==> know this, but also remember good coding practice, check the variables against the values you want, unless they are meant to be used as boolean flags.

==> although for python with its emphasis on convenience, 

if l2: #is fine

===> this form is used to ensure readability

if not l2:

#empty

else:

#code

=====> the desirable practice is actually costing us performance

if len(l2) == 0

===> but 

if not num & 1 #check for/true if even number

==>should yield in favor of ==> not vs. ==, not much different

if num & 1 == 0

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值