Python

Prakash Dale · July 25, 2020

String

A string is defined as sequence of characters.

Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> myString = "Some text"
>>> type(myString)
<class 'str'>
>>> myString[0]
'S'
>>> myString[-1]
't'
>>> len(myString)
9
>>> myString[0:2]
'So'
>>> myString[-2:]
'xt'
>>> "Conca" + "tenation"
'Concatenation'
>>> myString + str(1234) + " convert number to string for string concatenation"
'Some text1234 convert number to string for string concatenation'
>>> prod = "037-00901-00027"
>>> "Country code: " + prod[:3]
'Country code: 037'
>>> "Product Code: " + prod[4:(4+5)]
'Product Code: 00901'
>>> "Batch: " + prod[-5:]
'Batch: 00027'
>>> myname = input("What is your name?")
What is your name?Prakash
>>> print("Your name is " + myname)
Your name is Prakash

Lists and Tuples

Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> students = ["John", "Merry", "Steve"]
>>> type(students)
<class 'list'>
>>> len(students)
3
>>> students[0]
'John'
>>> students[0:2]
['John', 'Merry']
>>> students[-1]
'Steve'
>>> print("Tuples are immutable")
Tuples are immutable
>>> months = ("J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D")
>>> months[0]
'J'
>>> students.append("Kate")
>>> students
['John', 'Merry', 'Steve', 'Kate']
>>> students.insert(0, "Peter")
>>> students
['Peter', 'John', 'Merry', 'Steve', 'Kate']
>>> students.pop()
'Kate'
>>> students
['Peter', 'John', 'Merry', 'Steve']
>>> students.remove("John")
>>> students
['Peter', 'Merry', 'Steve']
>>> students2 = ["Paul", "Merry"]
>>> students + students2
['Peter', 'Merry', 'Steve', 'Paul', 'Merry']``

Dictionaries

Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Dictonary is key-value pair")
Dictonary is key-value pair
>>> person = {"first_name": "John", "last_name": "Green"}
>>> type(person)
<class 'dict'>
>>> person["first_name"]
'John'
>>> person["country"] = "Canada"
>>> person
{'first_name': 'John', 'last_name': 'Green', 'country': 'Canada'}
>>> person.get("age", "invalid property")
'invalid property'
>>> person.get("first_name", "invalid property")
'John'
>>> person.clear()
>>> person
{}

Machine Learning

Installation

Open command prompt, activate conda environment and go to the folder where you wish to keep your code files.

pip install scipy
pip install scikit-learn
pip install ipython
pip install "ipython[notebook]"
jupyter notebook

Youtube videos

Twitter, Facebook