Python Notes
Python Notes
Why Python?
Python was designed for readability, and has some similarities to the English language
with influence from mathematics.
Python uses new lines to complete a command, as opposed to other programming
languages which often use semicolons or parentheses.
Python relies on indentation, using whitespace, to define scope; such as the scope of
loops, functions and classes. Other programming languages often use curly-brackets for
this purpose.
1
Python syntax
1. Hallow World
one difference between Python 2 and 3 is the print statement. In Python 2, the "print" statement
is not a function, and therefore it is invoked without parentheses (). However, in Python 3, it is a
function, and must be invoked with parenthesis ().
EXAMPLE:1.1
2. Indentation
Indentation in Python refers to the spaces and tabs that are used at the beginning of a statement
EXAMPLE: - 1.2
3. Python Variables
In python language variables are created when you assign the value to it.
2
Python has no command for declaring a variable.
I. Casting
To specify the datatype of the variables. We have to do casting
Example:
3
Variable Names
Rules of variables:
4
Multi Words Variable Names:
It is difficult to read long variables names. due to this some techniques are been used for
describing the long variables names.
1. Camel Case
Each word, except the first, starts with a capital letter:
2. Pascal Case
Each word starts with a capital letter:
3. Snake Case
Each word is separated by an underscore character:
5
One Value to Multiple Variables
you can assign the same value to multiple variables in one line:
Unpack a Collection
If you have a collection of values in a list, tuple etc. Python allows you to extract the values into
variables. This is called unpacking.
Output Variables
The Python print() function is often used to output variables.
6
The best way to output multiple variables in the print() function is to separate them with
commas, which even support different data types:
Create a variable inside a function, with the same name as the global variable
4. Comments
For commenting in python, we use the (#) tag.
7
For commenting multiples lines “““aaaaaaaaaaaaaa
bbbbbbbbbb”””
5. QUESTIONS
I. Write your Mother Name?
II. What is the output of this code?
print ("This line will be printed.")
III. Correct the code given below
V. Create a variable named school and assign the value students to it.
VI. Write the keyword for making the variable X belong to the global scope.
8
Find the Datatype of the given variables:
X=3
Examples:
9
10
Tuples are immutable and lists are mutable
As tuples are stored in a single memory block therefore they don’t require extra space for new
objects whereas the lists are allocated in two blocks, first the fixed one with all the Python object
information and second a variable-sized block for the data.
Python Indexing
11
Python Slicing
Python Concatenation
lists and tuples can be concatenated using the “+” operator.
Python Append
Lists can be appended with new elements using the append() method.
Python Extend
Lists can also be extended with another list using the extend() method.
12
Python Remove
Lists can have elements removed using the remove() method.
13
Type Conversion
Random Number
Python does not have a random () function to make a random number, but Python has a built-in
module called random that can be used to make random numbers:
Python Casting
Casting in python is therefore done using constructor functions:
int() - constructs an integer number from an integer literal, a float literal (by removing all
decimals), or a string literal (providing the string represents a whole number)
float() - constructs a float number from an integer literal, a float literal or a string literal
(providing the string represents a float or an integer)
str() - constructs a string from a wide variety of data types, including strings, integer literals
and float literals
14
15