系统相关
首页 > 系统相关> > Linux + Python 第五天

Linux + Python 第五天

作者:互联网

一、格式化打印
# 将信息打印
------------------info of tanuki------------------
Name  : Tanuki
email : tanuki_11@163.com
hobby : stars and seas
-----------------------end------------------------
# 开始
In [1]: info = 'info of tanuki'
In [2]: end = 'end'
In [3]: info.center(50,'-')
Out[3]: '------------------info of tanuki------------------'
In [4]: end.center(50,'-')
Out[4]: '-----------------------end------------------------'

In [5]: name = input()
In [6]: name
Out[6]: 'tanuki '
In [7]: email = input("email:")
email:tanuki_11@163.com
In [8]: hobby = input("hobby:")
hobby:stars and seas
In [9]: mes = f'''
   ...: ------------------info of tanuki------------------
   ...: Name  : {name}
   ...: email : {email}
   ...: hobby : {hobby}
   ...: -----------------------end------------------------
   ...: '''
In [10]: print(mes)

------------------info of tanuki------------------
Name  : tanuki 
email : tanuki_11@163.com
hobby : stars and seas
-----------------------end------------------------

二、for循环

# 之前用shell,python分别写了for 和 if 的循环,今天补充一下for循环。
# python 中的for
In [1]: for i in range(10):
    ...:     number = 1
    ...:     a = input("insert a number:")
    ...:     if not a:
    ...:         continue
    ...:     if a == "q":
    ...:         break
    ...:     a = int(a)
    ...:     if a > 1:
    ...:         print("less then 1")
    ...:     elif a < 1:
    ...:         print("more then 1")
    ...:     else:
    ...:         print(f"{a} is right!bingo")
    ...: 
    ...: 
insert a number:1
1 is right!bingo
insert a number:2
less then 1
insert a number:6
less then 1
insert a number:q
# shell脚本中的for
[tanuki@localhost python_study]$ bash for.sh 
please insert a number: 1
20 greater then 1
please insert a number: 10
20 greater then 10
please insert a number: 20
the 20 is right!
[tanuki@localhost python_study]$ cat for.sh 
#!/bin/bash
# author : tanuki
# using  : For loop in shell
a=20
for i in `seq 1 10`
do 
  read -p "please insert a number: " number
  if [ "$a" -lt "$number" ] ;then
    echo "$a less $number"
  elif [ "$a" -gt "$number" ] ;then
    echo "$a greater then $number" 
  else
    echo "the $number is right!"
    exit
  fi
done 
二、判别奇偶数
# shell判断
[tanuki@localhost python_study]$ cat ji_ou_judge.sh 
#!/bin/bash
#!/bin/bash
for((i=1;i<=10;i++))
do
  a=`expr $i % 2`
  if [ "$a" -eq 0 ] ;then
    echo "$i 是偶数 "
  else
    echo "$i 是奇数"
  fi
done
[tanuki@localhost python_study]$ ji_ou_judge.sh 
1 是奇数
2 是偶数 
3 是奇数
4 是偶数 
5 是奇数
6 是偶数 
7 是奇数
8 是偶数 
9 是奇数
10 是偶数 
[tanuki@localhost python_study]$ 
# python判断
[tanuki@localhost python_study]$ ji_ou_judge.py 
0是偶数
1是奇数
2是偶数
3是奇数
4是偶数
5是奇数
6是偶数
7是奇数
8是偶数
9是奇数
10是偶数
[tanuki@localhost python_study]$ cat ji_ou_judge.py 
#!/usr/bin/env python3
# function about python
#def tanuki():
#    print("螃蟹在剥我的壳,笔记本在写我,")
#    print("漫天的我落在枫叶上雪花上,") 
#    print("而你在想我...")
#tanuki()
for i in range(11):
#    if i % 2 == 0:   #两种写法,不赋值a也可以。
     a = i % 2
     if a > 0:
        print(f"{i}是奇数")
     else:
        print(f"{i}是偶数")

标签:...,insert,Python,number,python,Linux,print,第五天,tanuki
来源: https://www.cnblogs.com/tanukisama/p/16345668.html