Python基础语法

编码

# -*- coding: cp-1252 -*-
# -*- coding: utf-8 -*-

交互式编程

-Windows
C:\Users\cy>python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bi
t (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
-Linux
[root@izwz91cugo68lt8hfj4j0uz ~]# python
Python 2.7.5 (default, Aug  4 2017, 00:39:18) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

脚本式编程

[root@izwz91cugo68lt8hfj4j0uz ~]# python test.py 
Hello,World!
[root@izwz91cugo68lt8hfj4j0uz ~]# ./test.py 
Hello,World!
[root@izwz91cugo68lt8hfj4j0uz ~]# 

Python命名规范

文件名部分:
    大小写字母、数字、下划线
    不用数字开头
    名称有意义
    避免使用python保留字和关键字
文件后缀:
    .py
    .pyw

Python关键字

>>> import keyword
>>> print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', '
def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if',
 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'retu
rn', 'try', 'while', 'with', 'yield']
>>>

缩进

注释

-行注释
print("Hello,World!") #打印Hello,World!
-块注释
"""
多行注释
随便写点
"""

输出与输入

-print()

默认换行

print("这是")
print("在不同行")

使用end=”“使输出在同一行

print("这是",end="")
print("在同一行")
-input()
u_name=input("Please input your name:")
print("Hi %s" % u_uname)
print("Hi {0}".format(u_name))

你可能感兴趣的:(Pyton)