python代码示例3.8-python3.8 五分钟就可以入门

嗨大成带你五分钟入门 python编程语言

了解Python的语法(第一个hello world)

print('Hello world!') #打印Hello world

字符串格式

name = '嗨大成'

print('Hello, %s' % name)? # Hello,嗨大成

print('Hello,{}'.format(name))? # string formatting? #也是上面的 Hello,嗨大成

定义函数

def add_one(x):

return x + 1

测试

x = 1

y = 2

print( 'x is equal to y: %s' % (x == y))

z = 1

print ('x is equal to z: %s' % (x == z))

names = ['Donald', 'Jake', 'Phil']

words = ['Random', 'Words', 'Dogs']

if names == words:

print ('Names list is equal to words')

else:

print ("Names list isn't equal to words")

new_names = ['Donald', 'Jake', 'Phil']

print ('New names list is equal to names: %s' % (new_names == names))

定义类里面的2个函数

class Talker(object):

def greet(self, name):

print ('Hello, %s!' % name)

def farewell(self, name):

print ('Farewell, %s!' % name)

定义list

dynamic_languages = ['Python', 'Ruby', 'Groovy']

dynamic_languages.append('Lisp')

定义字典

numbered_words = dict()

numbered_words[2] = 'world'

numbered_words[1] = 'Hello'

numbered_words[3] = '!'

循环

while True:

if value == wanted_value: #判断条件

break

else:

pass

定义多行字符串

string = '''This is a string with embedded newlines.

Also known as a tripled-quoted string.

Whitespace at the beginning of lines is included,

so the above line is indented but the others are not.

'''

源代码上拆分长字符串

string = ('This is a single long, long string'

' written over many lines for convenience'

' using implicit concatenation to join each'

' piece into a single string without extra'

' newlines (unless you add them yourself).')

循环

for x in xrange(1, 4):

print ('Hello, new Python user!'

'This is time number %d') % x

列表

l = [x**2 for x in range(4)]

print(l)

# [0, 1, 4, 9]

有条件的

squares = {x**2 for x in [0,2,4] if x < 4}

print(squares)

# {0, 4}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值