python初学

参考http://www.jb51.net/article/926.htm

python特点:

严格的代码格式,用空格来缩进,方法下需要4个空格;

代码的结尾不需要“;”,加了也不会错;

执行程序可以不要main方法,如果有main方法会先执行main方法,但非方法的语句,如果在main方法之上,会在main方法前执行;

python是解释形语言,不会编译为二进制文件,一切皆源码;

代码的执行是自上而下顺序执行,被调用的方法一定要在上方;

 

2 快速入门

2.1 Hello world

    安装完Python之后(我本机的版本是3.6.2),打开IDLE(Python GUI) , 该程序是Python语言解释器,你写的语句能够立即运行.我们写下一句著名的程序语句:

print("Hello")
2.2 国际化支持
# -*- coding: utf8 -*-
print("你好")
2.3 方便易用的计算器

用微软附带的计算器来计数实在太麻烦了.打开Python解释器,直接进行计算:

a=100.0
b=201.1
c=2343.541552156456415641515641456561
d=(a+b+c)
print(d)
print(d/c)

 

2.4 字符串,ASCII和UNICODE

 python和js一样是弱类型的语言;

字符串使用双引号和单引号都可以,python的习惯是单引号;(“”)||(‘’)   ;

字符串是字符数组;

str(s)方法将任意类型数据转换为字符串类型;
  

word = 'abcdefg'
a = word[2]
print("a is: " + a)
b = word[1:3]
print("b is: " + b)
c = word[:2]
print("c is: " + c)
d = word[0:]
print("d is: " + d)
e = word[:2] + word[2:]
print("e is: " + e)
f = word[-1]
print("f is: " + f)
g = word[-4:-2]
print("g is: " + g)
h = word[-2:]
print("h is: " + h)
i = word[:-2]
print("i is: " + i)
l = len(word)
print("Length of word is: " + str(l))
a is: c
b is: bc
c is: ab
d is: abcdefg
e is: abcdefg
f is: g
g is: de
h is: fg
i is: abcde
Length of word is: 7

 

2.5 容器:列表(list),字典(map)

    类似Java里的List,这是一种方便易用的数据类型:

使用方法:

标识符=[0,0,0,0,0]

任意类型,有没有泛型待确认,

取值,前闭后开;

word = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
a = word[2]
print("a is: " + a)
b = word[1:3]
print("b is: ")
print(b)
c = word[:2]
print("c is: ")
print(c)
d = word[0:]
print("d is: ")
print(d)
e = word[:2] + word[2:]
print("e is: ")
print(e)
f = word[-1]
print("f is: ")
print(f)
g = word[-4:-2]
print("g is: ")
print(g)
h = word[-2:]
print("h is: ")
print(h)
i = word[:-2]
print("i is: ")
print(i)
l = len(word)
print("Length of word is: " + str(l))
print("Adds new element")
word.append('h')
print(word)

字典

map = {"k": "v"}
print(map.get("k"))

 2.6 条件和循环语句

if

x = -10
if x < 0:
    x = 0
    print("X小于0")

elif x == 0:
    print("X等于0")

else:
    print("其它")

 

for循环

# 遍历 List
a = ['a', 'b', 'c']
for x in a:
    print (x)

 

 

 

 

 

 

 

待续。。。

posted @ 2017-09-05 10:40  路迢迢  阅读(243)  评论(0编辑  收藏  举报