python-基础


输出print()

print()在括号中加上字符串,就可以向屏幕上输出指定的文字。比如输出'hello world',用代码实现如下:

print('hello world')
hello world

print()函数也可以接受多个字符串,用逗号“,”隔开,就可以连成一串输出:

print('hello','world')
hello world

print()会依次打印每个字符串,遇到逗号“,”会输出一个空格

print()可以计算

print(200+100)
300

print()输出变量不需要单双引号

n = 'hello world'
print(n)
hello world

如果不是变量,输出时没有引号会提示变量未定义

print(name)
Traceback (most recent call last):
  File "E:/python/day1/zidian.py", line 47, in <module>
    print(name)
NameError: name 'name' is not defined

print()与字符串格式操作符(%)结合使用,可实现字符串替换功能,%s代表字符,%d代表数字,%f为浮点,后面需要%,不需要逗号。

print('%s is number %d'%('Python',10))
Python is number 10

注释

python也是使用#注释,从#开始到一行结束的内容都是注释

print('hello world') #my first pyrhon
hello world

也可以使用一对三个单引号(''')或双引号(""")来注释,引号中的内容都不会输出,多用于多行注释

'''
this is a doc string
not print
'''

也可以用作多行的变量

doc = '''
this is a doc string
not print
'''
print(doc)
this is a doc string
not print

值得注意的一件事是,在一个字符串中,行末的单独一个反斜杠表示字符串在下一行继续,而不是开始一个新的行。

doc = '''
this is a doc string \
not print
'''
print(doc)

变量

标识符的第一个字符必须是字母表中的字母(大写或小写)或者一个下划线(‘ _ ’)。

标识符名称的其他部分可以由字母(大写或小写)、下划线(‘ _ ’)或数字(0-9)组成。

标识符名称是对大小写敏感的。例如,myname和myName不是一个标识符。注意前者中的小写n和后者中的大写N。

有效 标识符名称的例子有i、__my_name、name_23和a1b2_c3。

无效标识符名称的例子有2things、this is spaced out和my-name。

如果只有 name = hello 则无效,会认为hello也为变量,处理方法hello =’world’定义一个变量,或者name = ‘hello’ 值加引号。

常量用大写表示,PIE = 3.14

普通变量:

小写字母,单词之间用_分割

this_is_a_var

单词首字母大写

AdStats

ConfigUtil

全局变量名(类变量,在java中相当于static变量):

大写字母,单词之间用_分割

NUMBER

COLOR_WRITE

专有变量:

__开头,__结尾,一般为python的自有变量,不要以这种方式命名

__doc__

__class__

实例变量:

以_开头,其他和普通变量一样

_price   

_instance_var

私有实例变量(外部访问会报错):

以__开头(2个下划线),其他和普通变量一样

__private_var

变量引用

a = 'ABC'
b = a
a = 'XYZ'
print(a)
print(b)
XYZ
ABC

执行a = 'ABC',解释器创建了字符串 'ABC'和变量 a,并把a指向 'ABC'

执行b = a,解释器创建了变量 b,并把b指向 a 指向的字符串'ABC'

执行a = 'XYZ',解释器创建了字符串'XYZ',并把a的指向改为'XYZ',但b并没有更改

合法赋值

x = 1
y = x = x + 1
print(x,y)
2 2

不合法赋值

x = 1
y = (x = x + 1)
print(x,y)
  File "E:/python/day1/day1.py", line 4
    y = (x = x + 1)
           ^
SyntaxError: invalid syntax

多重赋值

x = y = z = 1
print(x,y,z)
1 1 1

多元赋值

x,y,z = 1,3,5
print(x,y,z)
1 3 5

用户输入input

python3中输入使用input,在版本2中使用raw_input

代码:

name = input('please input your name:')
print(name)
please input your name:wxianj
wxianj

输入变量引用1:

name = input("please input your name: ")
age = input("please input your age: ")
job = input("please input your job: ")

print(''' Info of %s

    NAME: %s
    AGE : %s
    JOB : %s
'''%(name,name,age,job))
please input your name: wxianj
please input your age: 20
please input your job: IT
 Info of wxianj

    NAME: wxianj
    AGE : 20
    JOB : IT

引用外部变量 %s,字符串和变量连接%(name,age,job)要按照对应关系,%s 字符串,%d 数字,%f 浮点,字符转换数字 int(),input默认字符串格式,如果使用数字需要转义int,strip()去掉两边空格。

数字

age = input("please input your age: ")

print(''' 
    AGE : %d
'''%(age))

会提示错误

please input your age: 10
Traceback (most recent call last):
  File "E:/python/day1/d.py", line 9, in <module>
    '''%(age))
TypeError: %d format: a number is required, not str

打印类型type

age = input("please input your age: ")
print(type(age))
print(''' 
    AGE : %d
'''%(age))
please input your age: 1
Traceback (most recent call last):
  File "E:/python/day1/d.py", line 7, in <module>
    '''%(age))
TypeError: %d format: a number is required, not str
<class 'str'>

解决方法1,输入时int转换为数字(str转换为字符):

age = int(input("please input your age: "))
print(type(age))
print(''' 
    AGE : %d
'''%(age))
please input your age: 1
<class 'int'>
 
    AGE : 1

方法2:引入时转换:

age = input("please input your age: ")
print(''' 
    AGE : %d
'''%(int(age)))
please input your age: 1
 
    AGE : 1

变量引用2:

name = input("please input your name: ")
age = input("please input your age: ")
job = input("please input your job: ")

print(''' Info of {_name}

    NAME: {_name}
    AGE : {_age}
    JOB : {_job}
'''.format(_name=name,_job=job,_age=age))
please input your name: wxianj
please input your age: 1
please input your job: IT
 Info of wxianj

    NAME: wxianj
    AGE : 1
    JOB : IT

_name只是为了和变量区分,可以改为如name1

打印内容可以定义为变量

name = input("please input your name: ")
age = input("please input your age: ")
job = input("please input your job: ")

info = ''' Info of {_name}

    NAME: {_name}
    AGE : {_age}
    JOB : {_job}
'''.format(_name=name,_job=job,_age=age)

print(info)
please input your name: wxianj
please input your age: 1
please input your job: IT
 Info of wxianj

    NAME: wxianj
    AGE : 1
    JOB : IT

注意:

print(name)这种是打印括号中的变量

print(‘name’)这种是直接打印字符name

name=input('please input your name:')

print('---%s----'% name)不要有逗号,否则会报错

name=input('please input your name:')

passwd=input('please input your passwd:')

print('%s, %s '% (name,passwd))%s需要在引号中,不能单独存在

编程风格

缩进要统一,IndentationError缩进报错,同一级别缩进要一致,建议使用四个空格,不要空格和TAB键混合使用

print('hello')
    print('world')
  File "E:/python/day1/day1.py", line 3
    print('world')
    ^
IndentationError: unexpected indent

IF条件语句

Python 编程中 if 语句用于控制程序的执行,基本形式为(注意判断条件后有冒号:):

if 判断条件:
    执行语句1……
else:
    执行语句2……

如果表达式的值为0或者为布尔值True,则执行语句1,否则执行语句2。任何非0和非空(null)值为true,0 或者 null为false。

a = 3
b = 4

if a > b:
    print(a,'is bigger...')
else:
    print(b,'is bigger...')
4 is bigger...

if 语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)来表示其关系。

当判断条件为多个值是,可以使用以下形式:

if 判断条件1:
    执行语句1……
elif 判断条件2:
    执行语句2……
elif 判断条件3:
    执行语句3……
else:
    执行语句4……

例:

a = 4
b = 4

if a > b:
    print(a,'is bigger...')
elif a < b:
    print(b,'is bigger...')
else:
    print('a = b')
a = b

由于 python 并不支持 switch 语句,所以多个条件判断,只能用 elif 来实现,如果判断需要多个条件需同时判断时,可以使用 or (或),表示两个条件有一个成立时判断条件成功;使用 and (与)时,表示只有两个条件同时成立的情况下,判断条件才成功。

n = 9

if n >=0 and n <= 10:
    print('hello')
hello

当if有多个条件时可使用括号来区分判断的先后顺序,括号中的判断优先执行,此外 and 和 or 的优先级低于>(大于)、<(小于)等判断符号,即大于和小于在没有括号的情况下会比与或要优先判断。

n = 9

if (n >=0 and n <= 5) or (n >= 10 and n <= 15):
    print('hello')
else:
    print('undefine')
undefine

简单的语句组你也可以在同一行的位置上使用if条件判断语句,如下实例:

if 3 > 1:print('hello')
hello

一个例子

name='wxianj'
passwd='123456'

_name=input('please input your name:')
_passwd=input('please input your passwd:')

if name==_name and passwd==_passwd:
    print('welcome {name1}'.format(name1=name))
else:
    print('username or passwd wrong!!!')

输错

please input your name:w
please input your passwd:1
username or passwd wrong!!!

正确

please input your name:wxianj
please input your passwd:123456
welcome wxianj

range

range从0开始,不包含最后

a = range(10)
print(a)
for i in range(5):
    print(i)
range(0, 10)
0
1
2
3
4

从1到10,不包含10,步长为2

for i in range(1,10,2):
    print(i)
1
3
5
7
9

len

统计长度

a='1234567'
print(len(a))

长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn