python基础学习笔记第一天

1 、 在inux和 UNIX系统安装中(包括Mac OS X),Python的解释器就已经存在了。输入python命令使用

liuyangdeMacBook-Pro:~ liuyang$ python

Python 2.7.10 (default, Jul 30 2016, 18:31:42) 

[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> 

输入一下命令,看其是否可以正常运行。

>>> print "hello,word"

hello,word

解释器路径

#!/usr/bin/env python

编码格式

# -*- coding:utf8 -*-   

ps.python3.5版本可以不指定编码格式

python两种执行方式

python解释器 py文件路径
python 进入解释器:
实时输入并获取到执行结果

2 、数字和表达式:

>>> 1-2
-1
>>> 1+2

>>> 32784*13

>>> 1/2

试试“%” 取余

>>> 6 % 3

>>> 6 / 3

>>> 6 % 3

>>> 7 / 3

>>> 7 % 3

>>> 13 % 9
>>> 0.75 % 0.5
0.25

再试试“ ** ” 幂运算

>>> 2*2*2

>>> 2**3

>>> 2**6

>>> -3**2
-9
>>> (-3)**2

2的3次方可以用乘方符(**)表示

3 执行一个用户输入操作

提醒用户输入:用户和密码                                                x=input("name:")    y=input("pwd:") 

获取用户名和密码,检测:用户名=root 密码=root                 if x=="root" and y =="root":
正确:登录成功                                                                print("success")
错误:登陆失败                                                              else :  

                              pritn("error")

4变量名

       变量名智能字母,数字,下划线 并且不能以数字开头,也不能使用python关键字取名.    

5 条件语句

   

缩进用4个空格
a.
n1 = input('>>>')

if "alex" == "alex":
n2 = input('>>>')
if n2 == "确认":
print('alex SB')
else:
print('alex DB')
else:
print('error')


注意:
n1 = "alex" 赋值
n1 == 'alex' 比较,
b.

if 条件1:
pass
elif 条件2:
pass
elif 条件3:
pass
else:
pass

print('end')

c. 条件1
and or

if n1 == "root" or n2 == "root":
print('OK')
else:
print('OK')

PS:
pass 代指空代码,无意义,仅仅用于表示代码块

6 死循环 

while 1==1:
print('ok')

条件永远成立,输出的无限的ok

7 练习题

1、使用while循环输入 1 2 3 4 5 6     8 9 10

n=1

while n<11:

  if n==7:

    pass:

  else:

    print(n)

  n +=1

print("end")

2 求1-100的所有数的和

n=0 

for i in range(101):

  n=n+i

  print(n)

3、输出 1-100 内的所有奇数

for i in range(100)

  if i%2==1:

    print(i)

  else:

    pass

4、

for i in range(100)

  if i%2==0:

    print(i)

  else:

    pass

5、求1-2+3-4+5 ... 99的所有数的和

n=1

m=0

  while n<100:

    temp=n%2

    if temp==0:

      m=m-n

    else:

      m=m+n

    n +=1

    print(m)

7 用户登陆(三次机会重试)

i=0

while i<3:

  n1=input("输入用户名")

  n2=input("输入密码")

  print(i)

  if n1=="root"and n2=="123"

    print("登陆成功")

  else:

    print("密码错误请重试")

  i +=1    

                                                                        



原文地址:https://www.cnblogs.com/liuyang1987/p/6088041.html