Chapter – 0
What is Programming?
Just like we use Hindi or English to communicate with each other, we use a Programming language to communicate with the computer.
Programming is a way to instruct the computer to perform various tasks.
What is Python?
Python is a simple and easy-to-understand language that feels like reading simple English. This Pseudo code nature of Python makes it easy to learn and understandable for beginners.
Features of Python:
- Easy to understand = Less development time
- Free and open source
- High-level language
- Portable – works on Windows/Linux/Mac
- + Fun to work with :)
Installation
Python can be easily installed from python.org
When you click on the download button, python can be installed right after you complete the setup by executing the file for your platform.
Tip: Just install it like a game ☻
Chapter 1: Modules, Comments & Pip
Let’s write our very first python program.
Create a file called hello.py and paste the below code into it
Execute this file (.py file) by typing python hello.py, and you will see Hello World printed on the screen.
Modules
A module is a file containing code written by somebody else (usually) which can be imported and used in our programs.
Pip
Pip is a package manager for python. You can use pip to install a module on your system.
E.g., pip install flask (It will install flask module in your system)
Types of modules
There are two types of modules in Python:
- Built-in modules – Pre-installed in Python
- External modules – Need to install using pip
Some examples of built-in modules are os, ABC, etc.
Some examples of external modules are TensorFlow, flask, etc.
Using Python as a Calculator
We can use python as a calculator by typing “python” + TO DO on the terminal. [It opens REPL or read evaluation print loop]
Comments
Comments are used to write something which the programmer does not want to execute.
Comments can be used to mark the author's name, date, etc.
Types of Comments:
There are two types of comments in python,
- Single line comments – Written using # (pound/hash symbol)
- Multi-line comments – Written using ‘’’ Comment ‘’’ or “”” Comment “””.
Chapter 1 – Practice Set
- Write a program to print the Twinkle-Twinkle Little Star poem in python.
- Use REPL and print the table of 5 using it.
- Install an external module and use it to perform an operation of your interest.
- Write a python program to print the contents of a directory using the os module. Search online for the function which does that.
- Label the program written in problem 4 with comments.
Chapter 2 – Variables and Data Types
A variable is a name given to a memory location in a program. For example
Variable – Container to store a value
Keywords – Reserved words in Python
Identifiers – class/function/variable name
Data Types:
Primarily there are the following data types in Python:
- Integers
- Floating point numbers
- Strings
- Booleans
- None
Python is a fantastic language that automatically identifies the type of data for us.
Rules for defining a variable name: (Also applicable to other identifiers)
- A variable name can contain alphabets, digits, and underscore.
- A variable name can only start with an alphabet and underscore.
- A variable can’t start with a digit.
- No white space is allowed to be used inside a variable name.
Examples of a few valid variable names,
Anmol, anmol, one8, _akki, Aakash, anmol_bro, etc.
Operators in Python
The following are some common operators in Python:
- Arithmetic Operators (+, -, *, /, etc.)
- Assignment Operators (=, +=, -=, etc.)
- Comparison Operators (==, >=, <=, >, <, !=, etc.)
- Logical Operators (and, or, not)
type() function and Typecasting
type function are used to find the data type of a given variable in Python.
A number can be converted into a string and vice versa (if possible)
There are many functions to convert one data type into another.
… and so on
Here “31” is a string literal, and 31 is a numeric literal.
input() function
This function allows the user to take input from the keyboard as a string.
Note: The output of the input function is always a string even if the number is entered by the user.
Suppose if a user enters 34, then this 34 will automatically convert to a “34” string literal.
Chapter 2 – Practice Set
- Write a Python program to add two numbers.
- Write a Python program to find the remainder when a number is divided by Z(Integer).
- Check the type of the variable assigned using the input() function.
- Use a comparison operator to find out whether a given variable a is greater than b or not. (Take a=34 and b=80)
- Write a Python program to find the average of two numbers entered by the user.
- Write a Python program to calculate the square of a number entered by the user.
Chapter 3 – Strings
The string is a data type in Python.
A string is a sequence of characters enclosed in quotes.
We can primarily write a string in three ways:
- Single quoted strings: a = ‘anmol’
- Double quoted strings: b = “anmol”
- Triple quoted strings: c = ‘’’ anmol‘’’
String Slicing:
A string in Python can be sliced for getting a part of the string.
Consider the following string: Consider the following string:
The index in a string starts from 0 to (length-1) in Python. To slice a string, we use the following syntax:
Negative Indices: Negative indices can also be used as shown in the figure above. -1 corresponds to the (length-1) index, -2 to (length-2).
Slicing with skip value
We can provide a skip value as a part of our slice like this:
Other advanced slicing techniques
String Functions
Some of the most used functions to perform operations on or manipulate strings are:
- Len() function: It returns the length of the string.
- endswith(“rry”): This function tells whether the variable string ends with the string “rry” or not. If the string is “anmol”, it returns for “rry” since anmol ends with a try.
- count(“c”): It counts the total number of occurrences of any character.
- capitalize(): This function capitalizes the first character of a given string.
- find(word): This function finds a word and returns the index of the first occurrence of that word in the string.
- replace(old word, new word): This function replaces the old word with the new word in the entire string.
Escape Sequence Characters:
The sequence of characters after backslash ‘\’ [Escape Sequence Characters]
Escape Sequence Characters comprise more than one character but represent one character when used within the string.
Examples: \n (newline), \t (tab), \’ (single quote), \\ (backslash), etc.
Chapter 3 – Practice Set
- Write a Python program to display a user-entered name followed by Good Afternoon using the input() function.
- Write a program to fill in a letter template given below with the name and date.
- Write a program to detect double spaces in a string.
- Replace the double spaces from problem 3 with single spaces.
- Write a program to format the following letter using escape sequence characters.
0 Comments