-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data_type_examples.py
153 lines (117 loc) · 3.2 KB
/
Data_type_examples.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# 1. String Concat
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result)
# 2. String-len
text = "Python is awesome"
length = len(text)
print("Lenght of the text: ", text)
# 3. String-lowercase
text1 = "Python is awesome"
uppercase = text.upper()
lowercase = text.lower()
print("Uppercase: ", uppercase)
print("Lowercase: ", lowercase)
# 4. String replace
text2 = "Python is awesome"
new_text = text.replace("awesome","great")
print("Modified text: ", new_text)
# 5. String split
text3 = "Python is lovely"
word = text3.split()
print("Words: ", word)
# 6. String strip
text4 = " Python is gorgeous "
word1 = text4.strip()
print("Strip words:", word1)
# 7. String-substring
text5 = "Python is awesome"
substring = "is"
if substring in text:
print("Found in the text: ", substring)
# 8. Float variable
num1 = 5.0
num2 = 2.0
# Basic Arithmetic
result1 = num1 + num2
print("Addition: ", result1)
result2 = num1 - num2
print("Subtraction: ", result2)
result3 = num1 * num2
print("Multiplication: ", result3)
result4 = num1 / num2
print("Division: ", result4)
# Rounding
result5 = round(3.14159265359) # Round to 2 decimal places
print("Rounded: ", result5)
# Integer Variable
num3 = 10
num4 = 5
# Integer Division
result6 = num3 // num4
print("Integer Division :", result6 )
#Modulus Remainder(%)
result7 = num3 % num4
print("Modulus Remainder: ", result7)
#Absolute Value
result8 = abs(-7)
print("Absolute Value: ", result8)
# regex search
import re
text6 = "The quick brown fox"
pattern1 = r"brown"
# re.search(): searches for entire string for a match and returns the first occurence
search = re.search(pattern1, text6)
if search:
print("Pattern found: ", search.group())
else:
print("Pattern not found")
# regex match
# Check for the match at the beginning of the string
match = re.match(pattern1, text)
if match:
print("Match found: ", match.group())
else:
print("No Match")
# regex replace
text7_A = "The quick brown fox jumps over the lazy brown dog"
pattern = r"brown"
replacement = "red"
new_text_a = re.sub(pattern, replacement, text7_A)
print("Modified text: ", new_text_a)
# regex-findall
matches_A = re.findall(pattern, text7_A)
if matches_A:
print("Pattern found: ", matches_A)
else:
print("Pattern not found")
# regex-split
text8 = "apple,grapes,orange,banana,pinaple"
pattern_b = r","
split_result = re.split(pattern_b, text8)
print("Split result: ", split_result)
"""
(.venv) gauravmtwt1@gauravmtwt1-IdeaPad-3-15ADA05:~/Documents$ /home/gauravmtwt1/Documents/git-demo/.venv/bin/python "/home/gauravmtwt1/Documents/Python for DevOps/github_python_topic_upload/Python-for-Devops/Data_type_examples.py"
Hello World
Lenght of the text: Python is awesome
Uppercase: PYTHON IS AWESOME
Lowercase: python is awesome
Modified text: Python is great
Words: ['Python', 'is', 'lovely']
Strip words: Python is gorgeous
Found in the text: is
Addition: 7.0
Subtraction: 3.0
Multiplication: 10.0
Division: 2.5
Rounded: 3
Integer Division : 2
Modulus Remainder: 0
Absolute Value: 7
Pattern found: brown
No Match
Modified text: The quick red fox jumps over the lazy red dog
Pattern found: ['brown', 'brown']
Split result: ['apple', 'grapes', 'orange', 'banana', 'pinaple']
"""