Python

  • # means comments.

  • print
    e.g.

print("Hello World.")
# or
print('Hello World.')
# or
message_string = "Hello World."
print(message_string)

  • Variables
    Variables can’t have spaces or symbols in their names other than an underscore (_). They can’t begin with numbers but they can have numbers after the first letter.

  • Numbers
    An integer, or int, is a whole number. It has no decimal point.
    A floating-point number, or a float, is a decimal number.

  • Calculations +, -, *, /, ** (exponent), % (Modulo)
    Notice that when we perform division, the result has a decimal place. This is because Python converts all ints to floats before performing division. In older versions of Python (2.7 and earlier) this conversion did not happen, and integer division would always round down to the nearest integer.

  • The + operator doesn’t just add two numbers, it can also “add” two strings!
    e.g.

greeting_text = "Hey there!"
question_text = "How are you doing?"
full_text = greeting_text + question_text

# Prints "Hey there!How are you doing?"
print(full_text)
  • str() : convert number to str
    e.g.
birthday_string = "I am "
age = 10
birthday_string_2 = " years old today!"

# Concatenating an integer with strings is possible if we turn the integer into a string first
full_birthday_string = birthday_string + str(age) + birthday_string_2

# Prints "I am 10 years old today!"
print(full_birthday_string)
  • += Plus Equals
    Python offers a shorthand for updating
    Preview: Docs Loading link description
    variables. When you have a number saved in a variable and want to add to the current value of the variable, you can use the += (plus-equals) operator.
    e.g.
# First we have a variable with a number saved
number_of_miles_hiked = 12

# Then we need to update that variable
# Let's say we hike another two miles today
number_of_miles_hiked += 2

# The new value is the old value
# Plus the number after the plus-equals
print(number_of_miles_hiked)
# Prints 14
  • Multi-line Strings: By using three quote-marks (""" or ''') instead of one, we tell the program that the string doesn’t end until the next triple-quote.
    e.g.
leaves_of_grass = """
Poets to come! orators, singers, musicians to come!
Not to-day is to justify me and answer what I am for,
But you, a new brood, native, athletic, continental, greater than
  before known,
Arouse! for you must justify me.
"""

你可能感兴趣的:(python,开发语言)