Home Python grammar
Post
Cancel

Python grammar

1
2
3
4
5
6
7
8
9
#List: One of the four data types in Python that are used to hold collections of data
list = [1,2,3,4,5,6] 
list.insert(1,9) 
print(list)
# [1,9,2,3,4,5,6]
list[:3]
# [1,9,2] 
list[-3:]
#[4,5,6]
1
2
3
# tuple : list which is immutable
my_tuple = tuple(list)
new_list = list(my_tuple)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# WHile other languages are usually represented as integers,
# Python  is represented as float
12/3
# 4.0
# But, it shows integer when you use `//` as operations
12//3
#4
#Modulo - to find the remainder
12 % 3
#0
#power operator
2**3
#8

#String
new_str = 'welcome'

#input
name = input('input your name: ') 
james
#input your name:

print(name)
#james

num1 = input('input number: ')
type(num1)
### str

### change into int from str type
num1 = int(input())

# Formatting the string input
'Name: {}'.format('Paul')


name = input()
f'my name is {name.upper()}'

# dir - returns the attributes of an object
dir

# help() - provides documentation for a mehod Etc.
help()

# Dictionaries
info = {'one' : 1 , 'two' : 2, 'three' : 3}
info.keys()
info.values()
for i in info:
   print(i)

# Dictionaries item()
info.item()

#zipped
list1 = [1,2,3]
list2 = [4,5,6]
zipped = zip(list1, list2)
list(zipped)

for number in num_dict:
   print(f'The {number} is {num_dict[number]}')
1
2
3
4
5
6
class Person:
  def __init__(self, n, a):
     self.name = n
     self.age = a

person = Person('Jaekyeong', 25)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Person:
    def __init__(self, n, a):
        self.name = n   # self.age is instance variable
        self.age = a
    def display_details(self):
        return f'{self.name} is {self.age} years old'

person = Person('Jaekyeong', 25)
print(person.display_details())
person2 = Person('Joy', 22)
print(person2.display_details())

person_list = [person, person2]
#print(person_list)

for p in person_list:
    print(p.display_details())
    

class Contract_employee(Person):
    def __init__(self, n, a):
        super().__init__(n, a)
        
person3 = Contract_employee('John', 22)
print(person3.display_details())
This post is licensed under CC BY 4.0 by the author.