unit 1_Python basics, control structures and other features
unit 1_Python basics, control structures and other features
Subtitle
Development and Release:
▪ Guido van Rossum, a Dutch programmer, started working on Python in the late
1980s at the National Research Institute for Mathematics and Computer Science
in the Netherlands (CWI).
▪ Guido aimed to create a successor to the ABC programming language while
incorporating certain desirable features such as an easy-to-understand syntax
and automatic memory management.
▪ The first version of Python, Python 0.9.0, was released in February 1991.
Definition
Python
C Programming
programming dynamic semantics ->
Int x;
X=10 Variables are dynamic objects
Int y;
Y=5
Printf (“%d”, x+y);
Print(x+y)
Compiler Translator Interpreter
Is a software who Computer programs are Translates source code
converts source code to written in high level to machine code line by
machine language, hence we line at run time
understandable code. need translator to
convert binary Ex- Java
Ex:- C language.
Can run directly if
Exe file can be used to interpreter is available.
share with others it can
be compiled / run in
any machine
▪ Python comes with a comprehensive standard library that provides a wide range
of modules and functions for various tasks, such as file handling, networking,
database access, GUI development, and more.
▪ The standard library eliminates the need for external dependencies in many
cases.
Cross-Platform Compatibility:
▪ Python has a large and active community of developers who contribute to its
growth.
▪ The Python Package Index (PyPI) hosts thousands of third-party libraries and
frameworks that extend Python's capabilities in areas like web development,
data analysis, scientific computing, machine learning, and more.
Object-Oriented Programming (OOP) Support:
▪ Python has a vibrant and supportive community that actively contributes to its
development and offers resources, tutorials, and forums for assistance.
▪ This community-driven approach fosters collaboration, knowledge sharing, and
continuous improvement of the language.
Scalability and Flexibility:
▪ Sequence of characters
▪ Represented by using ‘ ‘ or “ “.
Input and Output Statements
▪ input and output statements are used to interact with the user and display
information.
▪ Print Statement:
Used to display information to the console.
python
Copy code
print("Hello, World!")
Input and Output Statements
▪ Formatted Output:
Using the format() method or f-strings for formatted output.
name = "John"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
▪ Input Statements:
▪ Input Function:
Used to take user input. The input() function returns a string.
name = input("Enter your name: ")
print("Hello, {}!".format(name))
▪ Note: If you want to take numerical input and use it as a number, you need to
convert it using int() or float().
age = int(input("Enter your age: "))
Variable
▪ Constants are variables whose values should not change throughout the
execution of a program. Python does not have built-in support for constants.
▪ PI = 3.14159
Identifiers
▪ Identifiers are the names assigned to memory locations like variables, functions,
classes, modules or objects.
▪ Keywords are reserved words and cannot be used as constants, variables or other
identifier names.
▪ Keywords are always mentioned in lower case only expect True, False and None.
▪ 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']
Lines and Indentation
▪ No braces to indicate blocks of code for class and function definitions or flow
control
▪ Blocks of code are denoted by line indentation.
▪ All statements within the same block must be indented with the same number of
spaces. All the continuous lines indented with same number of spaces will be
considered as same block.
▪ Spaces should be consistent through out the block.
Statements
▪ Every expression consists of a least one operand and one or many operator
ex:- z = x+5
z,x,5 are operands
▪ =, + are operators
DATA TYPE CONVERSION
1. Number
2. String
3. List
4. Tuple
5. Dictionary
6. Set
Number
▪ float (floating point real values): Float is a positive or negative number containing one or more
decimals. Float can also be scientific numbers with an “e” to indicate the power of 10.
Example: 0.12, -34.343, 232e+21, 342.2E-12
▪ Complex (complex numbers): Complex numbers are written with a real and “j” as the imaginary
part
Example: 122.3 + 12j , 23.32 – 34.3j
Random Numbers
Python does not have a random() function to make a random number, but Python has a
built-in module called random that can be used to generate random numbers.
Import the random module, and display a random number between the specified series.
Example:
>>> import random
>>> print(random.randrange(1,1000))
800
Strings
▪ Ordered
▪ Changeable
▪ Allow Duplicates
▪ Can have different data type
Tuples
▪ Unchangeable
▪ Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple
has been created.
▪ Allow Duplicates
▪ Since tuples are indexed, they can have items with the same value
▪ Membership operators test the membership of a value in a sequence, such as strings, lists, or tuples.
▪ If the value is present in the sequence, the resulting value is true; otherwise, it returns false.
▪ Membership operators check the membership of value inside a data structure. If the value is present
in the data structure, the resulting value is true; otherwise, it returns false.
Arrays
▪ if statement
The condition will be evaluated and the result can be either TRUE or FALSE
Syntax: if (expression)
# set of statements to be executed when the condition is TRUE
Decision Making
If else statement
Syntax: if (condition):
# TRUE block statements
else:
# FALSE block statements
Decision Making
if…elif…else statement
Decision Making
Nested if statement:
Looping
▪ The while loop checks the condition initially and repeats the execution of the block of the statements
until the evaluated condition becomes FALSE.
▪ We use a while loop when we do not know the number of times to iterate in advance.
The syntax for the while loop is:
while (condition):
#block of statements
Control Statements
▪ Python supports the following three control statements. (i) break (ii) continue (iii) pass
▪ Break ->
During the execution of the loop, if an emergency exit from the loop is required, then break
statement is used.
loop (condition):
#block of statements
Break
else:
#block of statements
Continue