学习日记 | 5.18 [Python3] Python3基础与面向对象

注:这是一系列基于实验楼网络培训的python学习日记,内容零散,只是便于我自己回顾,有需要请了解www.shiyanlou.com。

1. Github相关

首先是复习github相关操作:

1.1 完成创建账号和仓库

登陆github.com,创建new repository,自动初始化README.md。

1.2 使用ssh-keygen生成公私钥

建议密钥保存在默认位置中:终端输入ssh-keygen,默认保存密钥位置为/home/shiyanlou/.ssh/id_rsa,一路回车。然后用ls -a可以看到隐藏文件,进入.ssh可以看到名为id_rsa(私钥)和id_rsa.pub(公钥)的两个文件。

打开github用户设置里面的SSH and GPG keys项目,将公钥内容粘贴进去。

1.3 配置git工具

1 git --version                              # check whether git has been installed
2 sudo apt-get install git -y                # install git
3 git config --global user.name "username"   # for example, xxxx0101
4 git config --global user.email "useremail"  

打开github的repository,将ssh链接复制到剪切板上,输入以下命令:

 1 cd "yourfolder"
 2 git clone "xxxxxxxxxx"   # copied link
 3 cd "yourRepoName"
 4 # create with echo or touch
 5 # then add these new files
 6 git add "newFileName"
 7 # delete file
 8 git rm "someFile"
 9 
10 # restore the file
11 git reset --hard HEAD
12 
13 # commit after change
14 git commit -m "your activity/change description"  # use m when commit for the first time 
15 
16 # then push, and change the repo
17 git push -u origin master
18 
19 #check the changes 20 git fetch origin

2. 实验3: Python 基础语法

用vim circle.py创建文件,并编辑:

#!/usr/bin/env python3
# 第一行叫shebang,告诉shell使用python3执行代码

from math import pi # calculate result = 5 * 5 * pi print(result)

完成后输入chmod +x circle.py增加权限,并使用./circle.py执行。

输入多行字符串使用三个“来解决。

strip()去除首尾,split()切割单词。

break代表停止循环,continue代表跳过当前轮循环。

用try except finally捕捉异常,finally是无论任何情况都会执行的代码,例如:

filename = '/etc/protocols'
f = open(filename)
try:
    f.write('shiyanlou')
except:
    print("File write error") finally: print("finally") f.close()

通过如下方法查询模块搜索路径:

import sys
sys.path

每一个.py文件都是一个模块。

如果要将一个文件夹识别为包的话,这个文件夹里需要有__init__.py的文件,内容可以为空,此时可以用 import 文件夹(包)名.文件(模块)名 来引入。

用sys模块的sys.argv获取【命令行参数】:

#!/usr/bin/env python3

import sys
print(len(sys.argv))

for arg in sys.argv:
    print(arg)

执行结果:

$ python3 argtest.py hello shiyanlou
3
argtest.py     # argv[0]         
hello          # argv[1]
shiyanlou      # argv[2]

每一个python文件的__name__属性默认为本身文件名不含.py的形式,如hello.py的__name__属性为hello。在终端中用python解释器执行此文件时,__name__的值就变成__main__。当此文件作为模块倒入其他文件时,__name__保持默认。

猜你喜欢

转载自www.cnblogs.com/squidGuang/p/9059230.html