Asm - Artificial Intelligence - 130273

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

INTRODUCTION TO PYTHON

What is Python?

Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.

It is used for:

 web development (server-side),


 software development,
 mathematics,
 system scripting.

What can Python do?

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

Why Python?
 Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
 Python has a simple syntax similar to the English language.
 Python has syntax that allows developers to write programs with fewer lines than some other programming
languages.
 Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means
that prototyping can be very quick.
 Python can be treated in a procedural way, an object-orientated way or a functional way.

Execute Python Syntax

Python syntax can be executed by writing directly in the Command Line:

Or by creating a python file on the server, using the .py file extension, and running it in the Command Line:

LOTUS VALLEY INT. SCHOOL, GURUGRAM PAGE 1


Python Indentation

Indentation refers to the spaces at the beginning of a code line.

Where in other programming languages the indentation in code is for readability only, the indentation in Python
is very important.

Python uses indentation to indicate a block of code.

Example
if 5 > 2:
print("Five is greater than two!")
Python will give an error if there is no indentation :

Python Variables

In Python variables are created the moment you assign a value to it:

Comments
Python has commenting capability for the purpose of in-code documentation.

LOTUS VALLEY INT. SCHOOL, GURUGRAM PAGE 2


Comments start with a #, and Python will render the rest of the line as a comment:

Exercise:

Write a Python statement to output “Artificial Intelligence”.

_____________________________________________________________

Python Comments
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.
Creating a Comment

Comments starts with a #, and Python will ignore them:

Example
#This is a comment
print("Hello, World!")

Comments can be placed at the end of a line, and Python will ignore the rest of the line:

Example
print("Hello, World!") #This is a comment

Comments does not have to be text to explain the code, it can also be used to prevent Python from executing
code:

Example
#print("Hello, World!")
print("Cheers, Mate!")

LOTUS VALLEY INT. SCHOOL, GURUGRAM PAGE 3


Python Variables
Creating Variables
Variables are containers for storing data values.
Unlike other programming languages, Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.

Variables do not need to be declared with any particular type and can even change type after they have been set.

Example
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)

String variables can be declared either by using single or double quotes:

Example
x = "John"
# is the same as
x = 'John'
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
Rules for Python 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)

LOTUS VALLEY INT. SCHOOL, GURUGRAM PAGE 4


*Remember that variable names are case-sensitive

Output Variables

The Python print statement is often used to output variables.

To combine both text and a variable, Python uses the + character:

Example
x = "awesome"
print("Python is " + x)

You can also use the + character to add a variable to another variable:

Example
x = "Python is "
y = "awesome"
z= x+y
print(z)

Python Tokens
Basically, python tokens are the small units of the programming language. Python supports 5 types of Tokens

Tokens

Keywords Literals Identifiers Operators Punctuators

Keywords : These are the dedicated words that have special meaning and functions. Moreover, compiler defines
these words. Moreover, it does not allow users to use these words.

LOTUS VALLEY INT. SCHOOL, GURUGRAM PAGE 5


Literals : Literals are used to define the data as a variable (or ) constants. Python has 6 literals tokens.

Identifiers : Identifiers represent the programmable entities. The programmable entities include user-defined
names, variables, modules, and other objects. Moreover, python defines some rules in defining the identifiers. Now let
us discuss some of them. An identifier can be a sequence of lower case (or) upper case (or) integers (or) a combination
of any. The identifier name should start with the lower case (or) upper case (It must not start with digits) The identifier
name should not be a reserved word. Only Underscore (_) is allowed to use as a special character in identifier names.

Python Data Types


Built-in Data Types
In programming, data type is an important concept.
Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:

LOTUS VALLEY INT. SCHOOL, GURUGRAM PAGE 6


Getting the Data Type

You can get the data type of any object by using the type() function:

Example

Print the data type of the variable x:

x=5
print(type(x))
Setting the Data Type

In Python, the data type is set when you assign a value to a variable:

Python Numbers

There are three numeric types in Python:

 int
 float
 complex

Example:
x = 1 # int
y = 2.8 # float
z = 1j # complex

LOTUS VALLEY INT. SCHOOL, GURUGRAM PAGE 7


Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
x=1
y = 35656222554887711
z = -3255522

Float, or "floating point number" is a number, positive or negative, containing one or more decimals.
x = 1.10
y = 1.0
z = -35.59

Complex numbers are written with a "j" as the imaginary part:


x = 3+5j
y = 5j
z = -5j

Type Conversion
The practice of converting the value of a single data type (integer, string, float, etc.) to a different data type is
known as type conversion. Python includes two types of type conversion.
 Explicit Conversion
 Implicit Conversion
In Explicit Type Conversion, users convert the data type of the item to needed data type. We use the
predefined roles such as int(), float(), str(), etc to execute explicit type conversion.
num_int = 327
num_string = "573"
print("Data type of num_int:",type(num_int))
print("Data type of num_string before Type Casting:",type(num_string))

num_string = int(num_string)
print("Data type of num_string after Type Casting:",type(num_string))

num_sum = num_int + num_string

print("Sum of num_int and num_string:",num_sum)


print("Data type of the sum:",type(num_sum))

#OutPut will be:

Data type of num_int: <class 'int'>


Data type of num_string before Type Casting: <class 'str'>
Data type of num_string after Type Casting: <class 'int'>
Sum of num_int and num_string: 900

LOTUS VALLEY INT. SCHOOL, GURUGRAM PAGE 8


Data type of the sum: <class 'int'>

In the preceding program:


We include num_string and num_int factor.
We switched num_string from the string(greater ) into an integer(reduced ) type utilizing int() work to carry out
the addition.
After converting num_string into some integer value Python can bring these two factors.

Implicit Conversion

This procedure does not require any user participation. Let us see an instance where Python boosts the
conversion of a lower data type (integer) to the greater data type (float) to prevent data loss.
Example: Converting Int to Float
num_int = 321
num_float = 1.84

num_new = num_int + num_float

print("datatype of num_int:",type(num_int))
print("datatype of num_float:",type(num_float))

print("Value of num_new:",num_new)
print("datatype of num_new:",type(num_new))

#OutPut Will be
datatype of num_int: <class 'int'>
datatype of num_float: <class 'float'>
Value of num_new: 322.84
datatype of num_new: <class 'float'>

In the preceding program,


We include two factors num_int and num_float, preserving the value in num_new.
From the output, we may observe the datatype of num_int is an integer, the datatype of num_float is a float.
Additionally, we may observe the num_new has float data type as Python consistently transforms smaller data
type to bigger data type to prevent the lack of data.

LOTUS VALLEY INT. SCHOOL, GURUGRAM PAGE 9


Python Operators

Operators are used to perform operations on variables and values.

Python divides the operators in the following groups:

 Arithmetic operators
 Assignment operators
 Relational operators
 Logical operators

Arithmetic operators are used with numeric values to perform common mathematical operations:

Assignment operators are used to assign values to variables:

Comparison or Relational operators are used to compare two values:

LOTUS VALLEY INT. SCHOOL, GURUGRAM PAGE 10


Logical operators are used to combine conditional statements:

Python Input and Output

In Python, we have the following two functions to handle input from a user and system.

1. input(prompt) to accept input from a user.


2. print() to display output on the console.

The input() function reads a line entered on a console by an input device such as a keyboard and convert
it into a string and returns it.

Accept an Integer input from User

We need to convert an input string value into an integer using a int() function.

# program to do Addition of two input numbers


first_number = int ( input ("Enter first number") )
second_number = int ( input ("Enter second number") )

LOTUS VALLEY INT. SCHOOL, GURUGRAM PAGE 11


sum = first_number + second_number
print("Addition of two number is: ", sum)

Accept float input from User

We need to convert user input to the float number using the float() function as we did for the integer value.

n = float (input("Enter a float number") )


print ("input float number is: ", n )
print ("type is:", type(n) )

Accept String input from User

name = input("Enter Employee Name")


salary = input("Enter salary")
company = input ("Enter Company name")
print("Printing Employee Details")
print ("Name", "Salary", "Company")
print (name, salary, company)

SOME SAMPLE PROGRAMS TO TRY USING PYTHON:


1. Make a simple arithmetic calculator accepting 2 numbers from the user
2. Swapping two numbers with and without a third variable.
3. Accept the MRP of a product from the user. Calculate and display the final price after offering a 15%
discount.
4. Input the name, age and class of the user and print in the following format :
Name Age Class
5. Input the birth year of the user and print the age (in years) as on 1.1.2020.

LOTUS VALLEY INT. SCHOOL, GURUGRAM PAGE 12

You might also like