这可能是一本在线的笔记本(Python Self-study)

Jimmy Carlson posted @ 2017年11月06日 04:17 in OI / CS , 663 阅读
2019 JCarlson Works All Rights Reserved

教材来源:Codecademy   https://www.codecademy.com/learn/learn-python

 

01. Python Syntax  Python的格式

Output:  print a

变量:  a = 1 (Python变量不定义格式)

boolean:    True False  大小写敏感

论缩进的重要性:python靠空格进行缩进,结构体内的东西必需前面有空格

注释:#.......

多行注释:""" """ 三引号括起来的东西为注释 可为多行

算术运算符: A = 1 + 2    + - * /  ** %

乘方运算: A = 10 ** 2   <-->  A = 10^2

模运算:A = 7384973298478932 % 2

+= -= *= /= %= **= 

 

02. Strings and Console Output  字符串和输出控制

字符串变量: aaa = "233"  双引号单引号皆可

转义字符:\   "There's a book"   OR  'There\'s a book'

字符串单个字符的截取: "MONTY"[4]  -> Y

字符串长度:len(string)

大转小:.lower()  小转大:.upper()

数转串:str()

字符串连接: +

输出格式串:

print("格式串" % (参,数))

格式串内%s表示String类型

 

02-Special. Date and Time

datetime库:导入方法:from datetime import datetime (后者的datetime是库内的一个method,前者是库)

用.now()来获取当时时间 (例:2017-11-05 19:39:32.620534

分离.year .month .day .hour .minute .second  皆为String类型

 

03. Conditionals & Control Flows 条件和控制流

关系运算符: == != < > <= >=

算术运算优先于关系运算

逻辑运算符: && || !  分别对应 and or not

关系运算优先于逻辑运算 not优先于and优先于or

if语句

if <conditions>:            #不含<>

  sentences...

elif <conditions>:         #不含<>

  sentences...

else:

  sentences...

 

函数/过程

def <name>(<parameters>):  #不含<>

  sentences

 

主程序中:<name>(<parameters>)

 

03-Special. PigLatin

输入 a = raw_input("输入提示语")  

String.isalpha() 全字母时返回True,含有非字母字符时返回False

String截取多位:s[1:4]  截取从1~3的三位

 

04. Functions

返回语句:return

math库:

import math

print math.sqrt(25)

 

from math import sqrt   #从math库里调用sqrt

from math import *

 

dir(库) 可以获得该库的所有method名

 

python自带加载的method:max() min() abs() type()

type()可以得知该量的类型,e.g.

print type(120393)
print type(1239324.3)
print type("What the fuck")
 
<type 'int'>
<type 'float'>
<type 'str'>
 
 
05. Lists and Dictionarys
List定义法
a = []
b = ["1","2","3"]
c = [4,5,6]
 
访问
b[1]  c[0]
 
在List末尾添加新元素的函数:<List>.append(<item>)
计算List长度:len(<List Name>)
 
截取部分List:  <List>[<BEGIN>:<END>]  截取[BEGIN,END)的部分
 
截取字符串:<String>[<BEGIN>:<END>]  截取连续的一段字符串子串[BEGIN,END),缺省BEGIN为0,缺省END为串长
 
List查询:<List>.index(<item>)
List插入:<List>.insert(<index>,<item>)
List排序:<List>.sort()
List删除:<List>.remove(<item>) 注意是item不是index。
 
用FOR循环遍历List:
for <循环变量> in <List>:
    <循环体>
 
 
Dictionary是一个跟List很像的东西,但是它的下标,应该说是SearchKey不太一样,有点类似Map。
Dictionary定义法:
d = { 'key1' : 1, 'key2' : 2, 'key3' : 3 }
访问的方法比如 d['key1']
对于Dictionary的添加, <Dictionary>[<new_key>] = <new_value>
Dictionary的大小可以用len()计算。
 
删除关系可以用语句 del <Dictionary>[<key>]
更改值可以用和添加一样的,<Dictionary>[<key>] = <new_value>
 
05-Special. A day in Supermarket
 
for循环可以遍历Dictionary,循环变量是为key值。
for <循环变量:Key> in <Dictionary>:
   print <Dictionary>[<循环变量:Key>]
 
for循环遍历String,循环变量类型为char,对每一个字符进行遍历。
for <循环变量> in <String>:
    print <循环变量> #打印每一个字符
 
 
07. Lists and Fuctions
List的删除:<List>.pop(index)  #有返回值,为被删除的值
                   <List>.remove(item)
                   del(<List>[index])  #每有返回值
 
range()函数
range(stop)  返回[0,stop)步长为1的List
range(start,stop)  返回[start,stop)步长为1的List
range(start,stop,step)  返回[start,stop)步长为step的List
 
遍历一个List可以有两种方式:
1、for i in list  #可以直接访问每一个值 但是不能对值进行变更
2、for i in range(len(list))  #通过访问下标,可以对值进行变更
 
List合并
a = [1,2,3]
b = [4,5,6]
c = a+b
c == [1,2,3,4,5,6]
""" c = b+a  c==[4,5,6,1,2,3] """
 
 
07-Special. Battleship
这是一个神奇的操作。
["O"] * 5  #返回 ["O", "O", "O", "O", "O"]
 
输出格式
print <String>.join(<List>)
在<List>中间插入<String>输出,<String>起分隔符作用
 
随机数生成:
from random import randint
randint(low,high)生成一个[low,high]的整数 注意这里两段都可以达到
 
强制跳出循环:break
 
08. Loops
 
while循环的写法
while <conditions>:
  <循环体>
 
while/else 语句
格式
while <conditions>:
  <循环体>
else:
  <程序段>
当<conditions>不满足时,执行else后面的内容。
但是当从循环体中break出去的时候,else里的内容不会执行。
 
2019 JCarlson Works All Rights Reserved
Avatar_small
Recursion 说:
2017年12月07日 22:45

吉卡森更博客啦!吉卡森赛高!

Avatar_small
SEBA Model Paper 说:
2022年8月27日 15:14

Every year Students Struggle to acquire the Pass marks in 10th Examination. Student Download Assam HSLC Question Paper 2023 After Regular Prepare to Participate in must take a printout of Assam Board HSLC Model Paper 2023 available our Website. Student’s use Previous Paper and Start Preparation Assam HSLC Final Exam 2023 Every year Students Struggle to acquire the Pass marks in 10th Examination. SEBA Model Paper Student Download Assam HSLC Question Paper 2023 After Regular Prepare to Participate in must take a printout of Assam Board HSLC Model Paper 2023 available our Website. Student’s use Previous Paper and Start Preparation Assam HSLC Final Exam 2023


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter