-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatatypeVariables.py
More file actions
159 lines (119 loc) · 3.53 KB
/
DatatypeVariables.py
File metadata and controls
159 lines (119 loc) · 3.53 KB
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
154
155
156
157
158
# Author Madhu Kumar K S
# Below are basic data types
int # numbers examples 0 1 3 5 6
str # with in 'Python' / "Python"
bool # True / False
float # 0.12 1.23 3.455
# Declaring variables
name = "Python"
newName = 'Python'
# access the above variables
print(name)
print(newName)
name = "Welcome to Python"
print(name)
# Rules for variables
# Only contains _ , characters and numbers
# shouldn't start with number, but can be ended with numbers
NAME = "Hello"
name = "Python"
# Above 2 variables are different as Python is case sensitive
age = 17
#Literal assignment
#Checking the data type instances
name="madhu"
initial ="KS"
#There are different ways to check the type
print(type(name))
print(type(name) == str)
print(isinstance(name, str))
#Constructor function
pizza = str("Pepperoni")
print(type(pizza))
print(type(pizza) == str)
print(isinstance(pizza, str))
#Concatenation
fullname = name + " " + initial
print(fullname)
fullname += " Welcome."
#Casting a number to a string
decade = str(1986)
print(type(decade))
print(decade)
statement = "i like the music from " + decade + "s."
# Multiple lines
multiline = '''
Hey, How are you?
i was just checking from python!
all good.
'''
print(multiline)
#Escaping special characters
# \ helps tp add special character, \t helps to add tab, \n add new line
sentence = 'I\'m a Senior Software Engineer\t with 12 Years of Experience\nWorking on Cloud, React, Node and Python.'
print(sentence)
# String Methods
print(name)
# The Upper and Lower methods will return new string and it doesn't modify the current string
print(name.lower())
print(name.upper())
print(name)
# Title method will capitalize the given string
# Title and replace methods will return new string and it doesn't modify the current string
print(multiline.title())
print(multiline.replace("good", "ok"))
print(multiline)
# Strip, len methods
# Below methods will return new string and it doesn't modify the current string
print(multiline.title())
print(len(multiline))
print(len(multiline.strip())) # Remove complete white space from left and right ended
print(len(multiline.lstrip())) # Only remove the left side of the string
print(len(multiline.rstrip())) # Only remove the right side of the string
#Build a menu
title = "menu".upper();
# Center method will place the menu center with * sides
print(title.center(20, "*"))
# Ljust and Rjust are the method to adjust the left and right position
print("Coffee".ljust(16, " ") + "$1".rjust(4))
print("Tea".ljust(16, " ") + "$1.5".rjust(4))
print("Cake".ljust(16, " ") + "$2".rjust(4))
# String Index Values
print(name[1])
print(name[-1]) # we can use -1 to access last char of the string when we dont know the actual length
#String methods return boolean values
print(name.startswith("m"))
print(name.endswith("U".lower()))
#Boolean datatypes
myValue = True
x = bool(False)
print(type(x))
print(myValue)
#Numeric Datatypes
#Integer Types
price = 100
best_price = int(80)
print(type(price))
print(isinstance(best_price, int))
# Float type
gpa = 3.28
print(type(gpa))
# complex type
comp_value = 5+3j
print(type(comp_value))
print(comp_value.real)
print(comp_value.imag)
# Built in functions for numbers
print(abs(gpa))
print(round(gpa)) # Rounded to nearest integer
print(round(gpa,1)) # Rounded to nearest decimal value, 3.28 o/p will be 3.3
# Math module
import math
print(math.pi)
print(math.sqrt(64)) # Return float value
print(math.ceil(gpa))
print(math.floor(gpa))
# Casting a string to a number
zipcode = "10001"
zip_code = int(zipcode)
print(type(zip_code))