day4 基础

    1.变量    
    2.打印名片
    3.raw_input() 和 input()
    4.变量类型 type
    5. if-else  
    6.标示符的规则
    7.关键字
    8.运算符
    9.print打印多个值

1.变量

score = 100 #定义了一个变量, 变量名字score  它里面存储了一个数值 100
 
 #变量名 = 数值
 
 a = 180 #单位是cm
 high = 180 #身高180cm
 
 
 applePrice = 3.5 #苹果的价格 元/斤
 weight = 7.5  #购买的苹果的重量  斤
 
 money = applePrice * weight  #如果money=xxx是第一次的话,那么就表示定义>    了一个变量
 
 money = money - 10 #如果money = xxx 不是第一次出现,那么就不是定义变量,
 而是给这个已经存在的变量赋上一个新的值 

 2.打印名片

  1. input获取必要的信息
  2. print打印名片
#-*- coding:utf-8 -*-
 #1.input获取必要的信息
 name = input("请输入名字:")
 age = input("请输入年龄:")
 sex  = input("请输入性别:")
 qq = input("请输入qq号码:")
 
 
 #2.print打印名片
 print("================")
 print("姓名:%s"%name)
 print("姓名:%s"%age)
 print("姓名:%s"%sex)
 print("姓名:%s"%qq)
 print("================")

 3.raw_input() 和 input()

  python2版本 input  , 当成代码去执行 1+4

python@ubuntu:~/pythons06$ ipython
Python 2.7.12 (default, Jul  1 2016, 15:12:24) 

In [1]: name = input("请输入名字:")
请输入名字:alex
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-70a3a8047e72> in <module>()
----> 1 name = input("请输入名字:")

<string> in <module>()

NameError: name 'alex' is not defined

  

python@ubuntu:~/pythons06$ ipython

In [1]: age = input("请输入:")
请输入:1+4

In [2]: age
Out[2]: 5

In [3]: age = 1+4

In [4]: age 
Out[4]: 5


  python3版本 input  ,接受表达式输入

python@ubuntu:~/pythons06$ ipython3
Python 3.5.2 (default, Jul  5 2016, 12:43:10) 
Type "copyright", "credits" or "license" for more information.


In [1]: name = input("your name:")
your name:alex

In [2]: name
Out[2]: 'alex'

In [3]: print("%s"%name)
alex

  python2中的raw_input(),接受表达式输入

python2 版本

In [2]: name = raw_input("请输入:")
请输入:alex

In [3]: name
Out[3]: 'alex'

 4.变量类型 type

python@ubuntu:~/pythons06$ python3 10-判断一个年龄.py
输入你的年龄:18
Traceback (most recent call last):
  File "10-判断一个年龄.py", line 8, in <module>
    if age >= 18 :
TypeError: unorderable types: str() >= int()
In [1]: a = 100
In [2]: b = 3.14
In [3]: c = "alex"

In [4]: type (a)
Out[4]: int
In [5]: type (b)
Out[5]: float
In [6]: type (c)
Out[6]: str

5. if-else   

  if 用来完成判断

  if 条件:
    条件成立的时候,做的事情
  else:
    条件不成了的时候 做的事情

 #-*- coding:utf-8 -*-
 
 #1 input 获得输入年龄   
 age = input("输入你的年龄:")#input获取的所有数据是字符串类型,  20 --》age --》 “20”
 age_num = int(age)   #----》整型    去除了双引号之后的值  20
 
 #2 if 如果年龄大于18:
 if age_num >= 18 :
     print("已经成年,可以看18禁")
 #  else: 
 else:
     print("回家写作业去")
执行结果:
python@ubuntu:~/pythons06$ python 11-if-else.py 
输入你的年龄:22
已经成年,可以看18禁
python@ubuntu:~/pythons06$ python 11-if-else.py 
输入你的年龄:11
回家写作业去

 if 条件满足,执行多少条语句

age = 10

if age >= 18 :
    print("----18---")
    print("----18---")
    print("----18---")
    print("----18---")
else:
    print("----8---")
    print("----8---")
    print("----8---")
print("----10---")

    

 6.标示符的规则

  • 标示符由字母、下划线和数字组成,且数字不能开头

   afdaAFJAL             字母
     _     _              下划线
           733                 数字
    
     _3453243                 数字不能开头
    a343435                
  •  python中的标识符是区分大小写的

 

 命名规则

  •  见名知意

    名字 就定义为 name , 定义学生 用 student

  • 驼峰命名法

  

    小驼峰式命名法:例如:myName、aDog

    大驼峰式命名法:例如:FirstName、LastName

  • 下划线

    在程序员中还有一种命名法比较流行,就是用下划线“_”来连接所有的单词,比如send_buff

7.关键字

  • 什么是关键字

    python一些具有特殊功能的标示符,这就是所谓的关键字

    关键字,是python已经使用的了,所以不允许开发者自己定义和关键字相同的名字的标示符

>>> import keyword
>>> 
>>> 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', 'return', 'try', 'while', 'with', 'yield']

  

8.运算符

     

    

In [3]: 2*2
Out[3]: 4

In [4]: 2**2        #次方
Out[4]: 4

In [5]: 2**3
Out[5]: 8

In [6]: 

In [6]: 2**10        #重要
Out[6]: 1024

In [7]: 2**16        #重要
Out[7]: 65536
In [10]: 100*3.14
Out[10]: 314.0


In [11]: "H"*10
Out[11]: 'HHHHHHHHHH'

In [12]: "sorry"*10
Out[12]: 'sorrysorrysorrysorrysorrysorrysorrysorrysorrysorry'


In [14]: "="*50       #重要
Out[14]: '=================================================='

 9.print打印多个值

name = "alex"
age = 23
sex = "man"

print("姓名是:%s,年龄是:%d,性别是:%s"%(name,age,sex))

 

原文地址:https://www.cnblogs.com/venicid/p/7825441.html