Professional Documents
Culture Documents
Python Learning
Python Learning
--------
* Comments are used to write description about application logics to understand the
logics easily.
* The main objective of comments is maintenance of code will become easy.
* The comments are non-executable code.
* Python will ignore comments in run time.
Keywords:
--------
* Keywords in python 2.x
import keyword
print keyword.kwlist
Variables:
---------
* A variable is nothing but a reserved memory location to store values.
* Variables are used to store the data.
* Memory allocated when the values are stored in variables.
* Each variable must have some type.
Data Types:
----------
* Numbers
* String
* List
* Tuple
* Dictionary
* Boolean
Concatenation:
-------------
* We can concat 2 same types, but we cannot concat 2 different types
Input() and raw_input():
-----------------------
* Getting input from the end-user python2 vs python3
* Python 2.x:
- input function : Takes any type of data
- raw_input function : Takes only string data
* Python 3.x:
- input function : Takes only string data
Type Conversion:
---------------
* Getting data from user by using input function in the form of string and then
converting,
* String to Int
- num1 = int(input("Enter the value: "))
* String to Float
- num2 = float(input("Enter ther value: "))
Formatting Output:
-----------------
* Formatting data with % and {}
* %d - int
* %s - string
* %f or %g - float
Conditional statements:
----------------------
* If-else
* elif
Iterative statements:
--------------------
* for loop
* while loop
Strings:
-------
* Strings in python are immutable. Once string is created it can't be
modified(immutable)
* id() : Every object in python is stored somewhere in memory. We can use id() to
get that memory address.
Operations on Strings:
---------------------
* String index starts from 0.
* + operator is used to concatenate string and * operator is repetition operator
for string.
Slicing string:
--------------
* We can take subset of string from original string by using '[]' operator also
known as slicing operator.
* Syntax: s[start:end]
* This will return part of the string starting from index start to index end-1.
-> ord() - function returns ASCII code of the character
-> chr() - function returns character represented by a ASCII number
String Functions:
----------------
* len() - returns the length of the string
* max() - returns character having highest ASCII value
* min() - returns character having lowest ASCII value
List:
----
* List type is another sequence type defined by the list class of python.
* List allows us to add, delete or process elements in very simple ways.
* List is very similar to arrays.
* We can use index operator '[]' to access individual elements in the list. List
index starts from 0.
List Slicing:
------------
* slice operator ([start:end]) allows to fetch sublist from the list. It works
similar to string.
+ and * operators:
-----------------
* + operator join the two lists
* * operator replicates the elements in the list
Dictionary:
----------
* Dictionary is a python data type that is used to store key value pairs
* It enables to quickly retrieve, add, remove, modify, values using keys.
* Dictionary is very similar to HashMap in Java.
* Dictionaries are mutable.
Creating Dictionary:
-------------------
* Dictionaries can be created using pair of curly braces({ })
* Each item in the dictionary consists of key, followed by a colon, which is
followed by value.
* And each item is seperated using commas(,).
* Key must be of hashable type, but value can be of any type. Each key in the
dictionary must be unique.
Tuples:
------
* Tuples are very similar to list but once a tuple is created, we cannot add,
delete, replace, reorder elements.
* Tuples are immutable.
File Handling:
-------------
* We can use file handling to read and write data to and from the file.
* File Operation:
- Opening a file
- closing a file
- Writing data into a file
- Reading data from a file
- Appending data
- Looping through the data using for loop
Functions:
---------
* Functions are the re-usable pieces of code which helps us to organize structure
of the code.
* We create functions so that we can run a set of statements multiple times during
in the program without the repetition.