kaggle_python七天入门_第一天

概述

kaggle第一天学习内容主要有:

  1. Python 语法(syntax)
  2. 变量赋值(variable assignment)
  3. number中的整数(int)与小数(float)
  4. 算数运算符(arithmetic operator)
  5. notebook 快捷键

1. Hello Python

Python名字是源于英国戏剧团Monty Python,他们曾表演过短剧Spam。
我们用与之相关的例子,来引出Python的学习。

spam_amount = 0
print(spam_amount)

# Ordering Spam, egg, Spam, Spam, bacon and Spam (4 more servings of Spam)
spam_amount = spam_amount + 4

if spam_amount > 0:
    print("But I don't want ANY spam!")

viking_song = "Spam " * spam_amount
print(viking_song)

我们讲上述代码拆开分析,这个简单的代码块展示了Python中许多重要方面。

spam_amount = 0
  • = : 赋值运算符(assignment operator)
    Python语言中,我们不需要declare;也不需要告诉Python赋值的具体类型,我们也可以在之后重新为变量赋予不同的数据类型。
print(spam_amount)
  • print() : 是Python的一个功能。我们将括号(parenthese)置于方程之后,讲inputs放入括号内
# Ordering Spam, egg, Spam, Spam, bacon and Spam (4 more servings of Spam)

spam_amount = spam_amount + 4
  • 仍然用 = 表示再赋值;讲右边的式子(expression)赋值到左边
if spam_amount > 0:
    print("But I don't want ANY spam!")

viking_song = "Spam Spam Spam"
print(viking_song)

本节我们不讨论太多条件语句(conditionals)

  • 冒号(colon),: 是一个新的代码块(code block)的开始,随后的缩进(indent)的代码行是这一代码块的一部分

  • 字符串(strings)可以用单引号和双引号标记(single or double quotation marks)。
    但是strings中可能包含单引号,所以在使用单引号标记字符串的时候要小心。

  • * :在Python中,可以用一个字符串* 上一个数字,得到这个字符串重复相应的次数。
    Python提供了很多节省时间的小技巧,* ,+ 在不同类型的输入中,他们有不用的应用。

2. Numbers and arithmetic in Python 数字与算术

‘Number’ :是一个不正式的名称,数字有他自己的类型:

spam_amount = 0
type(spam_amount) # int
type(19.95) # float
  • int : short for integer

  • float : is a number with a decimal place - very useful for representing things like weights or proportions.

  • type : 是这节中第二个内置函数(built-in function)

Python中的基本运算:
Operater Name Description
a + b Addition Sum of a and b
a - b Subtraction Difference of a and b
a * b Multiplication Product of a and b
a / b True division Quotient of a and b
a // b Floor division Quotient of a and b, removing fractional parts
a % b Modulus Integer remainder after division of a by b
a ** b Exponentiation a raised to the power of b
-a Negation The negative of a

计算机可以做两种除法。“True division” 和 “Floor division”

# the below lines give us a `float`
print(5 / 2)  #2.5
print(6 / 2)  #3.0

# ''//' 运算符给我们一个四舍五入到下一个整数的结果
print(5 // 2) # 2
print(6 // 2) # 3
运算顺序(order of operations)
  • 计算顺序:

    PEMDAS - Parentheses, Exponents, Multiplication/Division, Addition/Subtraction.

  • 括号(parentheses)可以强制python按照编码者的想法去计算。

total_height_meters = (hat_height_cm + my_height_cm) / 100
print("Height in meters =", total_height_meters)
运算的内置函数(builtin function of working with numbers)
  • min and max 返回参数的最小值和最大值。

  • abs returns the absolute value of it argument:

print(min(1, 2, 3)) # 1
print(max(1, 2, 3)) # 3

print(abs(32)) # 32
print(abs(-32))# 32
  • int and float 可以转换他们的参数(arguments)到相应的类型
print(float(10)) # 10.0
print(int(3.33)) # 3
# They can even be called on strings!
print(int('807') + 1) # 808

3. notebook快捷键

  1. ctrl + enter:运行cell
  2. Esc + a(b): 在存在的cell之前(后)增加一个cel

猜你喜欢

转载自blog.csdn.net/weixin_49340599/article/details/113110417
今日推荐