Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

Comments:

--------
* 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.

There are 2 types of comments:


-----------------------------
1) Single line comments: write the description in single line and it starts with #
syntax: # statement
2) Multiline comments: write the description on more than one line and it starts
with """ and ends with """ (triple quotes)
In python while writing the comments we can write double
quote or single quote (" or ')

Keywords:
--------
* Keywords in python 2.x
import keyword
print keyword.kwlist

* Keywords in python 3.x


import keyword
print(keyword.kwlist)
* In 2.x paranthesis are optional for print
* In 3.x paranthesis are mandatory for print
* Removed keywords from 3.x : print
* Newly added keywords in 3.x : False, None, True

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

-> To know the datatype of the variable we have "type()"


Eg: a = 10
b = 10.5
name = "Satya

print(type(a)) - prints type of "a" as Int


print(type(b)) - prints type of "b" as Float
print(type(name)) - prints type of "name" as String

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

Note: In Python 3.x, raw_input() is changed to input(). Thus, input() of Python 3


will behave as raw_input() of Python 2

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

in and not in operator:


----------------------
* We can use in and not in operators to check existence of string in another
string. They are also known as membership operators.

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

Opening and closing files:


-------------------------
* Before reading/writing we first need to open the file.
syntax: f = open(filename, mode)
* After we have finished reading/writing to the file it should be closed using
close() method like below,
f.close()
* Different modes of opening a file are:

"r" - Open a file for read only


"w" - Open a file for writing. If the file already exists its data will be
declared before opening. Otherwise a new file will be created.
"a" - Opens the file in append mode i.e to write a data to the end of the file

Read data from the file:


-----------------------
* To read data back from the file we can use three methods:

read([number]) - Return specified number of char's from the file. If ommited it


will read entire contents of the file
readline() - Return the next line of the file
readlines() - Read all the lines as a list of strings in the file

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.

Class and Object:


----------------
* Class is logical entity which contains logic
* Class contains variables and methods
* Logic should be included within a method
* An object is a physical entity which is created for a class
* We can create any number of objects for a class

You might also like