020000_Python入门,,一、为什么选择Pyt


一、为什么选择Python?

  1、Python 追求的是找到最好的解决方案,相比之下,其他语言追求的是多种解决方案。

  2、Python 的最大优点,是使你能够专注于解决问题而不是去搞明白语言本身。

二、变量与字符串

  1、变量

    变量是编程中最基本的存储单位,变量会暂时性地储存你放进去的东西。

    变量的名字叫做标识符。

    Python 对大小写敏感,“a” 和 “A” 是两个不同的变量。

  2、print()

    打印是Python 中最常用的功能。

    驼峰命名法

    帕斯卡命名法

  3、字符串

    单引号、双引号、三引号

    字符串的基本用法——合并    

1 what_he_does = ‘ plays ‘2 his_instrument = ‘guitar‘3 his_name = ‘Robert Johnson‘4 artist_intro = his_name + what_he_does + his_instrument5 print(artist_intro) # Robert Johnson plays guitar

    type()函数查看变量的类型

    由于中文注释会打字报错,所以需要在文件开头加一行魔法注释# coding=utf-8 ,也可以在设置里面找到“File Encodings”设置为 UTF-8

    数据类型转换

1 num = 12 string = ‘1‘3 num2 = int(string)4 print(num + num2) # 2

    

    字符串相乘  

1 words = ‘words‘ * 32 print(words) # wordswordswords
1 word =‘a loooooong word‘2 num =123 string = ‘bang!‘4 total = string * (len(word) - num) # 这里的意思是total = ‘bang‘ * (16 - 12)5 print(total) # bang!bang!bang!bang!

  

    字符串的分片与索引

      字符串可以通过 string[x] 的方式进行索引、分片,也就是加一个 [ ]。

      分片(slice)获得的每个字符串可以看做是原字符串的一个副本。     

 1 name = ‘My name is Mike‘ 2 print(name[0])  # M 3 print(name[-4])  # M 4 print(name[11:14])  # Mik 5 print(name[11:15])  # Mike 6 print(name[5:])  # me is Mike 7 print(name[:5])  # My na 8 ‘‘‘ 9 1、:两边分别代表着字符串的分给是从哪里开始,并到哪里结束。10 2、以name[11:14]为例,截取的编号从第11个字符开始,到位置为14但不包括第14个字符结束。11 3、像name[5:]这样的写法代表着从编号为5的字符到结束的字符串分片。12 4、像name[:5]代表着从编号为0的字符开始到编号为5单不包括第5个字符的字符分片13 ‘‘‘

  技术分享图片

    找出你朋友中的魔鬼  

1 word = ‘friends‘2 find_the_evil_in_your_friends = word[0] + word[2:4] + word[-3:-1]3 print(find_the_evil_in_your_friends)  # fiend

  

    实际项目中的应用

  技术分享图片

    字符串的方法

      Python 是面向对象进行编程的语言,而对象拥有各种功能、特性,专业术语称之为——方法(Method)。

      隐藏信息  

1 phone_number = ‘1386-666-0006‘2 hiding_number = phone_number.replace(phone_number[:9], ‘*‘ * 9)3 ‘‘‘这里使用了新的字符串方法replace()进行“遮挡”,replace方法的括号中,第一个phone_number[:9] 代表要被替换掉的部分,4 后面的‘*‘ * 9 表示简要替换成什么字符,也就是把*乘以9,显示9个*  ‘‘‘5 print(hiding_number)  # *********0006

  

      模拟手机通讯录中的电话号码联想功能

1 search = ‘168‘2 num_a = ‘1386-168-0006‘3 num_b = ‘1681-222-0006‘4 print(search + ‘ is at ‘ + str(num_a.find(search) + 1) + ‘ to ‘ +str(num_a.find(search) + len(search)) + ‘ of num_a‘)5 # 168 is at 6 to 8 of num_a6 7 print(search + ‘ is at ‘ + str(num_b.find(search) + 1) + ‘ to ‘ +str(num_b.find(search) + len(search)) + ‘ of num_b‘)8 # 168 is at 1 to 3 of num_b

    字符串格式化符 

      当字符串中有多个“空”需要填写时,可以使用.format()进行批处理。

      填空题

      ____a word she can get what she ____ for.  

1 print(‘{} a word she can get what she {} for.‘.format(‘with‘, ‘came‘))2 print(‘{preposition} a word she can get what she {verb} for.‘.format(preposition=‘with‘, verb=‘came‘))3 #  preposition 介词 , verb 动词4 print(‘{0} a word she can get what she {1} for.‘.format(‘with‘, ‘came‘))

      利用百度提供的天气api实现客户端天气插件的开发的代码片段

1 city = input("write down the name of city:")2 url = "http://apistore.baidu.com/microservice/weather?citypinyin={}.format(city)"

三、最基本的魔法函数

  1、重新认识函数

    print 是一个放入对象就能将结果打印的函数

    input 是一个让用户输入信息的函数

    len 是一个可以测量对象长度的函数

    int 是一个可以将字符串类型的数字转换成证书类型的函数

    Python 中所谓的使用函数就是把你要处理的对象放到一个名字的括号里面就可以了。

    内建函数(Bulit-int Functions),在安装完成之后就可以使用它们,是“自带”的。

  2、开始创建函数

    def(即 define,定义)的含义是创建函数,也就是定义一个函数。

    arg(即 argument,参数),另外一种写法是parameter 。

    return 即返回结果。

    

020000_Python入门

评论关闭