Python is a popular programming language with many uses, including data science, task automation, scripting, web development, and competitive programming.
Python is used in data science as it has in-built mathematical libraries and functions, making it easier to calculate mathematical problems and to perform data analysis.
Python is used for automation because it helps automate the filtering of data based on specified factors depending on the data type.
It is used for scripting because Developers often rely on an assortment of Python scripts to manage and integrate different components of their projects. These scripts serve as essential tools, linking various processes within a project's architecture.
It is used in web development due to its multipurpose framework, fast processing, handling of complete development, testing and a large community support.
And it is used in competitive programming because its ease of use. The language is also Its simple and easy, Free and open source, High level language, Portable and Can be run in different types of operating systems.
Python was developed by Gudio vas Rossum
Lets start by printing Hello World.
print("Hello World")
Variable- A name given to a memory location in a program. variable=value
name="Dave"
age=100
weight=75
print("Name=",name,age,weight)
The name of the variable is an identifier.
Rules for identifiers- An identifier should not start with a digit. We cant use special symbols like !#% etc. Identifiers can be of any length. Identifiers name should not be a Keyword name. print(type(name)) // this prints the type of identifier.
Datatypes-Integers Numbers String Words Float Decimal number Boolean True or false.
Keyword-They are reserved words in python.
Types of Tokens
Punctuators-Symbols to organize sentence structure in programming.
Expression-String and numeric values can operate togeather with* txt='@' print(2*txt) String and string can operate with + print(txt+2)
Comments-Part of the code which the interpriter ignores
multi line comment="""HIII""" ,Single line comment=#HIII
Input statment We can input things from the user. name=input("Enter name Name")) age=int(input("Enter age"))
Conditional Statment if-elif-else
syntax
if(condition):
Statment 1
elif(condition):
Statment 2
else: Statment 3
Conditional statments are statments which cheak the condition given is true and only then executes the block of code.
Single line conditional statment
variable=value if condition else value 2 eat="Yes"if food=="Cake" else"No"
eg- vote="Yes"if age>18 else "no" print(vote)
Best practices when using python
Simple instructions
One instruction per task
Short and meaningful variable names
Use appropriate comments 5.Avoid complex expressions.
Operators Arithmetic operators: These operators are used to perform arithmetic operations on numbers. The following are the arithmetic operators in Python: Addition (+) Subtraction (-) Multiplication (*) Division (/) Floor division (//) Modulo (%) Exponentiation (**)
Assignment operators: These operators are used to assign values to variables. The following are the assignment operators in Python: Equal to (=) Add and assign (+=) Subtract and assign (-=) Multiply and assign (*=) Divide and assign (/=) Floor divide and assign (//=) Modulo and assign (%=) Exponentiate and assign (**=)
Comparison operators: These operators are used to compare two values. The following are the comparison operators in Python: Equal to (==) Not equal to (!=) Greater than (>) Less than (<) Greater than or equal to (>=) Less than or equal to (<=)
Logical operators: These operators are used to combine two or more Boolean expressions. The following are the logical operators in Python: And (&) Or (|) Not (!)
Identity operators: These operators are used to check if two objects are the same object. The following are the identity operators in Python: Is (is) Is not (is not)
Membership operators: These operators are used to check if a value is present in a sequence. The following are the membership operators in Python: In Not in
Bitwise operators: These operators are used to perform bitwise operations on numbers. The following are the bitwise operators in Python: And (&) Or (|) Not (~) XOR (^) Left shift (<<) Right shift (>>)
Q1. A program to show a simple calculator in python.
a=int(input("Enter 1st value:"))
b=int(input("Enter 2nd value:"))
x=int
f=input("Enter +-*/ for the type of operation:")
if a>0 and b>0:
if f=='+':
x=a+b
elif f=='-':
if a>b:
x=a-b
else:
x=b-a
elif f=='*':
x=a*b
elif f=='/':
if a>b:
x=a/b
else:
x=b/a
else:
print("Wrong input")
print("ans=",x)
Also try these programs.
To print the area of a square or the average of two numbers.
Python is an easy language to learn but it has a lot of applications.Dont forget to like,comment your openion and follow. Hope you have fun learning it.