python学习1-字符串数字基本运算以及if条件和while循环
python学习1-字符串数字基本运算以及if条件和while循环
字符串表达形式共四种:
name = "string"
name = 'string'
name = """string"""
name = '''string'''
数字基本运算方式:
a = 39
b = 4 c = a + b
c = a - b
c = a*b
c= a**b #次幂
c = a/b c = a%b #取余数 c = a//b #取除数
条件判断:
in1 = input('please input the rank:')
print(in1)
if in1 == '':
print('hello world!')
elif in1 == '':
print('HELLO WORLD!')
elif in1 == '':
print('hello world 3')
else:
print('hello world 4')
上面代码中,通过input输入的数字并没有被python视为数字,而是视为了字符串,故下面在条件判断时需要使用引号将数字引起。
循环判断:
while count < 10:
print(count)
count = count + 1
使用循环完成简单的算法:
# output numbers: 1 to 10 without 7
n = 1
while n < 11:
if n != 7:
print(n)
n = n + 1 print('---end---') # find sum of numbers from 1 to 100
n = 1
sumofn = 0
while n < 101:
sumofn = sumofn + n
n = n + 1 print(sumofn) # find sum of numbers 1-2+3-4+...-100
n = 1
sumofnn = 0
while n < 101:
if n%2 == 0:
sumofnn = sumofnn - n
else:
sumofnn = sumofnn + n
n = n + 1
print(sumofnn)
python学习1-字符串数字基本运算以及if条件和while循环的更多相关文章
- Python第四天 流程控制 if else条件判断 for循环 while循环
Python第四天 流程控制 if else条件判断 for循环 while循环 目录 Pycharm使用技巧(转载) Python第一天 安装 shell 文件 Python第二天 ...
- Python学习笔记-字符串
Python之使用字符串 1.所有的标准序列操作(索引,分片,乘法,判断成员资格,求长度,取最小值,最大值)对字符串同样适用.但是字符串都是不可变的. 2.字符串格式化使用字符串格式化操作符即%. f ...
- python学习笔记——字符串
类方法string.upper(str)需要引入string模块,实例方法str.upper()不需要引入string模块 无与伦比的列表解析功能 # coding=utf-8 # 列表解析 prin ...
- python学习之字符串常用方法和格式化字符串
Python中的字符串同样适用标准的序列操作(索引,分片,乘法,成员判断,求长度,取最小值和最大值),但因为字符串是不可变的,因此字符串不支持分片赋值. s='http://www.baidu.com ...
- 《Python学习手册 第五版》 -第13章 while循环和for循环
上一章已经讲过if条件语句,这章重点是循环语句:while.for 本章的重点内容 1.while循环 1)一般形式 2)break.continue.pass和循环的else 2.for循环 1)一 ...
- Python学习之字符串函数
下面是在看python核心编程中序列字符串中提到的一些函数,根据自己的学习理解总结了下,方便日后用到的时候查看. 1.string.capitalize() 把字符串的第一个字符大写 例子: ...
- Python学习二---字符串
一.字符串 1.1.字符串和转义字符 转义字符需要使用\来表示 1.2.字符串连接 print 字符串1 字符串2,打印出来的字符串直接连接在一起没有空格 print 字符串1,字符串2,打印出来的字 ...
- python学习:猜数字小游戏
在学习python过程中,没有项目做,就想到哪儿弄到哪儿. 头一发.让机器随机固定一个数字,然后让人去猜. 就这么简单.代码如下: #-*- encoding:utf8 -*- import rand ...
- python学习之字符串
最近在学习python,随手做些记录,方便以后回顾 #字符串是不可再改变的序列aa='abcd'#aa[2:]='ff' #报错,不可直接赋值#字符串格式化:使用格式化操作符即百分号%来实现print ...
随机推荐
- 14、java实现poi操作excel,包括读和写日期格式,并且设置字体样式
1.首先大家来看导出的结果 下边就是导出的代码了 protected void testExcel() throws IOException{ String path=getServletContex ...
- shell cat 合并文件,合并数据库sql文件
> 覆盖写入 >> append模式写入 ###################################################################合并数 ...
- Windows下安装PIL进行图像处理
过程一波三折 参考 http://blog.csdn.net/zxia1/article/details/8254113 http://stackoverflow.com/questions/3544 ...
- [已解决]报错:报错AttributeError: 'int' object has no attribute 'upper'
原因:openpyxl版本低,需升级 pip install --upgrade openpyxl
- Excel_VBA 常用代码
单元格编辑后改变背景色(6号,355832828) Dim oldvalue As Variant Private Sub Worksheet_Change(ByVal Target As Range ...
- js 万能判断
console.log(Object.prototype.toString.call(123)) //[object Number] console.log(Object.prototype.toSt ...
- oh my zsh 如何启用插件
注 根据自己的需求启用插件.但是,插件具体实现什么功能就得自己看啦. 官网说明 实践 其实默认oh my zsh(以下简称zsh)已经在安装的时候就帮我们下载好了所有插件,只不过需要用户自己选择启用哪 ...
- Ubuntu 奇怪踩坑记录
仓库ppa问题 当你使用 apt-get update 时候,提示 仓库 "http://ppa.launchpad.net/hzwhuang/ss-qt5/ubuntu bionic Re ...
- docker Dcokerfile学习---构建nginx环境
1.创建项目目录并上传包 $ mkdir docker_nginx $ cd docker_nginx 下载nginx包 $ wget http://nginx.org/download/nginx- ...
- 如何运行linux shell程序
原文地址:http://www.sohu.com/a/138822796_610671 首先,我们从一个十分简单的例子test.sh开始吧: #!/bin/sh #this is a test. cd ...