【自用】我非得学点py不可

学了这么多年编程(C++/C),到现在连脚本都不会写,我是fw。

其实是学过一点点py的,但是没有实际编程能力 = 没用。

我要好好学点py,要写出能应付一点事的脚本。

教材:Python 教程

先配好了环境。

py用缩进表代码块。

#来注释

变量不用声明,直接用。

x = 5
y = "Hello, World!"

而且还能中途易辙(?)

x = 5 # x is of type int
x = "Steve" # x is now of type str
print(x)

可以用 + 连接字符串,但是不能直接连接字符串和数字。

x = "Python is "
y = "awesome"
z =  x + y
print(z)

想连接很多个字符串(比如连接列表)可以直接用join函数

可以认为py里面只有int,double和string三种变量。

可以用裁切来处理string。

b = "Hello, World!"
print(b[2:5])

索引可以带符号,表示从末尾往前数。

string的长度:len(s)


 函数貌似是随处都可定义的,不像C++需要在主程序之外定义。

x = "awesome"

def myfunc():
  x = "fantastic"
  print("Python is " + x)

myfunc()

print("Python is " + x)

global 可以在函数里用全局变量。

x = "awesome"

def myfunc():
  global x
  x = "fantastic"

myfunc()

print("Python is " + x)
原文地址:https://www.cnblogs.com/qwerta/p/14520807.html