IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> My Python Notes -> 正文阅读

[Python知识库]My Python Notes

1 Something about print

syntaxexplanation
print(str1, str2, sep = "")print strings separately
print(str1 + str2)print strings as one string
print(f'{my_var}')f-string interpret what inside the {}
print('%type' %my_var)%d: integer; %f: float; %s: string; %b: binary; (doc)
print(\n)print use escape characters
print(f'{my_var:n[type]}')print the variable at a certain length
{:<}, {:>}, {:^}left aligned, right aligned, centred
syntaxexplanation
+, -, *, /addition, subtraction, multiplication, division
**exponentiation
//integer division
%modulus; the remainder of a deviation

2 Import modules

import numpy as np
print(np.sin(np.pi))

3 Variables and objects

type() : check data types.

  • Numeric: int, float, complex;

  • Sequence: str, list, range, tuple

  • Boolean

  • Set

  • Dictionary

  • number objects and string objects

3.1 Strings

syntaxexplanation
my_str[i]returns the i + 1 i+1 i+1th character in string
my_str[-i]returns the i i ith from the end
len(my_str)the length of the string
max(my_str), min(my_str)alphabetically
sorted(my_str)from smallest to largest
my_str.capitalize()capitalize the first letter of the string
my_str.split()split the string

3.2 Booleans

Operators:

syntaxexplanation
and, or, notlogical operators
==, <, <=, !=comparison operators

3.3 Numbers

Floating points numbers: 1.0e-6 is 1 × 1 0 ? 6 1 \times 10^{-6} 1×10?6.

3.4 Type casting

int(), float(), str().

bool(0) and bool('') are false.

3.5 List

List can contain almost any other object.

my_list = ['my', 1, 4.5, ['you', 'they'], 432, -2.3, 33]
emplist = []
print(list_1 + list_2)
print('hellow'+'world')   # "helloworld"
my_list.append('extar')    # add an item to the end of a list
print(my_list[3][1])       # the outcome is "they"
print(sorted(numberlist)) # sort a list
print(12 * my_list)        # create a list with 12 repetition of my_list
print('you' in my_list[3]) # check if something is in a list

Useful tools:

syntaxexplanation
len(my_list)the length of the list
my_list.append('a new element')add a new element to the list

Slicing:

a = [2, 5, 4, 8, 8]
print(a[1:3]) # the 2nd and 3rd | [5, 4]
print(a[2:])  # the 3rd to the last | [4, 8, 8]
print(a[:-2]) # the 1st to the 3rd from the last | [2, 5, 4]
syntaxexplanation
my_list[start:stop]from start to stop-1 共stop-start个
my_list[start:]from start to len(l)-1
my_list[:stop]from 0 to stop-1
my_list[start:stop:step]from start to stop-1, with increment step
my_list[::step]from 0 to len(l)-1, with increment step
my_list[::], my_list[:]all the elements
  • If j = = = i, then a[i:j] and a[i:j:k] are empty lists, for any value of k.
  • If j < < < i, then a[i:j] and a[i:j:k] are empty lists, for positive values of k.

4 if statements

if my_condition:
    [some instructions]

my_condition is a Boolean object.

if cond_1:
    [some instructions]
elif cond_2:
    [other instructions]
else:
    [other instructions]

5 Loops

5.1 for loops

for i in my_seq:
	[some instructions]
  • ranges: a sequence type
syntaxexplanation
range(j) 0 0 0, 1 1 1, 2 2 2, …, j ? 1 j-1 j?1; 共 j j j个.
range(i, j) i i i, i + 1 i+1 i+1, i + 2 i+2 i+2, …, j ? 1 j-1 j?1; 共 j ? i j-i j?i个.
range(i, j, k) i i i, i + k i+k i+k, i + 2 k i+2k i+2k, …, i + m i+m i+m; i + m ≤ j ? 1 i+m \leq j-1 i+mj?1.

5.2 while loops

while my_condition:
    [some instructions]

Use break to exit the loop conditionally:

while my_condition:
    [some instructions]
    if my_condition:
        break

6 Defining function

def my_func(inputs):
    [function body]
    return outputs

Outputs can be variables separated by commas.

7 Debugging and troubleshooting

Built-in exception types:

  • IndexError: a sequence subscript is out of range. r instance, here, we’re trying to access my_list[4], but my_list only has elements up to my_list[3].
  • NameError: the variable referred to does not exist – there is no box in memory with this label. This often comes up when you mistype a variable name.
  • SyntaxError: the code is not syntactically correct – it is not valid Python, so Python doesn’t know how to interpret it.
  • TypeError: a very common error to see when learning Python! This means that an operation or a function is applied to an object of the wrong type
  • ValueError: raised when an operation or function is applied to an object with the right type, but an invalid value. For example, the int() function can cast a string to an integer, if the string can be interpreted as a number.
  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-10-08 11:45:45  更:2021-10-08 11:47:07 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 17:31:15-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码