0% found this document useful (0 votes)
6 views15 pages

Python

Python is a high-level, general-purpose programming language known for its object-oriented features, dynamic typing, and easy syntax. It supports various data types, operators, and conditional statements, making it versatile for programming tasks. Python is open-source and runs on multiple platforms, with a strong emphasis on readability and simplicity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

Python

Python is a high-level, general-purpose programming language known for its object-oriented features, dynamic typing, and easy syntax. It supports various data types, operators, and conditional statements, making it versatile for programming tasks. Python is open-source and runs on multiple platforms, with a strong emphasis on readability and simplicity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

PYTHON

Introduction
Python is a widely used general-purpose, high-level programming
language.
designed by Guido Van Rossum and developed by Python Software
Foundation.
Why to use Python?
Object-oriented - Python supports such concepts as polymorphism,
operation overloading and multiple inheritance.
Indentation - Unlike other languages like C, C++, or Java that uses {} to
mark blocks of code. Python uses indentation to group statements.
(usually 4 spaces) - Note: Don’t mix tabs and spaces.
Open-source - downloading python and installing python is free and easy.
Dynamic Typing - you don’t have to declare the datatype of a variable
when you declare it. Python figures it out at runtime.
Built-in types and tools
Library utilities
Third party utilities (for example Numpy, SciPy etc)
Automatic memory management.
Portable - Python runs virtually every major platform used today as long
as you have a compatible python interpreter installed.
Easy to use and learn
No intermediate compiling
Python programs are compiled automatically to an intermediate form
called byte code, which the interpreter reads.
This gives the python the development speed of an interpreter without
performance loss inherent in purely interpreted languages.
Interpreted Language - Python is processed at runtime by Python
interpreter line by line.
Straight forward syntax
Byte
Source Code Runtime

m.py m.pye PVM

Source Code extension is .py


Byte code extension is .pye (Compiled Python Code)
Python Character Set
Character Set is a set of valid characters that a language can recognize. A
character represents any letter, digit, or any other symbol. Python
supports Unicode Encoding Standard.

Letters A-Z, a-z

Digits 0-9

Special Symbols space +-/*\() [] {} // = != == <, > . '"'"",;:%!& # <= >= @_(underscore)

Whitespaces Blank space, tabs ( -->), carriage return (), newline formatted.

Other Characters Python can process all ASCII and Unicode part of data or literals
Identifiers
Identifiers are fundamental building blocks of a program and are used as
the general terminology for the names given to different parts of the
program like variables, objects, classes, functions, lists, dictionaries etc.
An identifier is an arbitrary long sequence of letters and digits
The first character must be a letter, the underscore (_) counts as a letter.
Upper and lowercase letters are different. All characters are significant
The digits 0 through 9 can be part of identifier except for the first
character.
Identifiers are unlimited in length. Case Sensitive.
An identifier cannot contain any special character except for underscore
(_)
An identifier must not be a keyword of python.
Keywords in Python
False await else import pass

None break except in raise

True class finally is return

and continue for lambda try

as def from nonlocal while

assert del global not with

async elif if or yield

>>> import keyword


>>> print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif',
'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass',
'raise', 'return', 'try', 'while', 'with', 'yield']
Python Variables
Variables are identifiers of a physical memory location, which is used to
hold values temporarily during python execution.
Python interpreter allocates memory based on the values data type of
variable, different data types like integers, decimals, characters, etc. can
be stored in these variables.
Syntax:
<variable_name> = <value>
Datatypes

Python offers the following built-in core data types:


Numbers - immutable (cannot be changed / modified)
Strings - immutable (cannot be changed / modified)
List - mutable (changed / modified)
Tuple - immutable (cannot be changed / modified)
Dictionary - mutable (changed / modified)
Operators

Arithmetic Operators - +, -, * , /, //, %, **


Comparison Operators - ==, !=, >, <, >=, <=
Assignment Operators - =, +=, -=, *=, /=, %=, **=, //=
Bitwise Operators - &, |, ^, ~, <<, >>
Logical Operators - and, or, not
Membership Operators - in, not in
Identity Operators - “is” (returns true if both operands are pointing to
same memory location), “is not” - (returns true if both operands are
pointing to different memory location).
Console Input in Python

# using input() to take user input


num = int(input("Enter a number: "))
print("You entered: ", num)
Type Conversion
num_string = '12'
num_integer = 23
print("Data type of num_strng before Type Casting: ", type(num_string))
#Explicity Type Conversion
num_string = int(num_string)
print("Data type of num_strng after Type Casting: ", type(num_string))
Conditional Statements
Conditional Statements act depending on whether a given condition is
true or false. You can execute different blocks of codes depending on the
outcome of a condition. Condition statements always evaluate to either
True or False.
There are 4 types of conditional statements
if statement
if-else statement
if-elif-else statement
nested if-else statement
If statement is the simplest form. It takes a condition and evaluates to either
True or False.
If the condition is True, then the True block of code will be executed, and if the
condition is False, then the block of code is skipped, and the controller moves
to the next line.

number=6
if number > 5:
print(number**2)
print("Next Lines of Code")
The if-else statement checks the condition and executes the if block of code
when the condition is true and if the condition is false, then it will execute the
else block of code.
age = int(input("Enter the age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Cannot vote")

You might also like