Belows are what I have learned about Python from av37566782.
1 print
name = ccy
age = 22
print(name+" is my favourite singer, who is", age, "years old.")
The result is
ccy is my fvourite singer, who is 22 years old.
In “print”, the strings could be connected by “+”. When we want to connect a number with string, we should use “,”.
2 Variables
Strings: plain text
Numbers: int, float
Bool: true and false
3 Some functions
Functions about strings.
name = "CcY"
age = 22
print(name.lower())#turn all the letters into lower case
print(name)
print(len(" is my favourite singer, who is"))#calculate the numbers of string
print(name.upper().isupper())#input "Ture" if the string is upper case
The rusult is
ccy
CcY
31
True
Functions about list of string:
bel_canto = "tenor baritone "
singers = ["ccy", "ayg", "zyl"]
print(singers)#print all the strings in list
print(singers[1])#print the second string
print(singers.index("ccy"))#to find where "ccy" is
print(bel_canto.replace("tenor" , "bass"))#change"tenor" into "bass"
print(bel_canto)#the primary list dosen't change
The result is
[‘ccy’, ‘ayg’, ‘zyl’]
ayg
0
bass baritone
tenor baritone
4 import modules
There are three ways.
form…import…
from fib import fibonacci
Import function fibonacci from module fib.
from…import*
from math import *
Import all the funtions in module math.
import…
import math
print(math.sqrt(4))
5 input from outside
star = input("Your favourite singer: ")
song = input("The favourite song:")
print("Your favourite singer is " + star + ". The favourite song is " + song+".")
The result is
Your favourite singer: ccy
The favourite song:Ah,mes amis
Your favourite singer is ccy. The favourite song is Ah,mes amis.
6 list
ycf = ["ayg", "zyl", "fsj", "hzhf", "lpj", "zc"]
shx = ["wx", "zs"]
print("1",ycf[-1])#1, find the last one
ycf.extend(shx)
print("2",ycf)#2,add list "shx" to "ycf"
ycf.append("ccy")
print("3",ycf)#3,add string "ccy" to "ycf"
ycf.insert(2,"ccy")
print("4",ycf)#4, insert string "ccy" to the third location
ycf.remove("lpj")
print("5",ycf)#5, remove string "lpj"
print("6",shx)#6
shx.clear()
print("7",shx)#7, clear all the string in "shx"
ycf.pop()
print("8",ycf)#8,remove the last string
print("9",ycf.count("ccy"))#9, calculate the numbers of "ccy"
ycf.reverse()
print("10",ycf)#10, reverse
ycf.sort()
print("11",ycf)#11,sort the list as a ascending list
ycfszd = ycf
print("12",ycfszd)#12, copy the list
1 zc
2 [‘ayg’, ‘zyl’, ‘fsj’, ‘hzhf’, ‘lpj’, ‘zc’, ‘wx’, ‘zs’]
3 [‘ayg’, ‘zyl’, ‘fsj’, ‘hzhf’, ‘lpj’, ‘zc’, ‘wx’, ‘zs’, ‘ccy’]
4 [‘ayg’, ‘zyl’, ‘ccy’, ‘fsj’, ‘hzhf’, ‘lpj’, ‘zc’, ‘wx’, ‘zs’, ‘ccy’]
5 [‘ayg’, ‘zyl’, ‘ccy’, ‘fsj’, ‘hzhf’, ‘zc’, ‘wx’, ‘zs’, ‘ccy’]
6 [‘wx’, ‘zs’]
7 []
8 [‘ayg’, ‘zyl’, ‘ccy’, ‘fsj’, ‘hzhf’, ‘zc’, ‘wx’, ‘zs’]
9 1
10 [‘zs’, ‘wx’, ‘zc’, ‘hzhf’, ‘fsj’, ‘ccy’, ‘zyl’, ‘ayg’]
11 [‘ayg’, ‘ccy’, ‘fsj’, ‘hzhf’, ‘wx’, ‘zc’, ‘zs’, ‘zyl’]
12 [‘ayg’, ‘ccy’, ‘fsj’, ‘hzhf’, ‘wx’, ‘zc’, ‘zs’, ‘zyl’]
7 differences between Python and C++
- We don’t need to write the varible type before varibles
- There are different varible types in the list.
- Python could import modules to simplify codes.
- The keywords are easier to understand, because they always are the reality meanings in English.(For example, “print” is more direct than “cout”.
At all, Python is easier to use for beginners. What I should do is to overcome these differences between Python and C++.