-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariables.py
68 lines (57 loc) · 1.51 KB
/
Variables.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
# Python has no command for declaring a variable
# Variables are containers for storing data values.
# Variables do not need to be declared with any particular type, and can even change type after they have been set.
x=2
y="Dishang"
print(x,"\n")
print(y)
# Casting
# If you want to specify the data type of a variable, this can be done with casting.
x = str(3) # x will be '3'
print(x)
y = int(3) # y will be 3
print(y)
z = float(3) # z will be 3.0
print(z)
# You can get the data type of a variable with the type() function.
print(type(x))
print(type(y))
print(type(z))
# String variables can be declared either by using single or double quotes:
a='Dishang'
b="Rana"
# Variable names are case-sensitive
a="a"
A=45
# Assign Multiple Values
# Many Values To Multiple Variables
x,y,z="Dishang","Rana","07"
print(x)
print(y)
print(z)
# One Value To Multiple Variables
x=y=z="Khaer"
print(x)
print(y)
print(z)
# Unpack a Collection
# If you have a collection of values in a list, tuple etc. Python allows you to extract the values into variables. This is called unpacking.
colors=["Red","Blue","Green"]
a,b,c=colors
print(a)
print(b)
print(c)
# You output multiple variables, separated by a comma
p="I"
q="Am"
r="Indestructible"
s=3
print(p,q,r,s)
# You can also use the + operator to output multiple variables
# Space are excluded in + operator
print(p+q+r)
# Note:When you try to combine a string and a number with the + operator, Python will give you an error
# the + character works as a mathematical operator
a=10
b=2
print(a+b)