day 05总结(格式化输出的三种方式/基本运算符/if判断)

一、格式化输出的三种方式

1.占位符(Python3.0)

  • 占位符:%s(针对所有数据类型)、%d(仅仅针对数字类型)

  • 使用方法:引号内需要拼接的数据用占位符替代,引号外%加上数据所在的列表。

    name = 'Tbb'
    age = 20
    print('My name is %s' %name)
    print('Name:%s,Age:%s'%(name,age))

    结果:

    My name is Tbb
    Name:Tbb,Age:20
  • 友情提示:占位符格式化的话,一定要记住占位符%s针对所有数据类型使用。

2.Format格式化(Python3.4)

  • 使用方法:{}内加 索引,format()内加入需要的列表

    s1 = 'Tbb'
    s2 = 'male'
    s3 = 20
    print('Name:{0},Age:{2},Gender:{1}'.format(s1,s2,s3))

    结果:

    Name:Tbb,Age:20,Gender:male
  • 友情提示:如果你需要使用这个format格式化时,遇到多参数的时候,还是需要在句子后面传上一大堆参数。使用这个不如用占位符或下面的f-String格式化。(所以几乎没人用

3.f-String格式化(Python3.6)

  • 适用性:相比较前两种的格式化的方式,f-String格式化比较简单易懂,也是目前运用较多的格式化方式;所以三种格式化方式,首先推荐f-String格式化,其次占位符格式化,最后才是Format格式化。

  • 使用方法:引号前加上f(大小写都行),{}内加上所需列表

    name = "Tbb"
    age = 20
    print(f"Hello, {name}. I am {age}.")

    结果:
    Hello, Tbb. I am 20.

  • 骚操作系列:

    salary = 6.6666
    print(f'{salary:.2f}')

    结果:

    6.67

二、基本运算符

1.算术运算符

  • 算术运算符:

    u=1392726117,2823076269&fm=26&gp=0.jpg

  • 使用方法:

print(x + y)    #加
print(x - y)    #减
print(x * y)    #乘
print(x / y)    #除
print(x % y)    #取余
print(x // y)   #取整
print(x ** y)   #幂

2.比较运算符

  • 比较运算符:

    u=4194271112,1749686449&fm=26&gp=0.jpg

  • 使用方法:

    print(x > y)  #x大于y
    print(x < y)      #x小于y
    print(x >= y)   #x大于等于y
    print(x <= y) #x小于等于y
    print(x == y) #x等于y
    print(x != y)     #x不等于y
    print(x <> y) #x不等于y

    比较后返回的是布尔值True或False!

3.赋值运算符

  • 赋值运算符:

    u=1411142346,2907065328&fm=26&gp=0.jpg

  • 一元赋值运算符

    使用方法:

    x = 10

  • 二元赋值运算符

    使用方法:

    x += y    #x = x+y  
    x -= y    #x = x-y
    x *= y    #x = x*y
    x /= y    #x = x/y
    x **= y   #x = x**y
    x //= y   #x = x//y
    x %= y    #x = x%y

4.逻辑运算符

  • and(和),两个条件都为真就为True,否则都为False

  • or(或),只要有一个为真就为True,否则都为False

  • not(不是)

    使用例子:

    print(10>1 and False)
    print(True or False)
    print(not False)

    结果:

    False
    True
    True

5.身份运算符

  • 作用:身份运算符用于比较两个对象的存储单元。

  • 使用方法:

    x = 257
    y = x
    z = 257
    
    print(f'x is y:{x is y}')
    print(f'x == y:{x == y}')
    
    print(f'x is z:{x is z}')
    print(f'x == z:{x == z}')

    结果:

    x is y:True
    x == y:True
    x is z:False
    x == z:True
  • 注意:is和==的区别:is用于判断两个变量引用对象是否在同一块内存空间中, ==用于判断引用变量的值是否相等。

6.Python运算符优先级

  • 优先级:

    timg?image&quality=80&size=b9999_10000&sec=1564572221426&di=f1b7e4db297ccedafd72f00e02e3cdfc&imgtype=jpg&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D1492517199%2C501444532%26fm%3D214%26gp%3D0.jpg

  • 提示:你想让他优先算,加括号就行了,没必要记忆优先级!

三、流程控制之if判断

  • 概念:if就是如果的意思

1.if(单分支结构)

if 条件:
    代码1
    代码2
    代码3
    ...

2.if...else(双分支结构)

if 条件:
    代码1
    代码2
    代码3
    ...
else:
    代码1
    代码2
    代码3
    ...

if...else表示if成立代码成立会干什么,else不成立会干什么。

3.if...elif...else(多分支结构)

if 条件1:
    代码1
    代码2
    代码3
    ...
elif 条件2:
    代码1
    代码2
    代码3
    ...
elif 条件3:
    代码1
    代码2
    代码3
    ...
...
else:
    代码1
    代码2
    代码3
    ...

if...elif...else表示if条件1成立干什么,elif条件2成立干什么,elif条件3成立干什么,else...否则干什么。

4.if的嵌套

  • 嵌套的使用:
age = 30
inp_age = int(input('age:'))  # 17
if age >= inp_age:   # a
    if age > inp_age:  # b
        if 'e':
            print('猜小了') #  a成立b也成立e也成立我才做
    else: # c
        print('猜中了') # a成立c成立我才做
else: # d # a不成立我就做
    print('猜大了')
  • if的嵌套,比用多个if的方法,节约了时间和空间。

转载于:https://www.cnblogs.com/mgytz/p/11277474.html

以下是一个可能的实现方式: ```cpp #include <iostream> #include <sstream> #include <string> class Date { public: Date(int year = 1970, int month = 1, int day = 1) { if (isValidDate(year, month, day)) { year_ = year; month_ = month; day_ = day; } else { std::cerr << "Invalid date: " << year << "-" << month << "-" << day << std::endl; year_ = 1970; month_ = 1; day_ = 1; } } int getYear() const { return year_; } int getMonth() const { return month_; } int getDay() const { return day_; } Date nextday() const { int year = year_; int month = month_; int day = day_ + 1; if (day > daysInMonth(year, month)) { day = 1; ++month; if (month > 12) { month = 1; ++year; } } return Date(year, month, day); } int difference(const Date& other) const { int days = 0; if (*this < other) { for (Date d = *this; d < other; d = d.nextday()) { ++days; } } else if (*this > other) { for (Date d = other; d < *this; d = d.nextday()) { --days; } } return days; } std::string format() const { std::ostringstream oss; oss << year_ << "-" << month_ << "-" << day_; return oss.str(); } private: int year_; int month_; int day_; bool isValidDate(int year, int month, int day) const { if (year < 1970 || month < 1 || month > 12 || day < 1 || day > daysInMonth(year, month)) { return false; } return true; } int daysInMonth(int year, int month) const { static const int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int daysInFeb = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 29 : 28; if (month == 2) { return daysInFeb; } else { return days[month]; } } }; bool operator<(const Date& lhs, const Date& rhs) { if (lhs.getYear() < rhs.getYear()) { return true; } else if (lhs.getYear() > rhs.getYear()) { return false; } else { if (lhs.getMonth() < rhs.getMonth()) { return true; } else if (lhs.getMonth() > rhs.getMonth()) { return false; } else { return lhs.getDay() < rhs.getDay(); } } } bool operator>(const Date& lhs, const Date& rhs) { return rhs < lhs; } bool operator<=(const Date& lhs, const Date& rhs) { return !(rhs < lhs); } bool operator>=(const Date& lhs, const Date& rhs) { return !(lhs < rhs); } bool operator==(const Date& lhs, const Date& rhs) { return lhs.getYear() == rhs.getYear() && lhs.getMonth() == rhs.getMonth() && lhs.getDay() == rhs.getDay(); } bool operator!=(const Date& lhs, const Date& rhs) { return !(lhs == rhs); } int main() { Date d1(2022, 10, 1); std::cout << d1.format() << std::endl; Date d2(2022, 2, 28); std::cout << d2.format() << " -> " << d2.nextday().format() << std::endl; Date d3(2021, 12, 31); std::cout << d3.format() << " and " << d1.format() << " -> " << d3.difference(d1) << std::endl; return 0; } ``` 这个实现中,Date 类包含了年、月、日三个数据成员,构造函数会检查输入的日期是否合法,不合法则默认为 1970 年 1 月 1 日。类中还定义了 `getYear`、`getMonth`、`getDay` 函数用于获取年、月、日的值,以及 `nextday` 函数用于返回下一天的日期,`difference` 函数用于计算当前日期和另一个日期之间的天数差。类中还定义了一个 `format` 函数,用于将日期格式化成字符串。 此外,还实现了一些运算符重载,包括 `<`、`>`、`<=`、`>=`、`==` 和 `!=`,用于比较两个日期的大小关系。 在 main 函数中,我们可以创建一个 Date 对象,然后调用其各种函数来进行测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值