day1- Python基本语法 流程控制

一、Hello World程序

1.1. 在Linux中创建程序

  1) 在linux 下创建一个文件叫hello.py,并输入: 

1 print("Hello World!")

  保存后,然后执行命令:python hello.py ,输出:

  Hello World!

  2) 直接在Linux交互器中运行 

1 localhost:~ jieli$ python
2 Python 2.7.10 (default, Oct 23 2015, 18:05:06)
3 [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
4 Type "help", "copyright", "credits" or "license" for more information.
5 >>> print("Hello World!")
6 Hello World!

1.2. 在Windows中创建程序

  使用Pycharm工具,具体在上一博客章节中已经介绍过,不再细说。

 

二、Python基本语法

2.1 变量

   声明变量:

1 #_*_coding:utf-8_*_
2 
3  name = "yangts"

  变量定义的规则:

    • 变量名只能是 字母、数字或下划线的任意组合
    • 变量名的第一个字符不能是数字
    • 以下关键字不能声明为变量名
      ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

2.2 变量的赋值

1 name = "yangts"
2  
3 name2 = name
4 print(name,name2)
5  
6 name = "Jack"
7  
8 print("What is the value of name2 now?",name2)

  程序运行结果如下图所示:

1 yangts yangts
2 What is the value of name2 now? :  yangts

2.3  Python注释

  当行注释:  # 被注释内容

  多行注释:  """ 被注释内容 """

2.4  用户输入

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

  运行程序,输入yangts,将打印如下结果: 

What is your name?    yangts
Hello yangts

  输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法,即:

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3   
 4 import getpass
 5   
 6 # 将用户输入的内容赋值给 name 变量
 7 pwd = getpass.getpass("请输入密码:")
 8   
 9 # 打印输入的内容
10 print(pwd)

2.5 数据类型初识 

  1)字符串

  python中的字符串在C语言中体现为是一个字符数组,每次创建字符串时候需要在内存中开辟一块连续的空,并且一旦需要修改字符串的话,就需要再次开辟空间,万恶的+号每出现一次就会在内从中重新开辟一块空间。

  字符串格式化输出:

1 name = "yangts"
2 print("I am is %s" % name)
3 
4 #输出    I am is yangts
1 name = input("name:")
2 age = int(input("age:") ) #integer
3 
4 info = '''
5 -------- info of  %s  -----
6 Name:%s
7 Age:%d
8 ''' % (name,name,age)
View Code
1 name = input("name:")
2 age = int(input("age:") ) #integer
3 
4 info2 = '''
5 -------- info of {_name}  -----
6 Name:{_name}
7 Age:{_age}
8 '''.format(_name=name,
9            _age=age)
View Code
1 name = input("name:")
2 age = int(input("age:") ) #integer
3 
4 info3 =  '''
5 -------- info of {0} -----
6 Name:{0}
7 Age:{1}
8 '''.format(name,age)
View Code

  这三种运行结果如下: 

name:yangts
age:26

-------- info of yangts -----
Name:yangts
Age:26

 PS: 字符串是 %s;整数 %d;浮点数%f

  2) 列表 

1 name_list = ['yangts', 'seven', 'eric']

  可用name_list[0]来访问

2.6  逻辑运算

  猛击这里

三、Python流程控制语句

3.1 if...else

1 if name == "yangts" and passwd == "sznari":
2     print("you write right!")
3 else:
4     print("error")

3.2 for

1 for i in range(0,10,3):
2     print("loop:", i )

  python 使用缩进代表一个代码段,上述代码运行如下:

1 loop: 0
2 loop: 3
3 loop: 6
4 loop: 9

3.3   while

1 count = 0
2 while count < 3:
3     print("count:",count)
4     count += 1  # 每次loop 计数器+1
5 else:
6     print("else count:",count)

  程序打印如下:

count: 0
count: 1
count: 2
else count: 3

 

posted @ 2018-04-18 23:14  心随·风动  阅读(115)  评论(0)    收藏  举报