top of page

Python Basics

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

ree




Comment code

Tells others what the code does

# symbol followed by comment

ree




Syntactic error

Python does not understand the code

ree

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

ree





Type command

See the actual data type in Python

ree





int

Integer

Can be negative or positive

ree




float

“float”

real number

Include integers, but also decimals

ree




str

“string”

sequence of characters

ree




bool

Boolean

T - true

F - false

ree




Type-casting

Change the type of expression

Cast int into float:

ree



Cast int into str:

ree



BEWARE: can lose info - Cast float into int:

ree



ERROR: Cast str into int when it does not have an integer value:

ree

Cast bool into int:

ree



Cast int into bool:

ree




Expressions and Variables


Expression

A type of operation

Basic arithmetic operations

ree




Operand - numbers

Operator - math symbols


//

ree



Double slash

Integer division, the result is rounded


Remember PEMDAS? Ahh, memories...


Variables

Store values

ree




Can change variable

ree




Can store results of expressions

ree




Can perform operations on x and save to a new variable y

ree





Can use the type command on variables

ree



Use meaningful variable names

Can use underscore or capital letter to start a new word

ree





String Operations


Sequence of characters contained within two quotes

ree









A string is an ordered sequence

Each element of the sequence can be accessed using an index represented by the array of numbers

ree













Negative indexing

ree












Bind a string to another variable

ree



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

ree




len

Specify the length of the string

ree




cat

Concatenate

Combine strings using the addition symbols

ree





Replicate values of a string

Multiply string

ree




Cannot change value of the string, but can create new string by setting it to the original variable and concatenated with a new string

ree




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

ree





\t

Inserts a ‘tab’

ree




\\

Inserts a ‘\’

Can also insert an ‘r’ in front of the string

ree






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

ree







replace

Exchange second argument with the segment

ree







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

ree









Comments


Subscribe Form

©2019 by busybree. Proudly created with Wix.com

bottom of page