python3编程基础之一:操作

  基本操作有:读数据、写数据、运算、控制、输入、输出、语句块

1、读取数据:

  num1 = 50
  num2 = num1                   //通过num2取得num1的值,这就是逻辑上的读取

  测试数据:print(num1)
  结果:50
  测试数据:print(num2)
  结果:50

2、写入数据:

  num1 = 50 

  num2 = num1                   //将num1的值写入num1,实际上是num2读取到num1的值后,num2的值被写入,num1的值被读取,一般是赋值号左侧的被写入,右侧的被读取,分别是左值和右值

  测试数据:print(num1) 
  结果:50 
  测试数据:print(num2) 
  结果:50

3、运算:数值运算、位运算、逻辑运算

  1)、数值运算:+ - × /  %  pow() sqrt()等

  测试数据:print(num1 + num2)         //+
结果:100
  测试数据:print(num1 - 10)           //-
  结果:40
  测试数据:print(num1 * 2)           //*
  结果:100
  测试数据:print(num1 / 3)           // /,和c语言不一样,没有强制转换为整型结果
  结果:16.666666666666668

  import math                 //必须先导入数学库,才能使用内置的函数pow和sqrt
  测试数据:print(math.pow(num1, 2))        //pow函数,注意参数个数及类型
  结果:2500.0

  测试数据:print(math.sqrt(num1))          //sqrt函数,注意参数个数及类型
  结果:7.0710678118654755

  2)、改变数据流向:输入、输出

  调用函数:name = input("Enter your name: ")       //用name获取用户的输入数据,input中传入提示信息
  用户输入:Enter your name: zhangsan             //用户输入信息
  测试数据:print(name)
  结果:zhangsan
  调用函数:age = int(input("Enter your age: "))
  Enter your age: 23
  测试数据:print(name, age)
  结果:zhangsan 23                  //输入数据成功,并且完成数据的输出  

  3)、格式控制:转换含义

  测试数据:print('hello \                 // \换行
  ... world!')
  结果:hello world!
  测试数据:print('hello \\ world!')
  结果:hello \ world!
  测试数据:print('hello \' world!')
  结果:hello ' world!
  测试数据:print('hello \" world!')
  结果:hello " world!
  测试数据:print('hello \a world!')
  结果:hello  world!
  测试数据:print('hello \b world!')  
  结果:hello world!
  测试数据:print('hello \e world!')
  结果:hello \e world!
  测试数据:print('hello \000 world!')
  结果:hello  world!
  测试数据:print('hello \v world!')   
  结果:hello  
          world!
  测试数据:print('hello \t world!')
  结果:hello    world!      

  4)、语句块:组合语句

  5)、数据遍历:获取某个集合中所有数据

  

猜你喜欢

转载自www.cnblogs.com/guochaoxxl/p/11783788.html