python学习之路1,,Hello Worl


Hello World程序

学习语言必经之路。

1 #!/usr/bin/env python2   3 print "hello,world"

在交互器中执行

除了把程序写在文件里,还可以直接调用python自带的交互器运行代码。

D:\python>python "hello world.py"Hello World

Pyhthon基础语法

标识符/关键字

第一个字符必须是字母或下划线。

标识符由字母、数字和下划线组成。

标识符对大小写敏感

  import keyword   #引入模块
keyword.kwlist #keyword保存了python所有关键字。

变量\字符编码

声明变量
#_*_coding:utf-8_*_ name = "Mr peng"

上述代码声明一个变量,变量名为name值为"Mr peng"

注释

单行--->#

多行--->"""被注释的内容"""(多行注释需要成对出现)。

行的缩减

python最具特色的就是使用缩进来表示代码块,不需要使用大括号{}。

缩进的空格数是可变的,但是同一个代码块的语句必须包含相同的缩进空格数。实例如下:

同一代码块缩进时空格数必须相同。

多行语句

Python 通常是一行写完一条语句,但如果语句很长,我们可以使用反斜杠(\)来实现多行语句。

  
total = item_one + \
item_two + \
item_three

在 [], {}, 或 () 中的多行语句,不需要使用反斜杠(\),例如:

  
total = [‘item_one‘, ‘item_two‘, ‘item_three‘,
‘item_four‘, ‘item_five‘]

同一行显示多条语句

Python可以在同一行中使用多条语句,语句之间使用分号(;)分割

用户输入

#!/usr/bin/env python#_*_coding:utf-8_*_  #name = raw_input("What is your name?") #only on python 2.xname = input("What is your name?") #python 3.#print("Hello " + name )

输入密码时:在Pycharm5.0.3中引用:

#!/usr/bin/env python# -*- coding: utf-8 -*-import getpass#会出现执行不下去pwd = getpass.getpass("请输入密码:") 

查看print详细信息

print() ---->ctrl+鼠标左键 可以查看函数详细信息

  
def print(*args, sep=‘ ‘, end=‘\n‘, file=None): # known special case of print
"""
print(value, ..., sep=‘ ‘, end=‘\n‘, file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
"""
pass

条件判断

模拟用户登陆
#提示输入#if判断用户名和密码#_username 存放用户名;_password存放密码#!/usr/bin/env python# -*- coding:utf-8 -*-# Author:Mr pengimport getpass   #导入getpass模块_username = "junpeng"_password = "123"username = input("username:")#密文输入。在pycharm中会卡住,代码是没有问题password = getpass.getpass("password:")#注意代码的缩减if _username == username and _password == password:    print("Welcome user {name} login..."          .format(name=username))else:  #    print("Invalid username or password!")

猜测商品价格游戏

#根据系统提示猜测商品价格#仅限猜测3次,超过则提示#    如果正确,则输出bigger#    猜多猜少均有提示#!/usr/bin/env python# -*- coding:utf-8 -*-# Author:Mr pengC_PRICE = 1520count = 0while count < 3:    price = int(input("输入商品价格"))    if price == C_PRICE:        print("bigger")        break    elif price < C_PRICE:        print("少了")    else:  #表示排除以上两种剩余的哪些        print("多了")    count += 1    if count == 3:        countlink = input("按Enter键继续n结束")        if countlink != ‘n‘:            count = 0

条件循环

Python循环语句有 for 和 while.

技术分享图片

for语句

Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。

for循环的一般格式如下:

for i in range(0,10):    print("loop",i)    if i == 5:        breakelse:  #for循环正常结束,就能执行    print(‘结束‘)

for的遍历实例:

languages = ["C", "C++", "Perl", "Python"] for x in languages:    print (x)
whlie语句

有一种爱叫做天长地久,有一种循环叫做死循环,一旦运行,就运行到天荒地老。

天荒地老代码:

count = 0while True:    print(count)    count +=1

Python3 解释器

由两种交互式编程(命令行) 脚本式编程(编写完程序后,边解析边执行)

python学习之路1

评论关闭