This is the first module (Python Basics) for Python for Data Science, a course by IBM's Cognitive Class Labs.
OUTLINE:
- Your First Program
- Types
- Expressions and Variables
- String Operations
Your first program
A statement or expression is an instruction the computer will run or execute
Print statement
Simplest program to write
Value (argument) displayed in parentheses
![](https://static.wixstatic.com/media/49162d_4c612cb854274fc5b8f1a23aaa3c5a68~mv2.png/v1/fill/w_239,h_61,al_c,q_85,enc_auto/49162d_4c612cb854274fc5b8f1a23aaa3c5a68~mv2.png)
Comment code
Tells others what the code does
# symbol followed by comment
![](https://static.wixstatic.com/media/49162d_9149f50bbe4741d285c7201bb5500ae5~mv2.png/v1/fill/w_221,h_81,al_c,q_85,enc_auto/49162d_9149f50bbe4741d285c7201bb5500ae5~mv2.png)
Syntactic error
Python does not understand the code
![](https://static.wixstatic.com/media/49162d_2bc21da870914634aeed3b3513d574d0~mv2.png/v1/fill/w_595,h_152,al_c,q_85,enc_auto/49162d_2bc21da870914634aeed3b3513d574d0~mv2.png)
Semantic error
Logic is wrong
Don’t get an error message
Types
Type is how Python represents different types of data
Chart: expression | data type
![](https://static.wixstatic.com/media/49162d_0f4b36b5c4224576898568f7576d08d8~mv2.png/v1/fill/w_475,h_158,al_c,q_85,enc_auto/49162d_0f4b36b5c4224576898568f7576d08d8~mv2.png)
Type command
See the actual data type in Python
![](https://static.wixstatic.com/media/49162d_a26a75c19fd84eccb6d00db56209bd5e~mv2.png/v1/fill/w_474,h_158,al_c,q_85,enc_auto/49162d_a26a75c19fd84eccb6d00db56209bd5e~mv2.png)
int
Integer
Can be negative or positive
![](https://static.wixstatic.com/media/49162d_d12022ccf4ee402ca94b10647310726d~mv2.png/v1/fill/w_134,h_61,al_c,q_85,enc_auto/49162d_d12022ccf4ee402ca94b10647310726d~mv2.png)
float
“float”
real number
Include integers, but also decimals
![](https://static.wixstatic.com/media/49162d_3f9e71d036564e1c9d551c68b1ee9c4e~mv2.png/v1/fill/w_144,h_61,al_c,q_85,enc_auto/49162d_3f9e71d036564e1c9d551c68b1ee9c4e~mv2.png)
str
“string”
sequence of characters
![](https://static.wixstatic.com/media/49162d_0a3506e6e22342f5a0c918431c5bce1f~mv2.png/v1/fill/w_139,h_62,al_c,q_85,enc_auto/49162d_0a3506e6e22342f5a0c918431c5bce1f~mv2.png)
bool
Boolean
T - true
F - false
![](https://static.wixstatic.com/media/49162d_b35b5f5596324bd4bbff1bd69a5e6e2d~mv2.png/v1/fill/w_123,h_67,al_c,q_85,enc_auto/49162d_b35b5f5596324bd4bbff1bd69a5e6e2d~mv2.png)
Type-casting
Change the type of expression
Cast int into float:
![](https://static.wixstatic.com/media/49162d_429dfdbdb88342e68b25281ab480f963~mv2.png/v1/fill/w_125,h_69,al_c,q_85,enc_auto/49162d_429dfdbdb88342e68b25281ab480f963~mv2.png)
Cast int into str:
![](https://static.wixstatic.com/media/49162d_bebce3ce2a0349edac1c465a6e7b3523~mv2.png/v1/fill/w_140,h_67,al_c,q_85,enc_auto/49162d_bebce3ce2a0349edac1c465a6e7b3523~mv2.png)
BEWARE: can lose info - Cast float into int:
![](https://static.wixstatic.com/media/49162d_fb87d77722124dd49b9a71aa121e6a9e~mv2.png/v1/fill/w_119,h_66,al_c,q_85,enc_auto/49162d_fb87d77722124dd49b9a71aa121e6a9e~mv2.png)
ERROR: Cast str into int when it does not have an integer value:
![](https://static.wixstatic.com/media/49162d_994b0fbdfd2942a6a16a54fbd0f60984~mv2.png/v1/fill/w_604,h_152,al_c,q_85,enc_auto/49162d_994b0fbdfd2942a6a16a54fbd0f60984~mv2.png)
Cast bool into int:
![](https://static.wixstatic.com/media/49162d_461aa9e1805e40a584fd36a4f65df386~mv2.png/v1/fill/w_140,h_72,al_c,q_85,enc_auto/49162d_461aa9e1805e40a584fd36a4f65df386~mv2.png)
Cast int into bool:
![](https://static.wixstatic.com/media/49162d_f29bb8d681924f0fa828f90615f3fbd2~mv2.png/v1/fill/w_112,h_60,al_c,q_85,enc_auto/49162d_f29bb8d681924f0fa828f90615f3fbd2~mv2.png)
Expressions and Variables
Expression
A type of operation
Basic arithmetic operations
![](https://static.wixstatic.com/media/49162d_d0a12b9021c048e19a1bdeb73394f976~mv2.png/v1/fill/w_119,h_67,al_c,q_85,enc_auto/49162d_d0a12b9021c048e19a1bdeb73394f976~mv2.png)
Operand - numbers
Operator - math symbols
//
![](https://static.wixstatic.com/media/49162d_518339c35dce48e1acc73c5bf4b33cff~mv2.png/v1/fill/w_107,h_64,al_c,q_85,enc_auto/49162d_518339c35dce48e1acc73c5bf4b33cff~mv2.png)
Double slash
Integer division, the result is rounded
Remember PEMDAS? Ahh, memories...
Variables
Store values
![](https://static.wixstatic.com/media/49162d_bc0da5072cb84ffebdf3aa9f73402f11~mv2.png/v1/fill/w_151,h_107,al_c,q_85,enc_auto/49162d_bc0da5072cb84ffebdf3aa9f73402f11~mv2.png)
Can change variable
![](https://static.wixstatic.com/media/49162d_e60b783d58bd4adda2a1e06d4d6ddc92~mv2.png/v1/fill/w_159,h_108,al_c,q_85,enc_auto/49162d_e60b783d58bd4adda2a1e06d4d6ddc92~mv2.png)
Can store results of expressions
![](https://static.wixstatic.com/media/49162d_b135cf88af274c59aeb02d4299bde5d3~mv2.png/v1/fill/w_110,h_107,al_c,q_85,enc_auto/49162d_b135cf88af274c59aeb02d4299bde5d3~mv2.png)
Can perform operations on x and save to a new variable y
![](https://static.wixstatic.com/media/49162d_f2cb1e11450846c8b004e3e7b2e15878~mv2.png/v1/fill/w_124,h_112,al_c,q_85,enc_auto/49162d_f2cb1e11450846c8b004e3e7b2e15878~mv2.png)
Can use the type command on variables
![](https://static.wixstatic.com/media/49162d_8cb995df394d4fcaa0561a18c98b476f~mv2.png/v1/fill/w_122,h_65,al_c,q_85,enc_auto/49162d_8cb995df394d4fcaa0561a18c98b476f~mv2.png)
Use meaningful variable names
Can use underscore or capital letter to start a new word
![](https://static.wixstatic.com/media/49162d_66fa4dedb5714dd5a05b5327b199fc10~mv2.png/v1/fill/w_223,h_146,al_c,q_85,enc_auto/49162d_66fa4dedb5714dd5a05b5327b199fc10~mv2.png)
String Operations
Sequence of characters contained within two quotes
![](https://static.wixstatic.com/media/49162d_0dcc38c3db6b4094934119bc571bf52b~mv2.png/v1/fill/w_199,h_216,al_c,q_85,enc_auto/49162d_0dcc38c3db6b4094934119bc571bf52b~mv2.png)
A string is an ordered sequence
Each element of the sequence can be accessed using an index represented by the array of numbers
![](https://static.wixstatic.com/media/49162d_818542ab2ac84fab84481612d132f601~mv2.png/v1/fill/w_168,h_331,al_c,q_85,enc_auto/49162d_818542ab2ac84fab84481612d132f601~mv2.png)
Negative indexing
![](https://static.wixstatic.com/media/49162d_1c2d9e0b55cc4d088f196e5bcc80b049~mv2.png/v1/fill/w_125,h_295,al_c,q_85,enc_auto/49162d_1c2d9e0b55cc4d088f196e5bcc80b049~mv2.png)
Bind a string to another variable
![](https://static.wixstatic.com/media/49162d_1630b6e8294e4d9f8bf7955524593f73~mv2.png/v1/fill/w_125,h_78,al_c,q_85,enc_auto/49162d_1630b6e8294e4d9f8bf7955524593f73~mv2.png)
Think of a string as a list or tuple
Collection which is ordered or unchangeable
Treat the string as a sequence and perform sequence operations
Stride
Specifies how many characters to move forward after the first character is retrieved from the string
The third number in the slice command specifies the stride
![](https://static.wixstatic.com/media/49162d_fb44dde6a81743539ddfa17d2ef5bbb4~mv2.png/v1/fill/w_154,h_68,al_c,q_85,enc_auto/49162d_fb44dde6a81743539ddfa17d2ef5bbb4~mv2.png)
len
Specify the length of the string
![](https://static.wixstatic.com/media/49162d_eb07581477f340a4a6c1fe8d1e093cee~mv2.png/v1/fill/w_163,h_74,al_c,q_85,enc_auto/49162d_eb07581477f340a4a6c1fe8d1e093cee~mv2.png)
cat
Concatenate
Combine strings using the addition symbols
![](https://static.wixstatic.com/media/49162d_7f86d285f5d74324b1f7476bd072c3ad~mv2.png/v1/fill/w_313,h_109,al_c,q_85,enc_auto/49162d_7f86d285f5d74324b1f7476bd072c3ad~mv2.png)
Replicate values of a string
Multiply string
![](https://static.wixstatic.com/media/49162d_53baea9df05e44fcbd021f648e2d8056~mv2.png/v1/fill/w_209,h_76,al_c,q_85,enc_auto/49162d_53baea9df05e44fcbd021f648e2d8056~mv2.png)
Cannot change value of the string, but can create new string by setting it to the original variable and concatenated with a new string
![](https://static.wixstatic.com/media/49162d_54dcc95a9dfe4767856208e379e2808b~mv2.png/v1/fill/w_253,h_107,al_c,q_85,enc_auto/49162d_54dcc95a9dfe4767856208e379e2808b~mv2.png)
Strings are immutable - they can’t be changed once they have been created
\
Beginning of an escape sequence - represents strings that may be difficult to input
\n
New line
![](https://static.wixstatic.com/media/49162d_4a7c64d6b0ed4e9da696ffadcf8286f2~mv2.png/v1/fill/w_298,h_114,al_c,q_85,enc_auto/49162d_4a7c64d6b0ed4e9da696ffadcf8286f2~mv2.png)
\t
Inserts a ‘tab’
![](https://static.wixstatic.com/media/49162d_54bae7637e3346cc949a0ebb229fd4f3~mv2.png/v1/fill/w_294,h_66,al_c,q_85,enc_auto/49162d_54bae7637e3346cc949a0ebb229fd4f3~mv2.png)
\\
Inserts a ‘\’
Can also insert an ‘r’ in front of the string
![](https://static.wixstatic.com/media/49162d_6ee3db0f11d540a6b32fee48ca0911a9~mv2.png/v1/fill/w_298,h_131,al_c,q_85,enc_auto/49162d_6ee3db0f11d540a6b32fee48ca0911a9~mv2.png)
String Methods
Strings are sequences
Have apply methods that work on lists and tuples
Have a second set of methods that just work on strings
upper
Converts lowercase characters to uppercase
![](https://static.wixstatic.com/media/49162d_4b70a3abfeab4c59a8134b39957f65ba~mv2.png/v1/fill/w_256,h_156,al_c,q_85,enc_auto/49162d_4b70a3abfeab4c59a8134b39957f65ba~mv2.png)
replace
Exchange second argument with the segment
![](https://static.wixstatic.com/media/49162d_9f865075f1d7447d8f84828c8089f9f5~mv2.png/v1/fill/w_256,h_153,al_c,q_85,enc_auto/49162d_9f865075f1d7447d8f84828c8089f9f5~mv2.png)
find
Finds sub-strings
Argument is the sub-string you would like to find
Output is -1 if the sub-string is not in the string
![](https://static.wixstatic.com/media/49162d_d0b2d5cfe6364f5cb4a6638823ade3eb~mv2.png/v1/fill/w_192,h_187,al_c,q_85,enc_auto/49162d_d0b2d5cfe6364f5cb4a6638823ade3eb~mv2.png)
Comments