python3中的基本语法总结

1.python中的变量 需要注意的一点是区分大小写,a和A是两个不同的变量,声明的时候不需要 像java语法一样声明类型, 赋值方式为 a = 12 .
(前提是安装过python电脑上)windows10环境下打开命令行输入python后然后输入a=12,回车就完成了变量的赋值
2.print()函数的作用是打印 ,继续在命令行里输入print(a),会出现下面的打印结果:
这里写图片描述
3.python中的字符串不区分单双引号,‘aaa’与“aaa”效果是一样的,而三引号被用于过长的文字或者说明,如下“‘aaaaaaa……只要不写后面的三引号,你就可以随意换行写下面的文字”’ ,python中的字符串连接用+号实现,这一点和java语法一样
4.python中变量类型转换:
下面代码举个例子演示如何把两种不同类型的变量转为相同类型合并为一个变量输出打印

num = 1
string = "1"
num2 = int(string)
print(num + num2)

5.python不仅可以将字符串相加,还可以让他们相乘,有意思吧,看下面代码:

if __name__ == '__main__':
    string = "1"
    words = 'words' * 3
    print(words)
    # 好,来一个更复杂点的
    word = 'a looooooooooooooong word'  
    num = 12
    total = string * (len(word)-num)
    print(total)  

打印结果:
wordswordswords
1111111111111
到这里我们基本就知道字符串的用法了,Cool!!!

6.字符串的分片与索引:

 #字符串可以通过string[index]的方式进行索引,分片。字符串的分片可以理解为从字符串中截取你想要的部分,但并不会对源文件进行改动。而是从源文件复制出你想要的部分另存为一个副本,这里需要注意的是,分片的时候是字符串的第一个字符下标为0,然后每个空格也会算是一个下标
    name = 'My name is Mike'
    print(name[0])
    #输出    'M'
    print(name[-4])     //倒数第四个标
    #输出    'M'
    print(name[11:14])  //从下标11到下标14,但不包括第14个下标的字符
    #输出    'Mik'
    print(name[11:15])  //从下标11到下标15,但不包括第15个下标的字符
    #输出    'Mike'
    print(name[5:])     //从下标5到字符串末尾所有下标内容
    #输出    'me is Mike'
    print(name[:5])     //从下标0到下标5,不包括第5个标
    #输出    'My na'

贴一张图更清晰的展示每一个字符串中每个字符所占的下标(分正序和反序两种类型):
这里写图片描述
7.字符串还有替换的方法:

 name = 'My name is Mike'
 print(name.replace(name[:6],'*'*6))

输出结果:******e is Mike
8.字符串格式化:
当字符串中有多个这样的“空”需要填写的时候,我们可以使用.format()进行批量处理,它的使用方法有如下几种:

    print('{} a word she can get what she {} for.'.format('with','came'))
    print('{preposition} a word she can get what she {verb} for'.format(preposition = 'with',verb = 'came'))
    print('{0} a word she can get what she {1} for.'.format('with','came'))

打印结果如下:
with a word she can get what she came for.
with a word she can get what she came for
with a word she can get what she came for.
字符串格式化填空在项目中举例:
这种字符串填空的方式使用很广泛,例如下面代码可以填充网址中空缺的城市数据
city = input(“write down the name of city:”)
url = “http;//spistroe.baidu.com/microservice/weather?citypinyin={}”.format(city)
print(url)
输入结果是交互式的,会提示你输入城市名称,然后回车会看到拼接后的url字符串打印
这里写图片描述
9.关于python中的函数:
这里写图片描述
这里写图片描述
这里很容易会遇到一个关于缩进的异常,可以点击这里查看关于这个异常的深度剖析,重点是你要真正理解python中的没一个方法区分范围的原理

下面开始我们自己写一个python的函数,类中记得引入math方法哦

import math

def calculatedLength(a, b):
    c =int(math.sqrt(a * a + b * b) )  # 用勾股定理计算出三角形的最长斜边长度  math.sqrt()函数是python中的开方函数
    return str(c)

if __name__ == '__main__':
    print('三角形最长斜边长度为:'+calculatedLength(3,4))

输出结果为:三角形最长斜边长度为:5
这里附上:math中其他常用的数学函数:

ceil(x) 取顶
floor(x) 取底
fabs(x) 取绝对值
factorial (x) 阶乘
hypot(x,y)  sqrt(x*x+y*y)
pow(x,y) xy次方
sqrt(x) 开平方
log(x)
log10(x)
trunc(x)  截断取整数部分
isnan (x)  判断是否NaN(not a number)
degree (x) 弧度转角度
radians(x) 角度转弧度

10.关于python中的类型互转:

int(str) 函数将 符合整数的规范的字符串 转换成 int 型。

    num1 = "123";
    num2 = int(num1);
    print("num2: %d" % num2);

输出结果为 num2: 123

float(str) 函数将 符合 浮点型 的规范的字符串 转换成 float 型。

num1 = "123.12";
num2 = float(num1);
print("num2: %f" % num2);

输出结果为 num2: 123.120000

str(num) 将 整数,浮点型转换成 字符串

num = 123;
mystr = str(num);
print ("%s" % mystr);

出结果为 123

猜你喜欢

转载自blog.csdn.net/wjj1996825/article/details/80906543