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

python第一天

时间:2016-05-14 01:12:05      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:程序   字符串   python   

python3 

  Python的3.0版本,常被称为Python 3000,或简称Py3k。相对于Python的早期版本,这是一个较大的升级。为了不带入过多的累赘,Python 3.0在设计的时候没有考虑向下兼容。
  
第一个Python3.x程序

  #!/usr/bin/python3
  print("Hello, World!")
  
  执行命令:python hello.py
  输出结果:Hello, World!
  
Python3 基本数据类型
   
    Python中的变量不需要声明。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。
    在Python中,变量就是变量,它没有类型,我们所说的"类型"是变量所指的内存中对象的类型。
    Python 3中有六个标准的数据类型:
    Numbers(数字)
    String(字符串)
    List(列表)
    Tuple(元组)
    Sets(集合)
    Dictionaries(字典)

Numbers(数字)
    Python 3支持int、float、bool、complex(复数)。
    数值类型的赋值和计算都是很直观的,就像大多数语言一样。内置的type()函数可以用来查询变量所指的对象类型。
    
String(字符串)
    Python中的字符串str用单引号(‘ ‘)或双引号(" ")括起来,同时使用反斜杠(\)转义特殊字符。
    
List(列表)
    List(列表) 是 Python 中使用最频繁的数据类型。
    列表是写在方括号之间、用逗号分隔开的元素列表。列表中元素的类型可以不相同
    
Python 条件控制

if 语句

    Python中if语句的一般形式如下所示:
        if condition_1:
            statement_block_1
        elif condition_2:
            statement_block_2
        else:
            statement_block_3
            
    如果 "condition_1" 为 True 将执行 "statement_block_1" 块语句,如果 "condition_1" 为Fa    lse,将判断 "condition_2",如果"condition_2" 为 True 将执行 "statement_block_2" 块语    句,如果 "condition_2" 为False,将执行"statement_block_3"块语句。
    
例:

    if username == user:
        print("right name")
    if password == passwd:
        print("welcome to login")
    else:
        print("password error")
    else:
        print ("username error")
        
while 循环
    以下实例使用了 while 来计算 1 到 100 的总和:
    
    #!/usr/bin/env python3
 
    n = 100
 
    sum = 0
    counter = 1
    while counter <= n:
        sum = sum + counter
        counter += 1
 
    print("Sum of 1 until %d: %d" % (n,sum))

 

 执行结果:Sum of 1 until 100: 5000



for语句
    Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
    for循环的一般格式如下:
         
    for <variable> in <sequence>:
        <statements>
    else:
        <statements>


python第一天

标签:程序   字符串   python   

原文地址:http://7069044.blog.51cto.com/7059044/1773206

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