【ps VS python对比】python入门学习(二)

本章依旧是数据类型,包含容器,输入输出等。
上一篇的练习
字符串循环左移
python:

s = str(input())
n = int(input("set number N:"))
head = s[:n]
tail = s[n:]
new_str = tail + head
print(new_str)

powershell

$s = Read-Host("string")
$n = read-host("set number n")
$new_str = $s.Split($s[$n])
$new_str[1]+$new_str[0]

斜边上的高
python:

a = int(input("a:"))
b = int(input("b:"))
c2 =(a**2 + b**2) **0.5
print("%0.2f" % c2)  #方式一
print(round(c2,2)) #方式二

powershell

$a = [int](read-host("a"))
$b = [int](read-host("b"))
$c = [math]::pow($a*$a+$b*$b,0.5)
'{0:n2}' -f $c

最后一个单词
计算字符串最后一个单词的长度,单词以空格隔开。
python:

s = str(input())
arr = s.split(' ')
print(arr[-1])

powershell

$s = Read-Host("string:")
($s.Split(" "))[-1]

计算字符个数
python:

s = str(input())
print(len(s))

powershell

$s = Read-Host("string:")
$s.Length

A+B问题
python:

a = int(input())
b = int(input())
print(a+b)

powershell

$a = Read-Host("a")
$b = Read-Host("b")
$a+$b

数据类型

python:

#容器类型
#列表 vs 元组,都没有类型限制,各个数据叫元素
#列表 ,可增删改重排
使用[], 指明list()
#元组, 不可变序列,处理性能更高 
使用(),指明tuple()
#增列表
append/ insert/ extend 操作
#缩列表
pop 根据序号移除 / remove / clear 清空列表
#reverse/sort
>>> test.sort()
>>> test
[1, 2, 4, 4, 5, 67, 88]
>>> test.reverse()
>>> test
[88, 67, 5, 4, 4, 2, 1]
 # 字典
 #根据标签(字串)来定位数据 key:value 
 #定义
 a={"a":"hello"}
 a["a"] 
 'hello'
 a = dict.fromkeys(("name","age"))   #批量添加项
 a
 {'name': None, 'age': None}
 #增
{'name': None, 'age': None}
>>> a["new"] = "add new"
>>> a
{'name': None, 'age': None, 'new': 'add new'}
# 改
 >>> a.update({"name":"candice"})
>>> a
{'name': 'candice', 'age': None, 'new': 'add new'}
>>> 
#删 del,pop,popitem, clear
>>> a
{'name': 'candice', 'age': None, 'new': 'add new'}
>>> a.pop("age")
>>> a
{'name': 'candice', 'new': 'add new'}
>>> a.popitem()
('new', 'add new')
>>> a
{'name': 'candice'}
>>> a.clear()
>>> a
{}
#查询
>>> a
{'a': 'hello', 'B': ' world'}
>>> a.keys()
dict_keys(['a', 'B'])
>>> a.values()
dict_values(['hello', ' world'])
>>> a.items()
dict_items([('a', 'hello'), ('B', ' world')])
>>> 'a' in a
True
>>> 'a' in a.values()
>>> a={"a","b"}
>>> b={"b","d"}
>>> a | b
{'d', 'b', 'a'}
>>> a &b
{'b'}
>>> a-b
{'a'}
>>> b-a
{'d'}
>>> a ^b
{'d', 'a'}
>>> a = {"a","b","c","d"}
>>> a >b
True
>>> a<b
False
>>> a>=b
True
>>> a&b ==b
True

在这里插入图片描述

可变&不可变类型

python:

#不可变类型:整数、浮点数、复数、字符串、逻辑值、元组
#可变类型:列表、字典、集合
>>> test=[1,2,3,4]
>>> test
[1, 2, 3, 4]
>>> test2 = test
>>> test2
[1, 2, 3, 4]
>>> test[0]=44
>>> test2
[44, 2, 3, 4]


>>> a=10
>>> b = a
>>> b
10
>>> a=40
>>> b
10
>>> a
40

输入输出

python:

#输入 input, 与ps不一样, 这里的提示符默认不带‘:’
input('prompt') 
 #注意类型转换
 #输出 print
print("hello world")
#格式化字符串
#%d 数字,%s 字串
>>> '%03d_%10s' % (122222266,'string')
'122222266_    string'
>>> '%08d_%10s' % (12,'string')
'00000012_    string'

EX2
python:

>>> mylist
[1, 2, 3, 4, 5, 6]
>>> a = mylist[0:2:1].reverse()
>>> a
>>> mylist[0:2:1]
[1, 2]
>>> a
>>> mylist[0:3:1]
[1, 2, 3]
>>> a = mylist[0:3:1]
>>> a.reverse()
>>> a
[3, 2, 1]
>>> test
(1, 2, 3, 4, 5)
>>> test[1:3:1]
(2, 3)
>>> test[1:4:1]
(2, 3, 4)

在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值