ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python编程简介

2023-08-10 19:57:36  阅读:306  来源: 互联网

标签:Python 变量 type


Python变量

变量是存储数据值的容器。

创建变量

Python没有声明变量的命令。你只需要等于='符号来分配变量。

Name = "Akinnimi"
Age = 20
print(age) #retuns 20

与其他一些编程语言不同,Python中的变量在设置后可能会发生变化。

x = 200        # x is of type int
x = "Stefan"   # x is now of type str

print(x)       #returns Stefan 
               #x will overwrite x

铸造

您可以通过以下方式更改特定变量的数据类型
类型投射它。

a = str(6)    # a will be '6'
b = int(6)    # b will be 6
c = float(6)  # c will be 6.0

案例敏感

变量名称区分大小写。

这将创建两个变量

a = 4
A = "Sally"

#A will not overwrite a

评论

要在Python中进行注释,#符号被放置在句子的前面。Python读取并忽略该句子。

注释的目的是用于代码文档。它也可以用来解释特定代码在做什么。

#printing out Hello World!
print("Hello, World!")

多行评论

要添加多行注释,您可以为每行插入一个#:

#This is a comment
#written in
#more than just one line

print("Hello, World!")

您可以使用多行字符串。

"""
Writing out my first Python program

Printing out Hello World!

This is going to be fun

"""
print("Hello, World!")

Python将读取代码,但如果文本未分配给变量,并且您已经写了多行注释,则忽略它。

Python数据类型

Python中的数据类型指定了变量可以持有的值类型。

Python有几种内置数据类型:

a.数字类型:

int:整数,例如,5,-10。

浮点:浮点数,例如3.14,-0.5。

b.文本类型:

str:弦乐,例如,“你好,世界!”。

c.布尔类型:

布尔值为真或假

d.序列类型:

列表:有序,可变集合,例如,[1,2,3]。

元组:有序,不可变的集合,例如,(1,2,3)。

e.映射类型:

dict:键值映射,例如,{'name':'Alice','age':30}。

f.设置类型:

集合:独一无二的元素的无序变量集合。

冻结集:不同组件的无序、不可变的分组。

g.二进制类型:

字节:不可更改的字节序列,例如b'编程'。

bytearray:可变的字节序列。

memoryview:提供支持缓冲协议的对象内存的视图。

h.自定义类型:

用户定义的类和对象。

i.特殊类型:

NoneType:表示没有值,用None表示。

Python数字

Python中有三种数字类型:

int

漂浮物

情结

a = 50     # int
b = 3.5    # float
C = 18j    #complex

获取类型

在Python中,使用type()方法来确定任何对象的类型:

print(type(a))   #returns <class 'int'>

print(type(b))   #returns <class 'float'>

print(type(c))   #returns <class 'complex'>

信息

整数是一个整数,正数或负数,没有数字,长度无限。

a = 5
b = 4566677788889
c = -456667

print(type(a))    #returns <class 'int'>

print(type(b))    #returns <class 'int'>

print(type(c))    #returns <class 'int'>

漂浮物

浮点数,通常被称为“浮点数”,是具有一个或多个小数的正数或负数。

a = 1.20
b = 2.5
c = -50.8

print(type(a))    #returns <class 'float'>

print(type(b))    #returns <class 'float'>

print(type(c))    #returns <class 'float'>

情结

复数用“j”表示为虚数部分:

a = 16+5j
b = 3j
c = -10j

print(type(a)) #returns <class 'complex'>

print(type(b))    #returns <class 'complex'>

print(type(c))    #returns <class 'complex'>

类型转换

您可以使用int()和float()方法从一种类型转换为另一种类型:

a = 5    # int
b = 5.3  # float
c = -10j #complex

#convert from int to float:

x = float(a)

#convert from float to int:

y = int(b)

#convert from int to complex:
z = complex(a)

#printing out their values
print(x)  #returns 5.0
print(y) #returns 5
print(z) #returns (-0-10j)

#checking their data type
print(type(x))    #returns <class 'int'>
print(type(y))    #returns <class 'float'>
print(type(z))    #returns <class 'complex'>

Python字符串

在Python中,字符串用单引号或双引号包装。

‘world’ is the same as "world".

使用print()函数,您可以显示字符串文字:

print("Hello")    #returns Hello
print('Hello')    #returns Hello

将字符串分配给变量

a = "Hello"
print(a)    #returns Hello

多行字符串

使用三个引号,您可以为变量分配一个多行字符串:

a = """ Lorem derrym dssaawe ddfrty,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt."""

print(a)

或者三个单引号:

a = ' ' ' Lorem derrym dssaawe ddfrty,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt.' ' '

print(a)

字符串长度

使用len()方法确定字符串的长度。

a = "Hello, World!"
print(len(a))    #returns 13 
#you will notice the whitespace between the Hello World is also counted.

字符串串联

要连接或组合两个字符串,请使用+运算符。

将两个变量与+符号合并在一起

a = "Hello"
b = "World"
c = a + b

print(c)    #returns HelloWorld

#notice there is no space in between the hello world. 
#We will solve that in the next section.

要在Hello World之间添加空格,请添加两个“”

first_name = "Emmanuel"
Last_name = "Akinnimi"
My_name =first_name  + " " + Last_name

print(My_name)    #returns Emmanuel Akinnimi
                  #space is now added

修改字符串

要修改字符串,您必须在它上面调用字符串方法。

strip() #removes whitespace in strings
capitalize()  #Converts the first letter of each word to capital letter 
upper() #converts all the letters in the word to capital case.
lower() #converts all the letters in the word to lowercase.

示例:

Name = “akinnimi stefan”
print(Name.capitalize()) #returns Akinnimi Stefan
print(Name.upper()) #returns AKINNIMI STEFAN
print(Name.lower()) #returns akinnimi stefan

检查字符串

我们可以使用IN方法查看字符串中是否包含特定短语或字符。

favorite= "My favorite food is mash potatoes"
print("food" in Favorite )

Python:逃生字符

使用转义字符将不允许的字符插入字符串。

"""You will get an error when nesting double 
quotes inside another double quote.
"""
txt = “I am going to the “stadium” to watch the football match”
print(txt)   #returns an error

解决方案是在插入非法字符之前使用\反斜杠。

txt = “I am going to the \“stadium\” to watch the football match”
print(txt)

标签:Python,变量,type
来源:

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有