Python入门学习(一) 基础语法

目录

1.1.利用Python输出Helloworld

1.2.导入相应的模块或者库

1.3.python的代码语句的规范问题

1.4.python的变量定义

1.5.python变量的基本类型

1.6.python的字符串类型

1.7.python的布尔类型和强制转换

1.8.输入和输出方法

1.9.第一章源代码:


1.1.利用Python输出Helloworld

print("helloworld")
print('helloworld')

1.2.导入相应的模块或者库

#1.2.导入相应的模块或者库
import random #导入一个模块random
ts1=random.randint(1,10)#1-10随机生成一个数
print(ts1)
#这样的写法也是ok的,相当于typedef的用法了
import random as rd
ts2=rd.randint(1,10)
print(ts2)

1.3.python的代码语句的规范问题

1.与c语言不一样,每一行就是一个语句,不要在末尾加上;
2.导入模块不要写在一行,需要每一行分开写比较合适
3."#"表示的是注释符号
4. 4个空格被认为是一个缩进,在‘:’换行后一定要跟一个缩进,不然会出现语法错误。

1.4.python的变量定义

#python的变量的定义非常的随意简单
#python定义中没有字符和字符串的区别
#python定义中不需要int/float/char,编译器会自动的去识别
#1.4.1.变量直接定义
a=1
name='jack'
#1.4.2.变量类型是可以改变的
b=2
print(b)#2
print(type(b))#此时是int
b=float(b)
print(b)#2.0
print(type(b))#此时是float
#1.4.3.多变量同值
a1=b1=c1=0 #这样的定义是被允许的
#而且他们所占的内存地址是相同的
print("a1变量地址:",id(a1)) #a1变量地址: 140726750012168
print("b1变量地址:",id(b1)) #b1变量地址: 140726750012168
print("c1变量地址:",id(c1)) #c1变量地址: 140726750012168

1.5.python变量的基本类型

#1.5.1.整数类型
#二进制 0和1
#八进制 0o开头
#十进制 常用
#十六进制 0x开头
#1.5.2.浮点型类型
a_float=11.2323
print(a_float)
#1.5.3.复数类型
a=1123
b=12331.231
c=1.23+5.6j
print("a=",a)
print("a的类型:",type(a))#<class 'int'>
print("b=",b)
print("b的类型:",type(b))#<class 'float'>
print("c=",c)
print("c的类型:",type(c))#<class 'complex'>
#其实复数是一种特殊的浮点型数

1.6.python的字符串类型

#1.6.1.普通字符串
#1.’‘形式
str1='helloworld'
#2.""形式
str2="helloworld"
#3.3引号形式
str3='''helloworld,helloworld
helloworld,helloworld'''
print(str1)
print(str2)
print(str3)
#1.6.2.unicode字符串
#不常用也不会用
test = u'\u4eba\u751f\u82e6\u77ed\uff0c\u6211\u7528\u0050\u0079\u0074\u0068\u006f\u006e'
print(test)

1.7.python的布尔类型和强制转换

#1.7.1.布尔类型转换
a2=1
b2=0
c2=[]
print(bool(a2))#1-true
print(bool(b2))#0-false
print(bool(c2))#空序列false
if bool(a2):
    print("helloworld1")
else:
    print("helloworld0")
#1.7.2.强制类型转换
ts1=2
print(ts1)
print(type(ts1))#原先是int
ts1=float(ts1)
print(ts1)
print(type(ts1))#强制转化成了float
#1.7.3.进制转化
a=22
print("十进制数:",a)
print("二进制数:",bin(a))#bin()二进制
print("八进制数:",oct(a))#oct()八进制
print("十六进制数:",hex(a))#hex()十六进制

1.8.输入和输出方法

#1.8.1.输入方法
#第一种输入方法
a=input("请输入你的符号:")
print("你输入的符号是:",a)
#还可以这样子进行输入
a=int(input("输入你的数字:"))
print(a,type(a))
#第二种输入方法
#处理多个输入或者行输入的问题
a,b,c=map(int,input().split())
print(a,b,c)
list1=list(map(int,input().split()))
print(list1)
#1.8.2.输出方法
print(1+1)
print(input("请输入对应的数字:"))

1.9.第一章源代码:

#1.1.利用Python输出Helloworld
print("helloworld")
print('helloworld')
#1.2.导入相应的模块或者库
import random #导入一个模块random
ts1=random.randint(1,10)#1-10随机生成一个数
print(ts1)
#这样的写法也是ok的,相当于typedef的用法了
import random as rd
ts2=rd.randint(1,10)
print(ts2)
#1.3.python的代码语句的规范问题
#1.与c语言不一样,每一行就是一个语句,不要在末尾加上;
#2.导入模块不要写在一行,需要每一行分开写比较合适
#3."#"表示的是注释符号
#4. 4个空格被认为是一个缩进,在‘:’换行后一定要跟一个缩进,不然会出现语法错误。

#1.4.python的变量定义
#python的变量的定义非常的随意简单
#python定义中没有字符和字符串的区别
#python定义中不需要int/float/char,编译器会自动的去识别
#1.4.1.变量直接定义
a=1
name='jack'
#1.4.2.变量类型是可以改变的
b=2
print(b)#2
print(type(b))#此时是int
b=float(b)
print(b)#2.0
print(type(b))#此时是float
#1.4.3.多变量同值
a1=b1=c1=0 #这样的定义是被允许的
#而且他们所占的内存地址是相同的
print("a1变量地址:",id(a1)) #a1变量地址: 140726750012168
print("b1变量地址:",id(b1)) #b1变量地址: 140726750012168
print("c1变量地址:",id(c1)) #c1变量地址: 140726750012168
#1.5.python变量的基本类型 
#1.5.1.整数类型
#二进制 0和1
#八进制 0o开头
#十进制 常用
#十六进制 0x开头
#1.5.2.浮点型类型
a_float=11.2323
print(a_float)
#1.5.3.复数类型
a=1123
b=12331.231
c=1.23+5.6j
print("a=",a)
print("a的类型:",type(a))#<class 'int'>
print("b=",b)
print("b的类型:",type(b))#<class 'float'>
print("c=",c)
print("c的类型:",type(c))#<class 'complex'>
#其实复数是一种特殊的浮点型数
#1.6.python的字符串类型
#1.6.1.普通字符串
#1.’‘形式
str1='helloworld'
#2.""形式
str2="helloworld"
#3.3引号形式
str3='''helloworld,helloworld
helloworld,helloworld'''
print(str1)
print(str2)
print(str3)
#1.6.2.unicode字符串
#不常用也不会用
test = u'\u4eba\u751f\u82e6\u77ed\uff0c\u6211\u7528\u0050\u0079\u0074\u0068\u006f\u006e'
print(test)
#1.7.python的布尔类型和强制转换
#1.7.1.布尔类型转换
a2=1
b2=0
c2=[]
print(bool(a2))#1-true
print(bool(b2))#0-false
print(bool(c2))#空序列false
if bool(a2):
    print("helloworld1")
else:
    print("helloworld0")
#1.7.2.强制类型转换
ts1=2
print(ts1)
print(type(ts1))#原先是int
ts1=float(ts1)
print(ts1)
print(type(ts1))#强制转化成了float
#1.7.3.进制转化
a=22
print("十进制数:",a)
print("二进制数:",bin(a))#bin()二进制
print("八进制数:",oct(a))#oct()八进制
print("十六进制数:",hex(a))#hex()十六进制
#1.8.输入和输出方法
#1.8.1.输入方法
#第一种输入方法
a=input("请输入你的符号:")
print("你输入的符号是:",a)
#还可以这样子进行输入
a=int(input("输入你的数字:"))
print(a,type(a))
#第二种输入方法
#处理多个输入或者行输入的问题
a,b,c=map(int,input().split())
print(a,b,c)
list1=list(map(int,input().split()))
print(list1)
#1.8.2.输出方法
print(1+1)
print(input("请输入对应的数字:"))

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

温柔济沧海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值