Python Basics

学习语言的四要素

  1. terms of the language

  2. data types

  3. actions

  4. best practices

Data Types

fundamental data types

number : int & float & complex

  • print(2 / 4) #0.5,type is float,int型运算可得float型
  • m**n m的n次方
  • m//n 得数为integer
  • %取余
  • 以二进制形式存储

math functions(just Google it)

  • round 四舍五入,只写源数据,默认整数;写源数据和n,保留n位小数
  • abs 取绝对值

operator precedence运算符优先级(低->高)

  • ()
  • **
  • * /
  • + -

**bin(5) **求5的二进制

int(‘0b101’,2) <==> int(0b101) 告诉计算机0b101是二进制形式,并将其转换为int型,第一个值为字符串或数,第二个值表示进制

variables

  • snake_case
  • start with lowercase or underscore
  • cannot start with numbers
  • letters, numbers, underscores
  • case sensitive 同拼写大小写不同为不同变量
  • don’t overwrite keywords 特殊用途的单词不能用作变量名

constants

  • PI=3.14 大写变量名

a,b,c = 1,2,3 => a=1;b=2;c=3;

expression : iq/5 & statement : user_age = iq/5;

augmented assignment operator

  • a=5; a=a+2; print(a); #此时a=7
  • a=a+2 <=> a+=2

str

  • ‘Hello World’ “Hello World”
  • 长字符串可以’’’(中间可以换行,任意格式)’’’

string concatenation

  • adding strings together
  • str不能直接与int相连

type conversion

  • print(type(str(100))) #<class ‘str’>

escape sequence

  • weather = ‘It’s sunny’ ×
  • weather = 'It\‘s sunny’ √
  • the symbol after \ is considered as a part of the str
  • \t : tab
  • \n : a new line

formatted strings

1. hi Johnny. You are 55 years old.

  • print(‘hi ‘+name+’. You are ‘+str(age)+’ years old.’)
  • print(f’hi {name}. You are {age} years old.’) #python3
  • print(‘hi {}. You are {} years old.’.format(‘Johnny’,‘55’))
  • print(‘hi {}. You are {} years old.’.format(name,age))

2. hi Sally. You are 26 years old.

  • print(‘hi {new_name}. You are {new_age} years old.’.format(new_name=‘Sally’,age=26))

3. hi 55. You are Johnny years old.

  • print(‘hi {1}. You are {0} years old.’.format(name,age))

string indexes

  • be stored as ordered sequences character
  • str=‘me me me’ str[0]=‘m’, str[1]=‘e’, str[2]=’ ';
  • str=‘01234567’
  • string slicing : str[start:len] str[0:2]=‘01’
  • str[start:len:stepover] str[0:8:2]=‘0246’ str[::1]=‘01234567’
  • str[-1] 末位
  • str[::-1]=‘76543210’
  • immutability不变性
  • strings are immutable cannot change once it was created
  • str=‘0123’; str[0]=‘8’; 报错

built-in functions&methods

  • functions内置函数 : len(str) #求字符串长度
  • methods : str.methods() //creating a new string,对原本的str不改变
  • .upper()#全部大写
  • .lower()#全部小写
  • .capitalize()#首字母大写
  • .find()#找到首次出现位置
  • .replace(’’,’’)#用后面的替换前面的
    在这里插入图片描述

bool 1->True/0->False
在这里插入图片描述
commenting your code #给代码加注释

don’t read dictionary #无需详细了解每一个函数,Google it

list -> Data Structure

  • list slicing : a new copy
    在这里插入图片描述

  • mutability可变性 :可以改变list中的某一个值

  • new_cart = amazon_cart; new_cart[0] = ‘xx’ =>new_cart和amazon_cart同时变,相当于大名和小名,找到相同的位置

  • new_cart = amazon_cart[:];
    new_cart[0] = ‘xx’ =>new_cart形成一个和amazon_cart一样的list

  • .pop() 去掉最后一个元素 .pop(i) 去掉第i个
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • .sort() 在原list进行操作 .sorted() 新建一个copy进行操作
    在这里插入图片描述

list upacking

  • a,b,c = [1,2,3]
  • a,b,c,*other,d = [1,2,3,4,5,6,7,8,9] => a=1,b=2,c=3,other=[4,5,6,7,8],d=9

Matrix a way to describe 2-D lists -> machine learning
在这里插入图片描述

  • print(matrix[0][1]) #2
    

none -> it’s just nothing

dict(hash table/map/objects) -> data structure [不连续存储,不一定有序]

  • dictionary = {
    	'key': value, # value值类型无需相同
    	'b': 2, # print(dictionary['b']) -> 2
        ...
    }
    

在这里插入图片描述

  • key must be immutable & unique(or it’ll be rewrite)

  • user.get(‘age’,55) #在user字典中寻找age的value,如果没找到就用55

  • .update({‘key’:value}) 更新数据

understand DATA STRUCTURES!!!

tuple -> data structure immutable list

list相当于数组,tuple相当于结构体

set 不计重复元素
在这里插入图片描述

#classes -> custom types自定义

#specialized data types

#Moudules

if … elif …

is vs ==

  • == covert to the same type ; -> ‘1’ == 1 False
  • is won‘t convert,必须完全相同(地址&内容); -> [1,2,3] is [1,2,3] False List is a memory in location

iterable -> list, dict(.items()/.values()/.keys()), tuple, set, string

iterated ->one by one check each item in the collection

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值