A quick recap of topics covered in first week of Introduction to Programming for data Science through Python. Although a full disclosure: Just like the Summary for Statistics, this article is not meant to replace any standard textbook/course for Python. It is expected you have already gone through your lessons and are comfortable with the contents covered this week. Content might be updated periodically as I learn more and become better skilled. Meanwhile, I hope this article on Python Week 01 Summary helps. All the best and Happy Learning!
Some Important Points
- Literals – These are actual values stored inside a Variable
- Variables– Containers holding the Literals
Data Types
- When numbers are converted to “float” data type: –
- The number zero (‘0’) becomes “False”
- All other numbers become “True”
- Boolean: –
- Empty String always returns “False”
- All other strings always return “True”
- Integer “0” returns “False”
- Integer “1” returns “True”
Operators
- Operator precedence is decided by the PEMDAS rule: –
- P : Parenthesis
- E : Exponent
- M : Multiplication
- D : Division
- A : Addition
- S : Subtraction
- Three Types of Operators in Python: –
- Arithmetic Operator
- Relational operators
- Logical Operators
Type of Operator | Operator | Function |
---|---|---|
Logical | and or not | Both must be true either must be true must not be true |
Relational | > < >= <= = == != | Greater than Less Than Greater than or equal to Less than or equal to assignment Is equal to Is not equal to |
Arithmetic | + – * ** / // % | Usual addition Usual subtraction Usual multiplication Exponential Usual Division Gives quotient of Regular Division Gives remainder of Regular Division |
Strings
Q. How to slice out a part of a string?
A. We use “String Slicing”. If s = chocolate
and we want to slice out "oco"
, we type print(s[2:5])
. here 2 represents index of first letter of slice, and 5 represents the index of the letter immediately after the last letter of the slice. This is by design where the slice includes the start index, but not the end index. We must accordingly choose our index positions.
NB: One can use negative numbers to access the characters from the end. For example, -1 would refer to “e”, -2 would refer to “t”, and so on. So, “oco” could also be sliced by print(s[-7:-4])
. This is called “Negative Indexing”
Q. How do you print a string (Example x = Wow
) 3 times without gaps?
A. print(x * 3)
Q. Why is 2 > 1 but one > 2 ???
A. Python compares strings based on their UNICODE values and length of string. For examples Numbers have higher values than alphabets. Upper case letters have higher values than lower case letters. Which is why 2
comes after 1
, but one
comes after 2
.
Try the following code to check for any two alphanumeric strings: –
a1 = input('First String\n')
a2 = input('Second String\n')
keepplaying = True
while keepplaying:
if a1 < a2:
print(" " + a1 + " comes before " + a2 + " in the dictionary.")
elif a1 > a2:
print(" " + a2 + " comes before " + a1 + " in the dictionary.")
else:
print(" " + a1 + " and " + a2 + " are same.")
a1 = input('Next String. Enter stop if you want to end:\n')
if a1 == "stop":
break
a2 = input('Second String:\n')
Links for Python Summary for Week 02 will be added here later, along with those of subsequent weeks.
That’s all for today folks!
See ya!
Peace! ✌🏻