Python Basics
前言
內容
- Variable Assignment
# Create a variable savings
savings = 100
# Print out savings
print(savings)
- Calculations with variables
# Create a variable savings
savings = 100
# Create a variable factor
factor = 1.10
# Calculate result
result = savings * factor ** 7
# Print out result
print(result)
- Other variable types
# Create a variable desc
desc = "compound interest"
# Create a variable profitable
profitable = True
- Operations with other types
# Several variables to experiment with
savings = 100
factor = 1.1
desc = "compound interest"
# Assign product of factor and savings to year1
year1 = savings * factor
# Print the type of year1
print(type(year1))
# Assign sum of desc and desc to doubledesc
doubledesc = desc + desc
# Print out doubledesc
print(doubledesc)
- Type conversion
# Definition of savings and result
savings = 100
result = 100 * 1.10 ** 7
# Fix the printout
print("I started with $" + str(savings) + " and now have $" + str(result) + ". Awesome!")
# Definition of pi_string
pi_string = "3.1415926"
# Convert pi_string into float: pi_float
pi_float = float(pi_string)