Week 0 - CS50's Introduction to Programming with Python / SQL 作者: bluish 时间: 2025-08-15 分类: 未分类 #Python #Shorts ##VSCode basic ls #list cp hello.py goodbye.py #copy a file mv goodby.py farewell.py #move or rename rm #remove mkdir folder #make a folder rmdir folder #delete a folder mv farewell.py .. #..代表前一级 cd #根目录 ##Functions print("Hello World!") ``` def main(): print("Hello World!") #定义函数 ``` ``` def hello(content="world"): #可以设置默认值 print("hello "+content) ``` ##Variables time="7:00 AM" print(time) print("Now, it's",time ) #用,连接 print("Now, it's "+time) #用+连接 print(f"Now, it's {time}") #用外f+内{}指代 time=123123123 print(time+60) #数值加60 ##Return Values ``` def area(x,y): print(str(x*y)+" square feet") return x*y #Return Value and End def main(): house = area(5,20) print(house) main() ``` ##Side Effects ``` emotion="v.v" def say(phrase): print(phrase+" "+emotion) def main(): global emotion #用global来修改全局变量 say("Hi!") emotion="Q.Q" #修改变量 say("Hello!") main() ``` ##String Methods name = input("What's Your name?") # hao wu print(name.capitalize()) #Capitalize为字符串自带的方法,并非函数 print(name.title()) #单词首字母大写 print(name.strip()) #去除前后空格 print(name.strip().title()) #接续 namelist=['apple','banana','peach'] print(", ".join(namelist)) #字符串方法,针对“分隔符”的join方法,join上需要使用此分隔符处理的内容 ##Else In Lecture first, last=name.split(" ") #通过分隔符切割字符 round(x+y,2) #四舍五入,第二参数限定小数位数 print(f"{z:,}") #数值自身分隔,如1,000s print(f"{z:.2f}") #第二参数限定小数位数 #Problem Set 0 str.upper() #全部大写 str.lower() #全部小写 str.replace(" ","...") #将第一参替换成第二参 pow(a,b) #a的b次方 #SQL ##数据框创建与输入 - 创建sqlite db数据框 sqlite3 hr_data.db - 创建表、字段与主键 ``` create table tablename( field1 integer primary key, field2 text, field3 numeric, field4 date ); ``` deptno integer,foreign key(deptno)references dept(deptno) #外键——dept表中的deptno字段 - 运行sql代码 ``` sqlite3 hr_data.db Week 0 - CS50's Introduction to Programming with Python / SQL http://bluish.net/archives/2340/ 作者 bluish 发布时间 2025-08-15 许可协议 CC BY-SA 4.0 复制版权信息 标签: none