python快速入门day2

卧槽卧槽时不我待啊

一晃这么多天过去了太可怕了

=====================================

section: Basics

 
print('hello world')

# string: '' or "", immutable
# multi-line string: ''' or """
# escape sequences (\)
print("what's your name")
print("what\'s your name")
print("what\\s your name")

print('first line \nsecond line')
print('abc  \tdef')
print("first sentence.\
      second sentence.")
print('\n')

# index first position at 0
# string concatenation: +

age = 20
name = 'Swaroop'

print('{0} was {1} years old when he wrote this book'.format(name,age))
print('{} is {} years old'.format(name, age))
print(name + ' is ' + str(age) + ' years old')

# decimal (.) precision of 3 for float '0.333'
print('{0:0.3f}'.format(1.0/3))
print('{:0.3f}'.format(1.0/3))

# fill with underscore (_) with the text centered
# (^) to 11 width '___hello___'
print('{0:_^11}'.format('hello'))
print('{:_^11}'.format('hello'))
print('{0:-^11}'.format('hello'))
print('{0:_^14}'.format('hello'))

# keyword-based
print('{name} wrote {book}'.format(name='swaroop', book="a byte of python"))

# print ends with invisible new line character (\n)
print('a',end='')
print('b',end='')
print('c',end=' ')
print('d\ne')

#raw string: prefix r or R
print("newlines are indicated by \n")
print(r"newlines are indicated by \n")
print('\n')

# identifier naming
''' 
1. start with alphabet or underscore _
2. the rest can consist of letters, _ or digits(0-9)
3. case-sensitive
'''

# example: using variables and literal constants
i=5
print(i)
i=i+1
print(i)
s='''this is a multiline string.
 this is second line'''
print(s)

# end of logical statement ;
# However, it is recommended that you never use ;, and stick to writing a max of single logical line on each single physical line
i=5
print(i)
i=5;print(i)

s = 'This is a string. \
    This continues the string.'

print(s)
i=\
6
print(i)

#indentation:
'''
using 4 spaces is recommended
must use a consistent number of spaces for indentation 
'''

section: Operators and Expressions

# operators:
'''
+: add or concatenation
-: subtraction or negative 
*: multiply or repeat strings, 'la'*3 gives lalala
**: power/exponentiation, 3**4 gives 3^4
/: divide
//: divide and floor 
%: modulo, returns the remainder 
all comparison operators return True or False
comparison can be chained: 3<6<8 gives True 
==: equal
!=: not equal
not, and, or: boolean 

shortcut: a = 2; a = a*3 can be written as a = 2;a *= 3
'''

l = 5
w = 2
area = l * w
print('area is', area)
print('perimeter is', 2*(l+w))



 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值