Asm - Artificial Intelligence - 130273
Asm - Artificial Intelligence - 130273
Asm - Artificial Intelligence - 130273
What is Python?
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
It is used for:
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.
Or by creating a python file on the server, using the .py file extension, and running it in the Command Line:
Where in other programming languages the indentation in code is for readability only, the indentation in Python
is very important.
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.
Exercise:
_____________________________________________________________
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
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!")
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)
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:
Output Variables
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 : 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.
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.
You can get the data type of any object by using the type() function:
Example
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
int
float
complex
Example:
x = 1 # int
y = 2.8 # float
z = 1j # complex
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
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))
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
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'>
Arithmetic operators
Assignment operators
Relational operators
Logical operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
In Python, we have the following two functions to handle input from a user and system.
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.
We need to convert an input string value into an integer using a int() function.
We need to convert user input to the float number using the float() function as we did for the integer value.