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

Python 基础一

时间:2016-04-08 21:32:41      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

1.Python  Helloworld!

#!/usr/bin/env python
print "Hello World!!!"

2.Python 基本变量命名

合法变量:

Name=‘freeman‘

NameInfo =‘freeman‘

不合法变量:

name-info = ‘freeman‘

@name =‘freeman‘

总结:变量在命名时不能使用特殊字符和运算符作为变量,在命名变量时可以使用错分法比如 NameInfo

3.Python 判断语句

#!/usr/bin/env python
print "hello1"
if 1 > 10 :
   print "hello2"
   if 2 == 4 :
      print "hello3"
print "hello 4"

语法:

if 表达式:

    代码逻辑块

elif 表达式:

    代码逻辑块

else:

    代码逻辑块

总结:由于 Python 没有程序关闭符,是因为它使用了代码缩进来表达一段代码或者逻辑

3.Python 运算表达式

==  等于

>=  大于等于

<=  小于等于

 

4.Python 循环语句

#!/usr/bin/env python
while True:
  print "hh"
  target=False
  while True:
    print "gg"
    target=True
    break
  if target:
     break

#!/usr/bin/env python
import random
import tab
suiji=random.randrange(4)

#num=raw_input(‘Please you number:‘)
while True:
 num=raw_input(‘Please you number:‘)
 if int(num) == suiji :
  print "OK"
  print suiji
  break
 elif num > suiji :
  print "xiao xiao xiao"
  print suiji
  continue
 elif num < suiji:
  print "dadada"
  print suiji
  continue

5.Python 基本的数据类型

Python内置的几种数据类型有:列表,元组,字典,字符串,数字,符点型等

6.Python打开文件的几种模式

r 读模式

w 写模式

a  追加模式

a+  w+  r+

7.Python语法糖(IF True及三元运算符)

def is_freeman(name):
res = True if name.lower() == "freeman" else False
return res
print is_freeman("freeman")
print is_freeman("free")

#lambda
a = lambda x ,y : x > y
print a(5,3)

8.Python利用以上方法进行实践

 #!/usr/bin/env python
user=raw_input("Please you name:")
for i in range(3):
  passwd=raw_input("Please you passwd:")
  if user ==‘jzclvn‘ and passwd ==‘redhat‘:
     print "Wlecome"
     break
  else:
     print "bye"
else:
 print "lock user"

Python 基础一

标签:

原文地址:http://www.cnblogs.com/yjz1/p/5369831.html

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