Python Basics - 十四个基本点

直接上code了,code里有注释,最主要是理解python的基本概念和语法,适合已经有其它语言经验的developer快速学习。
from math import *

# 1. Variables, string, number, boolean
character_name = "aaaa"
character_age = 35.23
is_male = False

print(character_name.isupper())
print(character_name.replace("a", "n"))
print(round(3.5))
print(floor(3.7))
print(ceil(3.0))

# 2. how to define a list, defined by brackets
friends = [["Kevin", "Kevin12"], "Jim", "John"]
friends[0] = "Jim2"
print(friends[0:])
friends.extend([1, 2])

print(friends)

friends.remove("Jim")
print(friends)

# 3. Tuples, immutable list, can't be modified, defined by parenthesis
coordinates = (3, 4, 5)
print(coordinates[2])


# 4. Functions
def say_hi(name, age):
    print("Hello, " + name + ", you are " + str(age))
    return age + 1


next_age = say_hi("zhc", 30)
print(next_age)

# 5. if statement
is_male = True
is_tall = False
if is_male or is_tall:
    print("You are a man or tall")
else:
    print("You are neither male nor tall")

# 6. dictionary, key->value data structures, such as map in C++ and Java.
monthConversions = {
    "Jan": "January",
    "Feb": "February",
}

print(monthConversions["Jan"])
print(monthConversions.get("Jan2", "DefaultValue"))

# 7. loop
i = 1
while i < 5:
    print(i)
    i += 1

for letter in "zhc":
    print(letter)

# [3, 10)
for idx in range(3, 10):
    print(idx)

# 8. 2D lists, two dimensional list
number_grid = [
    [1, 2, 3],
    [4, 5, 6],
]

print(number_grid[0][1])

for row in number_grid:
    for num in row:
        print(num)

# 9. comments
'''
multi line comments
second line
'''

# 10. Try Except
'''
try:
    number = int(input("Enter a number: "))
    print(number)
except ValueError as err:
    print("Invalid input. Reason: " + str(err))
'''
# 11. Reading Files, r - read mode, r+ - read and write
working_file = open("working.txt", "r")

# Read all contents, print(working_file.read())

for day in working_file.readlines():
    print(day)
working_file.close()

working_file = open("working.txt", "a")
working_file.write("day5\n")
working_file.close()

# 12. Modules and Imports
'''
You need to have a good understanding of Python modules and packages to know how imports work.
A Python module is a file that has a .py extension, 
and a Python package is any folder that has modules inside it 
(or, in Python 2, a folder that contains an __init__.py file).

If the name isn’t found in the module cache, Python will proceed to search through a list 
of built-in modules. These are modules that come pre-installed with Python and can be found 
in the Python Standard Library. If the name still isn’t found in the built-in modules, Python 
then searches for it in a list of directories defined by sys.path. This list usually includes 
the current directory, which is searched first.

└── project
    ├── package1
    │   ├── module1.py
    │   └── module2.py
    └── package2
        ├── __init__.py
        ├── module3.py
        ├── module4.py
        └── subpackage1
            └── module5.py

from package1 import module1
from package1.module2 import function1
from package2 import class1
from package2.subpackage1.module5 import function2


'''
import useful_funcs

print(useful_funcs.feet_in_mile)


# pip install python-docx
# import docx

# 13. Classes and Objects

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age


stu = Student("zhc", 30)
print(stu.age)

# 14. Inheritance
class People:
    def __init__(self, name):
        self.name = name


class Student(People):
    def __init__(self, name, school):
        # People.__init__(self, name)
        super().__init__(name)
        self.school = school


stu = Student("zhc", "XiDian")
print(stu.school)
print(stu.name)

原文:http://blog.csdn.net/hongchangfirst/article/details/102878571

作者:hongchangfirst

hongchangfirst的主页:http://blog.csdn.net/hongchangfirst

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值