Skip to content

Commit f9a2e9b

Browse files
Made the variables.py file neater and cleaner to read
1 parent e210356 commit f9a2e9b

1 file changed

Lines changed: 53 additions & 69 deletions

File tree

basics/variables.py

Lines changed: 53 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
#-----------------------------------------------------------------------------------------
1+
"""
2+
---------------------------------------------------------------------
23
# VARIABLES WITH PYTHON
3-
#-----------------------------------------------------------------------
4+
#--------------------------------------------------------------------
5+
46
# In python variables are used to store values that can be refrenced and manipulated during program execution
57
# A variable is essentially a name that is assigned to a values
6-
8+
"""
79
name = "David" # This is the word David stored inside the variable <name>
810
age = 28 # 28 is stored inside of the variable <age>
911

@@ -12,34 +14,27 @@
1214

1315
# When you run :
1416
# python3 .\variables.py
15-
1617
# Output should look like :
17-
1818
"""
1919
# David
2020
# 28
21-
2221
"""
23-
24-
#---------------------------------------------------------------------------
25-
22+
"""
23+
------------------------------------------------------------------------
2624
# Rules for naming variables
2725
# Variable names can only contain letters, digits and underscores
2826
# A variable name cannot start with a digit
2927
# Variables are case sensitive
30-
31-
32-
#------------------------------------------------------------------
3328
# Variables in Python are assigned values using the = operator.
34-
29+
------------------------------------------------------------------------
30+
"""
3531
x = 5
3632
y = 3.14
3733
z = "HI" # The = sign assigns the value to the variable
38-
39-
40-
#------------------------------------------------------------
34+
"""
35+
------------------------------------------------------------
4136
# Dynamic Typing
42-
#--------------------------------------------------------------
37+
--------------------------------------------------------------
4338
# Python Variables are dynamically typed
4439
# This means the same variable can hold different types of values during execution
4540
# Example:
@@ -49,67 +44,65 @@
4944
# If we print it out it will look like
5045
print(x)
5146
# The output would be:
52-
"""
53-
Ten instead of 10
5447
48+
Ten instead of 10
5549
"""
5650

57-
# if we want to print out both we need to print the first variable before we assign the new value to the second variable
51+
"""
52+
# if we want to print out both we need to print the first variable before we assign the new value to the second variable"""
5853
# Example :
5954
x = 10
6055
print(x)
6156
x = "Ten"
6257
print(x)
6358

64-
# The output would look like :
65-
"""
59+
"""The output would look like :
60+
6661
10
6762
Ten
68-
6963
"""
70-
71-
72-
#-------------------------------------------
64+
"""
65+
-------------------------------------------
7366
# Multiple Assignments
74-
#-------------------------------------------
67+
-------------------------------------------
7568
# Python allows multiple variables to be assigned values in a single line
69+
"""
7670

7771
a = b = c = 100
7872
print(a, b, c)
79-
# Output would look like:
80-
"""
81-
100 100 100
8273

83-
"""
74+
"""# Output would look like:
8475
85-
86-
#--------------------------------------------------------
76+
100 100 100
77+
"""
78+
"""
79+
--------------------------------------------------------
8780
# Assigning Different Values
88-
#--------------------------------------------------------
81+
--------------------------------------------------------
8982
# We can assigne different values to multiple variables simultaneously
9083
# This makes the code concise and easier to read
84+
"""
9185

9286
x, y, z = 3, 1.4, "Python" # x, = 3 | y, = 1.4 | z = "Python"
9387
print(x, y, z) # This will then print out all those values
94-
# The output would look like this:
95-
"""
96-
3 1.4 Python
97-
98-
"""
9988

89+
"""# The output would look like this:
10090
101-
#------------------------------------------------------------------
91+
3 1.4 Python
92+
"""
93+
"""
94+
------------------------------------------------------------------
10295
# Type Casting a Variable
103-
#------------------------------------------------------------------
96+
------------------------------------------------------------------
10497
# Type casting refers to the process of converting the value of-
10598
# one data type into another
10699
107100
# Basic Castin Functions
108101
# int()
109102
# float()
110103
# str()
104+
"""
111105
# Example:
112-
113106
str_ing = "10" # This is a string or is initially a string
114107
new_string_to_int = int(str_ing) # This converts the string "10" into an integer 10
115108

@@ -122,20 +115,19 @@
122115
print(int_to_float)
123116
print(age_tostring)
124117

125-
# The output would look like :
126-
"""
118+
"""# The output would look like :
119+
127120
10
128121
5.0
129122
25
130-
131123
"""
132-
#-----------------------------------------------------------------------------
124+
"""
125+
--------------------------------------------------------------------------
133126
# Getting the Type of Variable
134-
#-----------------------------------------------------------------------------
127+
-----------------------------------------------------------------------------
135128
# In python we can determine the type of variable using the type() function.
136129
# This returns the type of object passed to it
137-
138-
130+
"""
139131
a = 5
140132
b = "hello"
141133
c = "0.5"
@@ -146,30 +138,28 @@
146138
print(type(c))
147139
print(type(d))
148140

149-
# Output will look like
150-
"""
141+
"""output will look like:
142+
151143
<class 'int'>
152144
<class 'str'>
153145
<class 'str'>
154146
<class 'bool'>
155-
156-
157147
"""
158148

159-
#---------------------User input using variables-----------
149+
"""
150+
---------------------User input using variables-----------
160151
161152
# In Order to get user input we have to assign the input() function.
162-
153+
"""
163154
nam = input("\nWhat is your name: ")
164155
print(nam)
165156

166157
fav_food = input("\nWhat is your favourite food ")
167158
print(fav_food)
168-
169159
# You may add your own name and fav food, I just used david and pizza as an example
170160

171-
# Output would look like:
172-
"""
161+
"""# Output would look like:
162+
173163
What is your name: david
174164
david
175165
@@ -178,30 +168,24 @@
178168
179169
"""
180170

181-
#------------------------Input sentences------------------
171+
"""
172+
------------------------Input sentences------------------
182173
# We can also print out sentences like we would with a variable
183174
# We use an f string for cleaner code
184-
175+
"""
185176
nam1 = input("\nWhat is your dogs name?: ")
186177
print(f"I can't believe your dogs name is {nam1}") # We can use an f string for cleaner code
187178

188179
nam2 = input("\nWhere do you live?: ")
189180
print(f"Oh you live in {nam2} I have no idea where that is")
190181

191-
# The output would look like this
192-
"""
182+
"""# The output would look like this
183+
193184
What is your dogs name?: Linux
194185
I can't believe your dogs name is Linux
195186
196187
Where do you live?: Planet Python
197188
Oh you live in Planet Python I have no idea where that is
198189
199190
"""
200-
201191
# We can modify this with if statements, However we will get to that in the conditionals.py file and mini projects folder
202-
203-
204-
205-
206-
207-

0 commit comments

Comments
 (0)