ICode9

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

python_crash_course(2.1-2.6)

2022-07-27 00:31:35  阅读:184  来源: 互联网

标签:Python name python course test print 2.1 message string


2.1 测试环境

print ("Hello Python World!")

2.2 变量

message = "Hello Python world!"
print(message)

message = "Hello python Crash Course world!"
print(message)

2.2.1 变量的命名和使用

变量的命名包含: 字母 数值 下划线

正确的命名方式:

message = "xxxxxx"
_message = "xxxxxx"
greeting_message = "xxxxxx"

错误的命名方式:

1_message = "xxxxxx"

0 = "xxxxxx"

greeting message = "xxxxxx"

print = "xxxxxx" #不可以使用python的关键字和函数名

变量的命名应是简短有描述性的

student_name = "xxq"

s_n = "xxq"

2.2.2 使用变量时避免命名错误

message = "Hello Python world!"
print(mesage)

Traceback (most recent call last):

File "e:\project\python\2.1.py", line 38, in

print(mesage)

NameError: name 'mesage' is not defined. Did you mean: 'message'?

2.2.3 变量是盒子

student_name = "xxq"
print (student_name)

2.3 字符串

用引号括起来的都是字符串

string = "This is a string."
print (string)
string = 'This is also a string.'
print (string)

string = 'I told my friend,"Python is my favorite language!"'
print (string)

string = "The language 'Python is named after Monty Python,not the snake.'"
print(string)

string = "One of Python's strengths is its diverse and supportive community."
print(string )

2.3.1 使用方法修改字符串的大小写

name = "ada lovelace"

print(name.title()) # .title() 首字母大写
print(name.upper()) # .upper() 所有字母大写

name1 = "ADA LOVELACE"

print(name1.lower()) # .lower() 所有字母小写

2.3.2 在字符串中使用变量

first_name = "ada"
last_name = "lovelace"

full_name = f"{first_name} {last_name}"
print(full_name)

f""方法出自3.6,python较前版本则使用 format()

将花括号内的变量替换为其值

2.3.3 使用制表符或换行符来添加空白

制表符

test = "Python"
print (test)

test = "\tPython"
print (test)

换行符

test = "Python"
print (test)
test = "\nPython"
print (test)

案例

test = "Languages:\nPython\nC\nJavascript"
print (test)

test = "Languages:\n\tPython\n\tC\n\tJavascript"
print (test)

2.3.4 删除空白

# .rstrip() 删除右边的空白

favorite_language = 'Python '
print (favorite_language.rstrip())

# .lstrip() 删除左边的空白

favorite_language = ' Python'
print (favorite_language.lstrip())

# .strip() 删除左右两边的空白

favorite_language = ' Python '
print (favorite_language.strip())

2.3.5 使用字符串避免语法错误

错误案例

message = 'One of Python's strengths is its diverse community'
print (message)

修改后 正确的案例

message = "One of Python's strengths is its diverse community"
print (message)

2.4 数

# 加法

print (2 + 3)

# 减法

print (3- 2)

# 乘法

print (2 * 3)

# 除法

print (3 / 2)

print (4 / 2)

# 乘方运算

print (3 ** 2)

print (3 ** 3)

print (10 ** 6)

# 同一个表达式中使用多种运算

print (2 + 3 * 4)

print ((2 + 3) * 4)

2.4.2 浮点数

print (0.1 + 0.1)

print (0.2 + 0.2)

print (2 * 0.1)

print (2 * 0.2)

print (0.2 + 0.2)

2.4.3 整数和浮点数

print (4 / 2)

print (1 + 2.0)

print (2 * 3.0)

print (3.0 ** 2)

无论是那种运算,只要有操作数是浮点数,Python默认得到的总是浮点数

2.4.4 数中的下划线

这种表示使用于整数和浮点数,但只有 Python 3.6 和更高版本支持

universe_age = 14_000_000_000
print (universe_age)

2.4.5 同时个多个变量赋值

# 普通案例

x = 0
y = 1
z = 2

print (x)
print (y)
print (z)

# 同时个多个变量赋值(按循序)

x,y,z = 0,1,2

print (x)
print (y)
print (z)

2.4.6 常量

常量类似与变量,但其值在程序的整个生命周期内保持不变。Python没有内置的常量类型,

当时Python程序员会使用全大写来指出应将某个变量是为常量,其值应始终不变:

在代码中,要指出应将特定的变量是为常量,可将其字母全部大写

MAX_CONNECTIONS = 5000
MAX_CONNECTIONS = 4000

print(MAX_CONNECTIONS)

2.5 注释

在Python中,注释用井(#)标识。井号后面的内容都会被python解释器忽略

# 向大家问好

print ("Hello Python people!")

2.6 Python之禅

python代码的指导原则

import this

标签:Python,name,python,course,test,print,2.1,message,string
来源: https://www.cnblogs.com/wm-plengong/p/16523207.html

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

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

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

ICode9版权所有