一入Python深似海--print

2024-05-12 17:58
文章标签 python print 一入 深似海

本文主要是介绍一入Python深似海--print,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

先给大家来个干货^~^,学习Python的一个好网站,http://learnpythonthehardway.org/book/


经典例子

下面是几个老经典的例子喽,刚接触Python的可以敲一敲,看看结果喽!

my_name='Zed A. Shaw'
my_age=35#not a lie
my_height=74#inches
my_weight=180#1bs
my_eyes='Blue'
my_teeth='white'
my_hair='Brown'
print my_name
print "Let's talk about %r" %my_name
print "He's %d inches tall." %my_height
print "He's %d pounds heavy." %my_weight
print "Actually that's not too heavy."
print "He's got %s eyes and %s hair."%(my_eyes,my_hair)
print "His teeth are usually %s depending on the coffee."%my_teeth
#this line is tricky,try to get it exactly right
print "If I add %d, %d,and %r I get %d."%(my_age,my_height,my_weight,my_age+my_height+my_weight)

x="There are %d types of people."%10
binary="binary"
do_not="don't"
y="Those who know %s and those who %s"%(binary,do_not)
print x
print yprint "I said: %r."%x
print "I also said: '%s'."%yhilarious=False
joke_evaluation="Isn't that joke so funny?! %r"
print joke_evaluation % hilariousw="This is the left side of..."
e="a string with a right side"print w+e

print "Mary had a little lamb."
print "Its fleece was white as %s."%'snow'
print "Its fleece was white as %r."%'snow'
print "And everywhere that Mary went."
print "."*10 #output . 10 timesend1="c"
end2="h"
end3="e"
end4="e"
end5="s"
end6="e"
#watch that comma at the end.
print end1+end2+end3,
print end4+end5+end6

formatter="%r %r %r %r"print formatter %(1,2,3,4)
print formatter %("one","two","three","four")
print formatter %(True,False,False,True)
print formatter %(formatter,formatter,formatter,formatter)
print formatter %("I had this thing.","That you could type up right.","But it didn't sing.","So I said goodnight.")

days="Mon Tue Wed Thu Fri Sat Sun"
months="Jan\nFeb\nMar\nApr\nMay\nJun\nAug"print "Here are the days:",days
print "Here are the months:",months
print "Here are the months: %r"%months
#That's how %r formatting works; 
#it prints it the way you wrote it (or close to it). It's the "raw" format for debugging.
print """
There's something going on here.
whith the three double-quotes.
we'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"""

tabby_cat="\tI'm stabbed in."
persian_cat="I'm split\non a line."
backslash_cat="I'm \\ a \\ cat."fat_fat="""
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""print tabby_cat
print persian_cat
print backslash_cat
print fat_fat


%r与%s的区别

我的总结是这么个点:  %r与%s的区别

That's how %r formatting works; it prints it the way you wrote it (or close to it). It's the "raw" format for debugging.

Always remember this: %r is for debugging, %s is for displaying.


这篇关于一入Python深似海--print的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/983339

相关文章

python: 多模块(.py)中全局变量的导入

文章目录 global关键字可变类型和不可变类型数据的内存地址单模块(单个py文件)的全局变量示例总结 多模块(多个py文件)的全局变量from x import x导入全局变量示例 import x导入全局变量示例 总结 global关键字 global 的作用范围是模块(.py)级别: 当你在一个模块(文件)中使用 global 声明变量时,这个变量只在该模块的全局命名空

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

nudepy,一个有趣的 Python 库!

更多资料获取 📚 个人网站:ipengtao.com 大家好,今天为大家分享一个有趣的 Python 库 - nudepy。 Github地址:https://github.com/hhatto/nude.py 在图像处理和计算机视觉应用中,检测图像中的不适当内容(例如裸露图像)是一个重要的任务。nudepy 是一个基于 Python 的库,专门用于检测图像中的不适当内容。该

pip-tools:打造可重复、可控的 Python 开发环境,解决依赖关系,让代码更稳定

在 Python 开发中,管理依赖关系是一项繁琐且容易出错的任务。手动更新依赖版本、处理冲突、确保一致性等等,都可能让开发者感到头疼。而 pip-tools 为开发者提供了一套稳定可靠的解决方案。 什么是 pip-tools? pip-tools 是一组命令行工具,旨在简化 Python 依赖关系的管理,确保项目环境的稳定性和可重复性。它主要包含两个核心工具:pip-compile 和 pip

HTML提交表单给python

python 代码 from flask import Flask, request, render_template, redirect, url_forapp = Flask(__name__)@app.route('/')def form():# 渲染表单页面return render_template('./index.html')@app.route('/submit_form',

Python QT实现A-star寻路算法

目录 1、界面使用方法 2、注意事项 3、补充说明 用Qt5搭建一个图形化测试寻路算法的测试环境。 1、界面使用方法 设定起点: 鼠标左键双击,设定红色的起点。左键双击设定起点,用红色标记。 设定终点: 鼠标右键双击,设定蓝色的终点。右键双击设定终点,用蓝色标记。 设置障碍点: 鼠标左键或者右键按着不放,拖动可以设置黑色的障碍点。按住左键或右键并拖动,设置一系列黑色障碍点

Python:豆瓣电影商业数据分析-爬取全数据【附带爬虫豆瓣,数据处理过程,数据分析,可视化,以及完整PPT报告】

**爬取豆瓣电影信息,分析近年电影行业的发展情况** 本文是完整的数据分析展现,代码有完整版,包含豆瓣电影爬取的具体方式【附带爬虫豆瓣,数据处理过程,数据分析,可视化,以及完整PPT报告】   最近MBA在学习《商业数据分析》,大实训作业给了数据要进行数据分析,所以先拿豆瓣电影练练手,网络上爬取豆瓣电影TOP250较多,但对于豆瓣电影全数据的爬取教程很少,所以我自己做一版。 目

【Python报错已解决】AttributeError: ‘list‘ object has no attribute ‘text‘

🎬 鸽芷咕:个人主页  🔥 个人专栏: 《C++干货基地》《粉丝福利》 ⛺️生活的理想,就是为了理想的生活! 文章目录 前言一、问题描述1.1 报错示例1.2 报错分析1.3 解决思路 二、解决方法2.1 方法一:检查属性名2.2 步骤二:访问列表元素的属性 三、其他解决方法四、总结 前言 在Python编程中,属性错误(At