-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariables.py
58 lines (46 loc) · 1.11 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
#
# Example file for variables
# LinkedIn Learning Python course by Joe Marini
#
# Basic data types in Python: Numbers, Strings, Booleans, Sequences, Dictionaries
myint = 5
myfloat = 13.2
mystr = "This is a string"
mybool = True
mylist = [0, 1, "two", 3.2, False]
mytuple = (0, 1, 2)
mydict = {"one" : 1, "two" : 2}
# print(myint)
# print(myfloat)
# print(mystr)
# print(mybool)
# print(mylist)
# print(mytuple)
# print(mydict)
# re-declaring a variable works
myint = 'abc'
print(myint)
# to access a member of a sequence type, use []
print(mylist[2])
print(mytuple[1])
# use slices to get parts of a sequence
print(mylist[1:5])
print(mylist[1:5:2]) #third parameter is step value
# you can use slices to reverse a sequence
print(mylist[::-1])
# dictionaries are accessed via keys
print(mydict["one"])
# ERROR: variables of different types cannot be combined
# print(mystr + 123)
# Python is strongly typed
print(mystr + str(123))
# Global vs. local variables in functions
def someFunction():
global mystr
mystr = "def"
print(mystr)
someFunction()
print(mystr)
del mystr
# print(mystr)
# Can delete variables