You are on page 1of 15

PYTHON NOTES

Why Python?

 Python can be used on a server to create web applications.


 Python can be used alongside software to create workflows.
 Python can connect to database systems. It can also read and modify files.
 Python can be used to handle big data and perform complex mathematics.
 Python can be used for rapid prototyping, or for production-ready software development.

Python Syntax compared to other programming languages

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

How to check python installation in different operating system?


For Windows

Go to Command Line (cmd.exe):

FOR LINUX OR MAC

Go to Command Line (cmd.exe):

*Python file can be written as (filename.py)

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:

Data type of a variable with the type() function

Variables can be declared in singles and doubles quotes both.

Variable names are case-sensitive.


 A, a, B, b this different provide different value.
 A will not overwrite a

3
Variable Names
Rules of variables:

 A variable name must start with a letter or the underscore character


 A variable name cannot start with a number
 A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _)
 Variable names are case-sensitive (age, Age and AGE are three different variables)
 A variable name cannot be any of the Python keywords.

Incorrect variables names examples:

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:

Assign Multiple Values


Many Values to Multiple Variables
Python allows you to assign values to multiple variables in one line:
Note: Make sure the number of variables matches the number of values, or else you will get
an error.

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.

 We can’t add str and int datatypes

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 outside of a function, and use it inside the function

 Create a variable inside a function, with the same name as the global variable

The global Keyword


To create a global variable inside a function, you can use the global keyword.

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

IV. Fill in the blanks

____ (“Hallow Friends”)

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.

6. Python Data Types


 Built-in Data Types
Python has the following data types built-in by default

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.

When to Use Tuples Over Lists?


Immutable Data – Tuples are immutable, thus once they are generated, their contents cannot be
changed. This makes tuples a suitable option for storing information that shouldn’t change, such
as setup settings, constant values, or other information that should stay the same while your
programmed is running.
Performance – Tuples are more lightweight than lists and might be quicker to generate, access,
and iterate through since they are immutable. Using a tuple can be more effective than using
a list if you have a huge collection of data that you need to store, retrieve, and use regularly and
that data does not need to be altered.
Data integrity – By ensuring that the data’s structure and contents stay consistent, tuples can be
utilized to ensure data integrity. To make sure the caller is aware of how much data to expect, for
instance, if a function returns a set amount of values, you might want to return them as a tuple
rather than a list.
Python Numbers
Three types of numeric
 int
 float
 complex

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

You might also like