博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python基本语法
阅读量:6653 次
发布时间:2019-06-25

本文共 1796 字,大约阅读时间需要 5 分钟。

获取变量类型:

print type(42)print type(4.2)print type('spam')if(type(1) == type(2.0)):  print "equal"else:  print "not equal"

 

list:

start_list = [5, 888, 1, 2, 4]square_list = [] print len(start_list) # number of items start_list.remove(888) # remove an item by value
del start_list[0] # remove an item by index
for num in start_list:  square_list.append(num*num) # append to tailsquare_list.sort() # alphabetic orderprint square_list

 

dictionary:

zoo_animals = { 'Unicorn' : 'Cotton Candy House','Sloth' : 'Rainforest Exhibit','Bengal Tiger' : 'Jungle House','Atlantic Puffin' : 'Arctic Exhibit','Rockhopper Penguin' : 'Arctic Exhibit'}idel zoo_animals['Unicorn'] # remove an itemzoo_animals['Rockhopper Penguin'] = 'anything'  # change an itemprint zoo_animalsfor one_key  in zoo_animals:    print zoo_animals[one_key]

 

# dictionary can hold many different types of valuesinventory = {    'gold' : 500,    'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key    'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']}# add a key called 'packet', and set the value of this key to be a listinventory['pocket'] = ['seashell', 'strange berry', 'lint']# sort the listinventory['backpack'].sort()# remove an item from the list stored under the 'backpack' keyinventory['backpack'].remove('dagger')# add 50 to the number stored under the 'gold' keyinventory['gold'] = inventory['gold']+50

 

 引入模块:

(1)  import math  // recommended wayprint math.sqrt(4)(2) from math import sqrt // recommended wayprint sqrt(4)(3) from math import * // NOT recommended wayprint sqrt(4)

函数定义和调用:

def cube(number):    return number**3def by_three(number):    if(number%3 == 0):        return cube(number)    else:        return Falseprint by_three(3)

 

转载于:https://www.cnblogs.com/ruanchao/p/4866294.html

你可能感兴趣的文章
[算法] 冒泡排序
查看>>
[LintCode] Two Sum 两数之和
查看>>
Linux高并发机制——epoll模型
查看>>
SQL注入与Java
查看>>
那些强悍的PHP一句话后门
查看>>
C# 文件下载 : WebClient
查看>>
JavaScript Ajax之美
查看>>
oracle linux 启动
查看>>
C# 一个多层循环中,break一次能跳出几个循环?
查看>>
脚本大全
查看>>
PostgreSQL 允许远程访问设置方法
查看>>
eclipse中project->clean的作用是什么
查看>>
【转】bash 参数展开(Parameter Expansion)
查看>>
CSS之div和span标签
查看>>
攻入Javascript,究竟什么是AJAX
查看>>
ASP.NET MVC:通过FileResult向浏览器发送文件
查看>>
Mac下关闭Sublime Text 3的更新检查
查看>>
Linux 块I/O子系统
查看>>
$ -----JavaScript 中美元符号 $ 的作用
查看>>
2017年网站安全狗绕过WebShell上传拦截的新姿势
查看>>