您的位置:首页 > 编程语言 > Python开发

python3.4学习笔记(一) 基本语法 python3不向下兼容,有些语法跟python2.x不一样

2015-06-22 20:32 766 查看
python3.4学习笔记(一) 基本语法 python3不向下兼容,有些语法跟python2.x不一样,IDLE shell编辑器,快捷键:ALT+p,上一个历史输入内容,ALT+n 下一个历史输入内容。#idle中按F5可以运行代码

BIF --> built in functions 查询python有多少BIF内置函数的方法: dir(__builtins__)

['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']

查询内置函数的用法:help(函数名)如: help(str)

helloworld.py

temp = input("input number:")
guess = int(temp)
if guess == 8:
print("is 8")
else:
print("is 8")
print("tab is useing")
print("out game")

frist = 3
second = 8
third = frist + second
print(third)

str1 = "string1"
str2 = " string2"
str3 = str1 + str2
print(str3)

------------------------------
input number:8
is 8
out game
11
string1 string2


结束语句不需要分号,:冒号后回车会自动缩进,tab键是有意义的,不仅仅是代码缩进,缩进里面属于同一个逻辑模块。 在非逻辑层次结构的代码前面不能使用空格,tab键。

变量不需要指定类型,会自动转换。python没有"变量",只有"名字"。变量就是可变的,变量使用之前需要先赋值。 数字相加,字符串拼接,字符串属于文本。

#前后必须使用同样的单引号或双引号,必须是英文半角的字符。 print('5' + "8")

#反斜杠可以作为转义字符串,原始字符串在字符串前面加上r即可,在结尾不能加反斜杠的

#跨越多行的字符串:三重引号字符串(可以是三个单引号或者三个双引号),把内容放中间,可当成多行注释使用

print('c:\\now I\'am')
print(''' line1
line3
''')
print("hello\n" * 3) #会打印3次


#注释使用#号,如果是使说明性注释,可以用__doc__ str.__doc__ #输出str函数的文档描述内容 python 的一个特点是不通过大括号 {} 来划定代码块,而是通过...'__doc__', '__eq__', '__format__', '__ge__', '__getattribute...

============================================

#引入外部模块 import random
#random模块,randint(开始数,结束数) 产生整数随机数
import random
secret = random.randint(1,10)
temp = input("请输入一个数字\n")
guess = int(temp)
count = 0;
while guess != secret: #猜错的时候才进入循环条件
if count == 0 and guess > secret:
print("猜大了")
if count == 0 and guess < secret:
print("猜小了")
temp = input("请重新输入\n") #需要在判断之前让用户输入,否则猜对了就直接退出了
guess = int(temp)
count += 1 #不能使用count++这种方式
if count > 2:
print("猜错4次自动退出了")
break #退出循环
if guess == secret:
print("恭喜,你猜对了")
print("猜对了也就那样")
else:
if guess > secret:
print("猜大了")
else:
print("猜小了")
print("游戏结束")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: