|
| 1 | + |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +# |
| 5 | +# ## Fundamentals of Programming |
| 6 | + |
| 7 | +# ### 1. Identifiers |
| 8 | +# #### 1.2 Data - Data Containers - Data Types |
| 9 | +# |
| 10 | + |
| 11 | +# x is a name of a variable. x is an identifier |
| 12 | + |
| 13 | +# In[2]: |
| 14 | + |
| 15 | + |
| 16 | +x = 3 |
| 17 | +print ("x is a variable. It is an identifier. Its value is: ",x,"\n") |
| 18 | +print("Data Type of x:", type(x),'\n') |
| 19 | + |
| 20 | + |
| 21 | +# stdName is a name of a variable that represents the name of a student. |
| 22 | +# stdName is an identifier |
| 23 | + |
| 24 | +# In[3]: |
| 25 | + |
| 26 | + |
| 27 | +stdName= "John Smith" |
| 28 | +print ("stdName is a variable. It is an identifier. Its value is: ",stdName,"\n") |
| 29 | +print("Data Type of stdName:", type(stdName),'\n') |
| 30 | + |
| 31 | + |
| 32 | +# From the above, the value of x is 3 that means "x now refers to the integer 3". |
| 33 | +# |
| 34 | +# The value of stdName is John Smith means " stdName refers to string John Smith |
| 35 | + |
| 36 | +# When Assignment manipulates references |
| 37 | + |
| 38 | +# In[5]: |
| 39 | + |
| 40 | + |
| 41 | +y = x |
| 42 | + |
| 43 | +print(y) |
| 44 | + |
| 45 | + |
| 46 | +# ### Identity Operators |
| 47 | +# |
| 48 | +# In Python, identity operators are used to check if the operands are identical - they refer to the same object |
| 49 | +# |
| 50 | + |
| 51 | +# In[6]: |
| 52 | + |
| 53 | + |
| 54 | +x = 5 |
| 55 | +y = 5 |
| 56 | + |
| 57 | +isTrue = "x is y" |
| 58 | +isFalse= "x is not y" |
| 59 | + |
| 60 | +if (x is y): |
| 61 | + print(isTrue) |
| 62 | +else: |
| 63 | + print (isFalse) |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +# In[7]: |
| 69 | + |
| 70 | + |
| 71 | +x = 5 |
| 72 | +y = 8 |
| 73 | + |
| 74 | +isTrue = "x is y" |
| 75 | +isFalse= "x is not y" |
| 76 | + |
| 77 | +if (x is y): |
| 78 | + print(isTrue) |
| 79 | +else: |
| 80 | + print (isFalse) |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +# In[8]: |
| 85 | + |
| 86 | + |
| 87 | +x = 5 |
| 88 | +y = 8 |
| 89 | + |
| 90 | +isTrue = "x is not y" |
| 91 | +isFalse= "x is y" |
| 92 | + |
| 93 | +if (x is y): |
| 94 | + print(isTrue) |
| 95 | +else: |
| 96 | + print (isFalse) |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +# ### Membership Operators |
| 101 | +# They are used to test if a value is found in sequence or not |
| 102 | + |
| 103 | +# In[9]: |
| 104 | + |
| 105 | + |
| 106 | +x = 'Hello World' |
| 107 | +if ('H' in x): |
| 108 | + print ("H in x") |
| 109 | +else: |
| 110 | + print ("H not in x") |
| 111 | + |
| 112 | + |
| 113 | +# In[10]: |
| 114 | + |
| 115 | + |
| 116 | +aList = [1,2,3,4,5] |
| 117 | +if(8 not in aList): |
| 118 | + print("8 not in aList") |
| 119 | +else: |
| 120 | + print("8 in aList") |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +# ### Calculate Diameter and the circumference of a circle |
| 125 | + |
| 126 | +# In[1]: |
| 127 | + |
| 128 | + |
| 129 | +pi=3.14159 |
| 130 | +radius=float(input("Enter radius: ")) |
| 131 | +unit=input("Enter units: ") |
| 132 | +diameter= 2*radius |
| 133 | +circumference=diameter * pi |
| 134 | +print("Diameter: ",diameter,unit) |
| 135 | +print("Circumference: ",circumference,unit) |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +# ### Comments |
| 141 | +# In Python, comments begin with a hash mark, a white space character and continue to the end of line |
| 142 | + |
| 143 | +# In[12]: |
| 144 | + |
| 145 | + |
| 146 | +# This is a comment |
| 147 | + |
| 148 | +x = 3 |
| 149 | +x |
| 150 | + |
| 151 | + |
| 152 | +# In[13]: |
| 153 | + |
| 154 | + |
| 155 | +# Print "Hello World" to console |
| 156 | +print ("Hello, World!") |
| 157 | + |
| 158 | + |
| 159 | +# In[14]: |
| 160 | + |
| 161 | + |
| 162 | +# Inline Comment |
| 163 | +x = 3 # This is an inline comment |
| 164 | +x |
| 165 | + |
| 166 | + |
| 167 | +# In[15]: |
| 168 | + |
| 169 | + |
| 170 | +# This is a comment of multiple lines, also known as block comment |
| 171 | +# Add another line of comment |
| 172 | +# Add another line |
| 173 | +# And another line |
| 174 | + |
| 175 | +x = 3 |
| 176 | +x |
| 177 | + |
0 commit comments