python学习记录(1)

本文介绍了Python的基础知识,包括Python 3.5.1在Linux环境下的安装、编辑与运行方法,详解了print输出函数、input输入函数的用法,以及变量赋值、布尔变量、数组等基本概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#2016-01-12
#(1)python3.5.1在Linux环境的安装;python的编辑编译与运行;

#(2)输出print;输入input;

#(3)变量与赋值、bool变量、数组


一、第一个python程序
[root@localhost pywork]# cat hello.py 
print ('hello,world!')
print ('This is my first python code')
print (309+418)
运行结果:
[root@localhost pywork]# python hello.py 
hello,world!
This is my first python code
727


二、print()输出
[root@localhost pywork]# cat print.py 
print ('hello,world')

print ('The quick brown fox','jumps over')
#print()中的“ ,“ 将两个多个输出隔开,自身显示为空格


print ('309+418 =',309+418)


print('1024*768 = ',1024*768)


print('I\'m ok.')
# 反斜杠\ :转义


print ('I\'m learning\nPython.')


print ('\\\n\\')
# \本身也需要转义


print('\\\t\\')
print(r'\\\t\\')
# r'  ' 用来表示:内部字符不转换


#换行的输出方法?

运行结果:
[root@localhost pywork]# python print.py 
hello,world
The quick brown fox jumps over
309+418 = 727
1024*768 =  786432
I'm ok.
I'm learning
Python.
\
\
\ \
\\\t\\


三、input( )输入

[root@localhost pywork]# cat input.py 

name = input('hello,what\'s your name?\n')
print('hello',name)

运行结果:
[root@localhost pywork]# python input.py 
hello,what's your name?
Jelly
hello Jelly


四、bool型变量
[root@localhost pywork]# cat bool.py 
#bool值只有两种:True和False
b1=True
b2=False
b3=3>2
b4=3>5
print(b1,b2,b3,b4)


#bool值可以进行 and、or、not运算
a1=True and False
a2=True or  False
a3=not True
print(a1,a2,a3)


#none 是一个特殊的空值

运行结果:
[root@localhost pywork]# python bool.py 
True False True False
False True False


五、python中的变量

[root@localhost pywork]# cat variable.py 

#变量的赋值
a=123
print(a)
a='abc'
print(a)


x=10
x=x+2
print(x)


#理解一个变量赋值的过程
# 例如:a='abc'
#       (1)内存中创建了abc字符串
#       (2)内存中创建了a,并将它指向'abc'
a='abc'
b=a
a='xyz'
print(b)


#变量的运算
x1=10/3
x2=10.0/3
x3=10%3
print(x1)
print(x2)
print(x3)

运行结果:
[root@localhost pywork]# python variable.py 
123
abc
12
abc
3.3333333333333335
3.3333333333333335

1

                            



六、python中的序列(数组)

[root@localhost pywork]# cat sequence.py 

#sequence
gg1=['Taeyeon Kim',26]
gg2=['Jessica Jung',26]
datebase=[gg1,gg2]
print(datebase)


#数组中下标从0开始,-1表示最后一个元素
greeting='hello'
print(greeting[0])
print(greeting[-1])


#不赋值而直接使用
fourth=input(r'Year: ')[3]
print(fourth)


#test2-1 
#输出给定格式的日期
#示例:
#   Year:1974
#   Month(1-12):8
#   Day(1-31):16
#输出结果:August 16th,1974


   #说明:英语日期中的ending:
   #1、21、31(1结尾的)后缀为21st
   #2、22(2结尾的)后缀为22nd
   #3、23(3结尾的)后缀为23rd
   #11、12、13和其他均为th


months=['January','February','March','April','May','June','July','August','September','October','November','December']

endings=['st','nd','rd']+17*['th']+['st','nd','rd']+7*['th']+['st']

year=input(r'Year: ')
month=input(r'Month(1-12): ')
day=input(r'Day(1-31): ')

month_name=months[int(month)-1]      
day_name=day+endings[int(day)-1]     
print(month_name+' '+day_name+','+year)


运行结果:
[root@localhost pywork]# python sequence.py 
[['Taeyeon Kim', 26], ['Jessica Jung', 26]]
h
o
Year: 1995
5
Year: 2016
Month(1-12): 3
Day(1-31): 2

March 2nd,2016

#之前没有加int时month_name=months[month-1]、day_name=day+endings[day-1],有报错:

               


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值