zoukankan      html  css  js  c++  java
  • 【第一篇】Python基础

    Python学习

    学习站点:https://www.shiyanlou.com/
    1
    hello world
    code如下:
    1. $ python [15:50:40]
      Python2.7.6(default,Mar222014,22:59:56)
      [GCC 4.8.2] on linux2
      Type"help","copyright","credits"or"license"for more information.
      >>>print('hello world');
      hello world
      >>>
    运行文本编辑器中的python:
     
    首先建立个py结尾的文件,里面代码如下:
    1. 1 print('hello world');
     
    1. 1 shiyanlou:~/ $ python hello.py [15:55:05]
      2 python: can't open file 'hello.py': [Errno 2] No such file or directory
      3 shiyanlou:~/ $ cd desktop [15:55:15]
      4 cd:cd:13: u6ca1u6709u90a3u4e2au6587u4ef6u6216u76eeu5f55: desktop
      5 shiyanlou:~/ $ cd Desktop [15:55:30]
      6 shiyanlou:Desktop/ $ python hello.py [15:55:42]
      7 hello world
      8 shiyanlou:Desktop/ $
     
    脚本:
    修改hello.py中的文件:
    1. 1 #!/usr/bin/env python
      2 print('hello world');
     
    运行:
    chmod 755 hello.py 为修改权限为可执行脚本
    1. 1 shiyanlou:Desktop/ $ chmod 755 hello.py [16:02:17]
      2 shiyanlou:Desktop/ $ ./hello.py [16:02:40]
      3 hello world
      4 shiyanlou:Desktop/ $ [16:02:53]
    基本数据类型
     
    python的变量不需要声明:
    变量a ,值为10,类型integer.数据类型由python决定。
    1. >>> a =100
      >>>print a
      >>>print type(a)
    type为查询变量的类型。
     
    python的常见数据类型:
    整形 a =100
    浮点型 a = 1.3
    真假值 a = True
    字符串 a = ‘hello world’
     
    序列:
    一组有顺序的元素集合
     
    序列包含0到多个元素。
     
    基本数据类型壳子作为序列的元素。
     
    元素还可以是另外一个序列。
     
    序列的分类:
    tuple 定值表
    list 表
    1. s1 =(2,1.3,'love',5.6,9,12,False)# s1是一个tuple
      s2 =[True,5,'smile']# s2是一个list
      print s1,type(s1)
      print s2,type(s2)
    tuple的各个元素不可再变更,而list的各个元素可以再变更。
     
    1. 一个序列作为另一个序列的元素:
    2. 1 s3 =[1,[3,4,5]]
    3. 空序列:
    4. 1 s4 =[]
    元素的引用:
    1. 序列元素的下标从0开始:
    2. 1 print s1[0]
      2 print s2[2]
      3 print s3[1][2]
    3. 由于list的元素可变更,你可以对list的某个元素赋值:
    4. 1 s2[1]=3.0
      2 print s2
    5. 如果你对tuple做这样的操作,会得到错误提示。
    6. 所以,可以看到,序列的引用通过s[int]实现,(int为下标)。
    其他方式的引用:
    基本样式 [下限:上限:步长]
    1. print s1[:5]# 从开始到下标4 (下标5的元素 不包括在内)
      print s1[2:]# 从下标2到最后
      print s1[0:5:2]# 从下标0到下标4 (下标5不包括在内),每隔2取一个元素 (下标为0,2,4的元素)
      print s1[2:0:-1]# 从下标2到下标1
    尾部元素引用:
    1. print s1[-1]# 序列最后一个元素
      print s1[-3]# 序列倒数第三个元素
     
    字符串是元祖:
    1. 1 str ='abcdef'
      2 print str[2:4]
    结果为:
    1. shiyanlou:~/ $ cd Desktop[19:12:53]
      shiyanlou:Desktop/ $ python hello.py [19:12:59]
      cd
    运算:
    包括加减乘除,乘方,求余
    1. 1 print1+9
      2 print1.3-4
      3 print3*4
      4 print4.5/1.5
      5 print3**2
      6 print10%3
    结果为:
    1. shiyanlou:Desktop/ $ python hello.py [19:19:43]
      10
      -2.7
      12
      3.0
      9
      1
    判断 :
    1. 1 print5==6
      2 print5!=6
      3 print3<3,3<=3
      4 print4>5,4>=0
      5 print5in[1,2,3]
    结果为:
    1. shiyanlou:Desktop/ $ python hello.py [19:20:26]
      False
      True
      FalseTrue
      FalseTrue
      False
    逻辑运算:
    包括,and,or ,not(注意python 里面 true和false第一个字母必须大写)
     
    1. 1 print True and False,True and True
      2 print True or False
      3 print not True
     
    结果为:
    1. shiyanlou:Desktop/ $ python hello.py [20:59:38]
      FalseTrue
      True
      False
    if语句:
    1. 1 i =1
      2 if i >0:
      3 print'x>0'
    结果为:
    1. shiyanlou:Desktop/ $ python hello.py [21:05:24]
      x>0
    复杂的if语句:
    elif 相当于java中的else if
    另外逻辑判断没有花括号
    1.  1 i =1
       2 if i >0:
       3 print'positive i'
       4 i = i+1
       5 elif i ==0:
       6 print'i is 0'
       7 i = i*10
       8 else:
       9 print'negative i'
      10 i = i-1
      11 print'new i : ',i
    结果:
    1. shiyanlou:Desktop/ $ python hello.py [21:07:41]
      positive i
      new i :2



  • 相关阅读:
    XJTUOJ13 (数论+FFT)
    ZOJ3956 ZJU2017校赛(dp)
    ZOJ3953 ZJU2017校赛(贪心)
    2014ACM/ICPC亚洲区西安站现场赛 F color(二项式反演)
    LOJ10129
    loj 10127最大数
    noip 选择客栈
    LOJ10121 与众不同
    CQOI 2006 简单题
    校门外的树
  • 原文地址:https://www.cnblogs.com/androidsuperman/p/4621266.html
Copyright © 2011-2022 走看看