美文网首页
python 记录

python 记录

作者: 幸宇 | 来源:发表于2020-12-16 17:50 被阅读0次

print("hello python interpreter")

message = "hello python world"
print(message)
message = "hello zx"
print(message)
print(message)

message = 'i told my friend,"python is my favorite language"'
print(message)

区分大小写

name = "ada lovelace"
print(name.title())
print(name.upper())
print(name.lower())

合并拼接字符串

first_name = 'ada'
last_name = 'lovelace'
full_name=first_name+' '+last_name
print(full_name)
print("hell, "+full_name.title()+'!')

空白泛指非打印字符,如空格、制表符和换行符

添加制表符

print("python")
print("\tpython")

添加换行符

print("languages:\npython\nC\nJavascript")

print("languages:\n\tpython\n\tC\n\tJavascript")

去掉空白字符

favorite_languages = ' python '
print(favorite_languages+'sss')
print(favorite_languages.rstrip()+'sss')

favorite_languages = favorite_languages.rstrip()
print(favorite_languages)
print(favorite_languages.lstrip())
print(favorite_languages.strip())

print(3+2)

乘方 **

print(3 ** 2)

使用函数str()避免类型错误

age=23
message = "happy"+str(age)+'birthday'
print(message)

bicycles = ['a', 'b', 'c']
print(bicycles[0])

访问最后一位元素

print(bicycles[-1])

修改元素

bicycles[0]='zx'
print(bicycles)

添加元素

bicycles.append('ducati')
print(bicycles)

插入元素

bicycles.insert(1, '888')
print(bicycles)

删除元素

del bicycles[1]
print(bicycles)

pop

motorcyes = ['honda', 'yayay', 'suzuki']
print(motorcyes)

popped_motorcyes = motorcyes.pop()
print(motorcyes)

print(popped_motorcyes)

remove

motorcyes.remove('honda')
print(motorcyes)

sort() 排序

carts = ['bmw', 'audi', 'toyota', 'subaru']
carts.sort()
print(carts)

倒序

carts.sort(reverse=True)
print(carts)

临时排序 sorted()

carts = ['bmw', 'audi', 'toyota', 'subaru']
print("here is the original list:")
print(carts)

print("\nhere is the sorted list:")
print(sorted(carts))
print("\nhere is the original list again:")
print(carts)

carts.reverse()
print(carts)

len

print(len(carts))

遍历

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
print("ssss")

range()函数

for value in range(1, 5):
print(value)

使用range()创建数字列表

number = list(range(1, 6))
print(number)

range()函数指定步长

even_numbers = list(range(2, 11, 3))
print(even_numbers)

使用range()创建需要的数字集

squares = []
for value in range(1, 11):
square = value ** 2
squares.append(square)
print(squares)

对数字列表执行简单的统计计算

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(min(digits))
print(max(digits))
print(sum(digits))

列表解析

arr = [value ** 2 for value in range(1, 11)]
print('列表解析。。。')
print(arr)

切片

players = ['chlars', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])

如果没有指定第一个索引,python将自动从列表头开始

print(players[:4])

如果没有指定结束索引,python将自动从列表结束

print(players[2:])

最后三个的话也可以

print(players[-3:])

遍历切片

for player in players[:3]:
print(player.title())

复制列表

my_foods = ['pizza', 'falafel', 'carrot cake']
firend_foods = my_foods[:]
print("my favorite foods are:")
print(my_foods)
print("\nmy friend's favorite foods are:")
print(firend_foods)

my_foods = ['pizza', 'falafel', 'carrot cake']
firend_foods = my_foods[:]
my_foods.append('cannoli')
firend_foods.append('ice cream')
print("my favorite foods are:")
print(my_foods)
print("\nmy friend's favorite foods are:")
print(firend_foods)

my_foods = ['pizza', 'falafel', 'carrot cake']

这样赋值 行不通

firend_foods = my_foods
my_foods.append('cannoli')
firend_foods.append('ice cream')
print("my favorite foods are:")
print(my_foods)
print("\nmy friend's favorite foods are:")
print(firend_foods)

元组

列表非常适合用于存储在程序运行期间可能变化的数据集。

列表是可以修改的,这对处理网站的用户列表或游戏中的角色列表至关重要。

然而,有时候你需要创建一系列不可修改的元素,元组可以满足这种需求。

Python将不能修改的值称为不可变的,而不可变的列表被称为元组。

dimensions = (200, 100)
print(dimensions[0], dimensions[1])

不可修改

dimensions[0] = 250

遍历元组中所有的值

for value in dimensions:
print(value)

修改元组变量

dimensions = (200,50)
print("original dimensions")
for value in dimensions:
print(value)

dimensions = (400,100)
print("\nmodified dimensions")
for value in dimensions:
print(value)

print("if 语句-------------------------")

if语句

cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())

检测是否相等 python中检测是否相等是检测大小写的

print('Audi'=='audi')
print('Audi'.lower()=='audi')

检测是否不相等

requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
print("hello the anchovies")

使用and检查多个条件

age_0 = 22
age_1 = 18
print(age_0 >= 21 and age_1 >=21)

或者

print((age_0 >= 21) and (age_1 >=21))

使用or检查多个条件

print(age_0 >= 21 or age_1 <=21)

检查特定值是否包含在列表中

requested_toppings = ['a', 'b', 'c']
print('a' in requested_toppings)

检测特定值是否不包含在列表中

banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
print("特定值不在列表中。。。")

字典

在Python中,字典是一系列键—值对。每个键都与一个值相关联,你可以使用键来访问与之相关联的值。

与键相关联的值可以是数字、字符串、列表乃至字典。事实上,可将任何Python对象用作字典中的值。

alien_0 = {'color':'green', 'poiner':5}
print(alien_0['color'])
print(alien_0['poiner'])

添加键值对

alien_0['x_position'] = 2
alien_0['y_position'] = 24
print(alien_0)

创建空字典

alien_0 = {}

删除键值对

del alien_0['color']

由类似对象组成字典

favorite_languages = {
'jen':'python',
'color':'green',
'age':'66'
}

遍历字典

for key, value in favorite_languages.items():
print("\nkey\t"+key)
print("\nvalue\t"+value)

for name in favorite_languages.keys():
print(name.title())

按顺序遍历

for name in sorted(favorite_languages.keys()):
print(name.title()+"thank you ")

遍历所有的值

for value in favorite_languages.values():
print(value)

踢出重复值

for value in set(favorite_languages.values()):
print(value)

message = input("tel me ....")
print(message)
name = input("please enter your name:")
print("hello"+message)

prompt = "if you tell us who you are,we can personalize the message you see"
prompt+="\nWhat is your first name?"
name = input(prompt)
print("\nHello, "+name+"!")

相关文章

  • Python笔记——记录程序运行时间

    记录一个python中的小模块 在python程序中如何记录程序运行的时间呢? 可以借助python中的time模...

  • Python简介

    自学整理记录,大神见笑 目标 Python起源 为什么要用Python Python的特点 Python的优缺点 ...

  • python3源码安装

    源码安装记录 #下载python源码包 $ wget http://python.org/ftp/python/3...

  • python中的主要数据类型及相关操作

    黑马python学习记录b站麦叔编程学习记录 菜鸟一枚,不喜勿喷 python中主要数据类型 python中的主要...

  • 【Python】抓取网页信息

    最近开始利用python实操抓取网页链接内容,记录学习过程,熟悉python操作。 版本:Python 3.6.0...

  • Python API 树

    Python API Guides (仅记录日常用到的api) Python API Guides Tensor...

  • Python3-mysql

    此文章记录一下python对mysql的一些操作方式、方法。 *python如何连接mysql ? python ...

  • Python学习笔记

    这是我学习python的过程中记录的一些东西,记录了一些python的特性以及python工程师面试过程中经常被问...

  • 2020-04-03

    今天正式开始记录,我的python学习旅程! Life is short, you need Python! 人生...

  • 无标题文章

    在此记录下Python的某段小程序,以此证明python的简单实用性 < !/usr/bin/python Fil...

网友评论

      本文标题:python 记录

      本文链接:https://www.haomeiwen.com/subject/sylogktx.html