python的变量不需要定义
每一句的结尾也不需要写分号
a = 10 // a为整型
b = 'hello' // a为字符串型
c = 4.6 // a为浮点型
b = 'hello' // a为字符串型
c = 4.6 // a为浮点型
输出
用 print()在括号中加上字符串,就可以向屏幕上输出指定的文字。比
如输出'hello, world'
如输出'hello, world'
python 的print很方便
print(a)
10
print(b)
hello
print(b*a)
hellohellohellohellohellohellohellohellohellohello
输入
Python 提供了一个 input(),可以让用
户输入字符串,并存放到一个变量里。
户输入字符串,并存放到一个变量里。
name = input('please enter your name:') // ,程序一运行,会首先打印出 please enter your name:
count = input() // 如果你输入的是一个数字,count也将不会是数字,input()默认输入的是字符串类型可以用强制类型转换转换成int型或其他类型
if else 条件判断
a = 100
if a >= 0:
print(a) // 当语句以冒号:结尾时,缩进的语句视为代码块。
else:
print(-a)
a = 100 if a >= 80: print('Good')
elif a >= 60:
print('Pass')
else:
print('Fail')
整数浮点数(略)
字符串
字符串是以单引号'或双引号"括起来的任意文本,比如'abc',"xyz"等
等。请注意,''或""本身只是一种表示方式,不是字符串的一部分,因
此,字符串'abc'只有 a,b,c 这 3 个字符。如果'本身也是一个字符,
那就可以用""括起来,比如"I'm OK"包含的字符是 I,',m,空格,O,K
这 6 个字符。
但如何把字符串里包含的 ' 给输出来呢?可以用转义字符\来标识
如果字符串里面有很多字符都需要转义,就需要加很多\,为了简化,
Python 还允许用 r''表示''内部的字符串默认不转义
print('I\'m learning\nPython.') I'm learning Python print(r'\\\t\\')
\\\t\\