跟着我们一起学 Python 30天课程-第2天-数据类型I(Data Types I)
在深入探究编程语言甚至人类语言的实质内容之前,我们需要了解其术语和基本原理,并开始构建基本的心理模型,我们可以在需要时返回并参考。
任何编程语言的构造块都可以主要分为以下几类:
- Terminologies
- Data Types
- Actions (functions)
- Best Practices
今天,我花了一些时间了解Python的一些基本术语,语法,其数据类型及其某些动作,或者在编程术语中被称为函数。
Data Types
简单的数据类型是表示值的一种方式。在我们的物理世界中,我们使用字母,数字,符号作为不同类型的常用值。同样,Python包含以下基本数据类型:
- int (to represent numbers)
- float (to represent decimal numbers)
- str (to represent strings)
- bool (to represent boolean)
- list
- tuple
- set
- dict
- complex (not used very often)
- None (to represent an absence of value)
这些是Python中可用的标准数据类型。为了创建我们自己的自定义类型,使用了类。也可以通过导入外部库或模块来使用专用数据类型。
相反,在JavaScript中,这些是以下可用的原始类型:
- number (for both whole and decimal numbers)
- string
- boolean
- symbol
- bigInt
- null
- undefined Also object as a non-primitive type.
今天,我只是花时间来了解Python的数值和字符串类型。
Numbers
有3种类型的数字数据类型:
- int(存储不限大小的整数)
- float(存储浮点实数值)
- complex(到目前为止,我了解到它不常用,就像JavaScript中的bigInt一样,我只是略过了)。
相反,JavaScript具有两种数字数据类型,Number和BigInt。
该type
函数用于确定值或表达式的类型。(类似于 JavaScript中的typeof
运算符)
num = 100 # variable assignement
print(type(num)) # <class 'int'>
num2 = 99.99
print(type(num2)) # <class 'float>
expression1 = num * 10
print(type(expression1)) # <class 'int'>
expression2 = num + num2
print(type(expression2)) # <class 'float'>
在Python中,变量的分配是通过写一个名称并使用
=
运算符分配一个值来实现的。
在JavaScript中,变量名需要与前面var
,const
或let
关键字。
Math functions 数学函数
有一些内置的数学函数使我们能够轻松地计算各种数学运算。数学函数和常量 -本文档包含所有内置数学函数和常量
print(round(2.1)) # 2
print(round(5.9)) # 6
print(abs(-34)) # 34
将详细探讨数学模块。
Variables 变量
变量存储值。在python中,这些是变量命名约定:
- 变量应以字母(最好是小写)或下划线开头,并可以后跟数字或下划线
- Snake大小写是用多个单词编写变量的常规方式,例如
user_name
(Javascript建议使用cameCasing之类userName
) - 他们区分大小写
- 关键字不应覆盖关键字(Python关键字)
Strings 字符串
Python中的字符串是字符的有序序列(类似于Javascript)。
name = 'Python' # string assignment within single quotes
name2 = "Python" # string assingment within double quotes
name3 = '''This is a a very long string and supports
multiline statements as well''' # string assingment within 3 single quotes
name4 = 'Hello! \"Rockstar Programmer\"' # string with escaped character sequence
print(type(name)) # <class 'str'>
print(type(name2)) # <class 'str'>
print(type(name3)) # <class 'str'>
print(type(name4)) # <class 'str'>
字符串串联
与Javascript类似,可以使用+
运算符来连接字符串。它只是连接或“连接”字符串。
first_name = 'Mike'
last_name = 'Tyson'
print(first_name + ' ' + last_name) # Mike Tyson
类型转换
与Javascript不同,在Javascript中存在隐式类型转换(也称为Type Coercion),如果对不同类型的操作执行操作,Python将抛出错误
user_name = 'John'
age = 40
print(user_name + age) # TypeError: can only concatenate str (not "int") to str
# This would work in Javscript where it would convert the result to string type
在Python中,需要显式转换类型以执行不同类型的操作
user_name = 'John'
age = 40
print(user_name + str(age)) # John40
print(type str(age)) # <class 'str'>
同样,字符串可以转换为数字
lucky_number = 7
lucky_number_stringified = str(7)
lucky_number_extracted = int(lucky_number_stringified)
print(lucky_number_extracted) # 7
今天就这些!仍然简单易用。将继续了解其他字符串操作以及内置的方法和函数以及布尔值和列表类型。
跟着我们一起学 Python 30天课程目录:
- 跟着我们一起学 Python 30天课程-第30天-免费Python资源
- 跟着我们一起学 Python 30天课程-第29天-自动化测试
- 跟着我们一起学 Python 30天课程-第28天-ML和数据科学II
- 跟着我们一起学 Python 30天课程-第27天-ML和数据科学I
- 跟着我们一起学 Python 30天课程-第26天-机器学习基础
- 跟着我们一起学 Python 30天课程-第25天-Web 开发进阶
- 跟着我们一起学 Python 30天课程-第24天-Web开发基础
- 跟着我们一起学 Python 30天课程-第23天-网页爬虫
- 跟着我们一起学 Python 30天课程-第22天-脚本额外功能Scripting Extras
- 跟着我们一起学 Python 30天课程-第21天-脚本编写基础
- 跟着我们一起学 Python 30天课程-第20天-调试和测试
- 跟着我们一起学 Python 30天课程-第19天-正则表达式
- 跟着我们一起学 Python 30天课程-第18天-文件I / O
- 跟着我们一起学 Python 30天课程-第17天-外部模块External Modules
- 跟着我们一起学 Python 30天课程-第16天-模块基础Module Basics
- 跟着我们一起学 Python 30天课程-第15天-生成器Generators
- 跟着我们一起学 Python 30天课程-第14天-错误处理Error Handling
- 跟着我们一起学 Python 30天课程-第13天-Decorators
- 跟着我们一起学 Python 30天课程-第12天-Lambda Expressions & Comprehensions
- 跟着我们一起学 Python 30天课程-第11天-函数编程Functional Programming基础
- 跟着我们一起学 Python 30天课程-第10天-OOP Missing Pieces
- 跟着我们一起学 Python 30天课程-第9天-OOP Pillars
- 跟着我们一起学 Python 30天课程-第8天-OOP基础知识
- 跟着我们一起学 Python 30天课程-第7天-开发环境搭建(Developer Environment)
- 跟着我们一起学 Python 30天课程-第6天-循环II和函数(Loops II & Functions)
- 跟着我们一起学 Python 30天课程-第5天-条件和循环I(Conditions & Loops I)
- 跟着我们一起学 Python 30天课程-第4天-数据类型III(Data Types III)
- 跟着我们一起学 Python 30天课程-第3天-数据类型II(Data Types II)
- 跟着我们一起学 Python 30天课程-第2天-数据类型I(Data Types I)
- 跟着我们一起学 Python 30天课程-第1天-简介
1. 本站资源转自互联网,源码资源分享仅供交流学习,下载后切勿用于商业用途,否则开发者追究责任与本站无关!
2. 本站使用「署名 4.0 国际」创作协议,可自由转载、引用,但需署名原版权作者且注明文章出处
3. 未登录无法下载,登录使用金币下载所有资源。
IT小站 » 跟着我们一起学 Python 30天课程-第2天-数据类型I(Data Types I)
常见问题FAQ
- 没有金币/金币不足 怎么办?
- 本站已开通每日签到送金币,每日签到赠送五枚金币,金币可累积。
- 所有资源普通会员都能下载吗?
- 本站所有资源普通会员都可以下载,需要消耗金币下载的白金会员资源,通过每日签到,即可获取免费金币,金币可累积使用。