码迷,mamicode.com
首页 > 编程语言 > 详细

python初识及变量

时间:2018-08-27 21:28:47      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:代码块   编码   布尔值   单行注释   python解释器   后缀   用户输入   用户   相等   

一、基础

1、文件的后缀是  .py

2、两种执行方式

    python解释器   .py文件路径

    python进入解释器  实时输入并获取执行结果

3、解释器路径

   #!/usr/bin/evn python (Linnux)

   #-*-coding:utf8-*-

 python 2中 只要出现中文,头部必须加编码 (想要加头部,两行必须相邻)

 python3 中无需关注

4、执行一个操作

 python中 单行注释用#,多行注释用   ‘‘‘

input 的用法 永远等待 直到用户输入了值 就将输入的值赋给一个东西

5、变量只能由数字、字母 、下划线组成 但不能以数字开头

  python的一些关键字不能作为变量名

  变量名最好有意义一些

  最好不能和python内置的东西重复

 user-id  python中一般不用userId

6、条件语句

if 条件:

  print()#内部代码块

else:

  print()

直接用tab 代替四个空格

同一个代码块下,缩进的字符必须相等

if 支持嵌套

if、elif (类似于多条件判断)

#pass 什么都不执行 过

7、基本数据类型

数字、字符串、列表、元祖、字典、布尔值

字符串 用印号引起来   " "   """  """   ‘  ‘   ‘‘‘  ‘‘‘ 
加法 乘法

数字   %  #对一个数 取余

       ** 一个数的幂

 判断奇偶性

temp=a%2
if temp==0:
    print(偶数‘)
elseprint(‘奇数’)

 使用while循环输入 123456 89

n=1 
while n<=10
    if n==7:
        pass
    else:
        print(n)
    n+=1
print(end)

求1-100所有数的和

s=0
n=1
while n<101
    s=s+n
    n=n+1
print(s)

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

n=1
s=0
while n<100
    temp=n%2
    if temp==0:
        s=s-n
    else:
        s=s+n
print(s)

输出1-100内所有奇数

n=1
while n<101
    temp=n%2
    if temp!=0:
        print(temp)
    else:
        pass
n=n+1

用户登录 (三次重试)

count=0
while  count <3   
    user=input(">>>")
    psd=input(">>>")
    if  user=="alex" ,psd=="123":
        print("欢迎登陆")
        break
    else:
        print("用户名或密码错误")
    count+=1
    

 

python初识及变量

标签:代码块   编码   布尔值   单行注释   python解释器   后缀   用户输入   用户   相等   

原文地址:https://www.cnblogs.com/lljfighting/p/9541744.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!