0% found this document useful (0 votes)
51 views

Python Basics-1

The document provides an overview of Python basics including: - Python is a versatile, high-level programming language used for tasks like data science, machine learning, web development and more. - It supports different programming paradigms like object-oriented, procedural and functional programming. - Key concepts discussed include keywords, identifiers, literals, string formatting and input/output. Control characters, string methods and data types are also covered at a high level. - The internal working of Python and how it uses a virtual machine to execute code is summarized briefly.

Uploaded by

sashankraj27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Python Basics-1

The document provides an overview of Python basics including: - Python is a versatile, high-level programming language used for tasks like data science, machine learning, web development and more. - It supports different programming paradigms like object-oriented, procedural and functional programming. - Key concepts discussed include keywords, identifiers, literals, string formatting and input/output. Control characters, string methods and data types are also covered at a high level. - The internal working of Python and how it uses a virtual machine to execute code is summarized briefly.

Uploaded by

sashankraj27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Computational

Problem Solving

Python Basics

CSE, Amrita School of Computing, Amritapuri


Man, Cabbage, Goat and Wolf Problem

A man lives on the east side of a river. He wishes to


bring a cabbage, a goat, and a wolf to a village on the
west side of the river to sell. However, his boat is
only big enough to hold himself, and either the
cabbage, goat, or wolf. In addition, the man cannot
leave the goat alone with the cabbage because the
goat will eat the cabbage, and he cannot leave the
wolf alone with the goat because the wolf will eat the
goat. How does the man solve his problem?
Man, Cabbage, Goat and Wolf Problem

● Steps:
1) Farmer crosses with goat
2) Farmer returns alone
3) Farmer crosses with cabbage
4) Farmer returns with goat
5) Farmer crosses with wolf
6) Farmer returns alone
7) Farmer crosses with goat
8) They are all on the other side
Programming Paradigms
• A programming paradigm is a style, or “way,” of programming.
• It does not refer to a specific language, but rather it refers to the way you
program.
Programming Paradigms
1. Imperative programming paradigm: It is one of the oldest programming
paradigms.
• It features a close relation to machine architecture.
• The paradigm consists of several statements, and after the execution of all of
them, the result is stored.
• It’s about writing a list of instructions to tell the computer what to do step by
step.
• Examples of Imperative programming paradigm: C, Fortran
• Divided into three broad categories: Procedural, OOP, and Parallel
processing.
Imperative programming paradigm
A. Object oriented programming
• The program is written as a collection of classes and objects which are meant
for communication.
• The smallest and basic entity is an object and all kind of computation is
performed on the objects only.
• Advantages:
• Data security
• Inheritance
• Code reusability
• Flexible and abstraction is also present
• Examples of Object Oriented programming paradigm: Java, C++, Visual Basic
.NET, Ruby, Python
Imperative programming paradigm
B. Procedural programming paradigm
• This paradigm emphasizes procedure in terms of underlying machine model.
• Imperative programming with procedure calls
• Examples – C, C++, Java, Pascal

C. Parallel processing approach


• Processing of program instructions by dividing them among multiple
processors
Declarative programming paradigm
• A style of building programs that expresses logic of computation without
talking about its control flow.
• The approach focuses on what needs to be achieved instead of instructing
how to achieve it.
• The logic programming paradigm takes a declarative approach to problem-
solving.
• not made up of instructions - rather it's made up of facts and clauses.
• The functional programming paradigm has its roots in mathematics and it is
language-independent.
• Eg: Javascript, Scala, Erlang, Lisp
• Database/Data-driven programming approach is based on data and its
movement.
What is Python?
● Python: High-level, object-oriented, interpreted language
● Python is extremely versatile
○ Data Mining, Data Science, AI, Machine Learning, Web Development, Web
Frameworks, Embedded Systems, Graphic Design applications, Gaming,
Network development, Product development, Rapid Application
Development, Testing, Automation Scripting, the list goes on.
● Extensive libraries and environments
Python
• One of the simplest and intuitive languages to use
• Portable: Same code can be used in different machine. No need to rewrite
for multiple platforms
• Python is dynamically-typed – a variable can have different types
• a=5
• a = "Five"
• Can be used in multiple programming paradigms.
• You can write code in an object-oriented way.
• Or, you can do some functional programming or work procedurally.
• Python has indentation rules
• It is general-purpose.
Internal working of Python
• Python uses Python Virtual Machine (PVM) to convert Python code to machine-
understandable code.
• The Python Interpreter is stored in memory as a bunch of instructions.
• It has two components - Compiler, and Python Virtual Machine.
• Translates the source code into an intermediate language that is called Byte Code.
• Python Virtual Machine (PVM) takes those byte codes and converts those
instructions into machine code.
• so that the computer can execute
those machine code instructions and
display the final output.

• As long as the Python bytecode and the Virtual Machine


have the same version, Python bytecode can be executed on any platform (Windows, MacOS,
etc.).
The Magic of Python
• The “>>>” is a Python prompt indicating that Python is ready for us to
give it a command. These commands are called statements.
• >>> print("Hello, world“)
Hello, world
>>> print(2+3)
5
>>> print("2+3=", 2+3)
2+3= 5
>>>

Python Programming, 2/e 12


Keywords

● Keywords are some predefined and reserved words in python that have special
meanings. Keywords are used to define the syntax of the coding.
● To display the keywords, type help() in the Python shell, and then type keywords
(type 'q' to quit).
Identifier

● An identifier is used to provide a name for a given program element with a


sequence of one or more characters.
● Each variable name is an identifier.

● Rules to be followed:

○ Can only contain alphanumeric characters and underscores (a-z, A-Z, 0-9, _)
○ Must start with letters but cannot start with a number
○ Names are case-sensitive
○ Name cannot be a keyword
○ Should be one word with no spaces in between
○ Python does not allow special characters
Identifier
Literal

● Literals in Python can be defined as numbers, text, or other data that represent
values to be stored in variables.
● Raw data assigned to variables, or, constants used while programming
○ Numeric Literals
○ String Literals
○ Boolean Literals
○ Literal Collections
○ Special Literals
Literal
• A numeric literal is a literal containing only the digits 0–9, an optional sign character,
and a possible decimal point.
• Commas are never used in numeric literals
• Floating Point: used to produce a specific number of decimal places
How to encode(represent) character within
a computer ???
String Literal

● String literals -> delimited (surrounded) by a matching pair of either single (') or
double (") quotes.

● May also contain quote characters as long as different quotes are used to delimit:
"Jennifer Smith's Friend" Correct

Incorrect
Literal

• Example of String Literals in Python


• name = ‘Johni’ , fname=“johny”
• Example of Integer Literals in Python(numeric literal)
• age = 22
• Example of Float Literals in Python(numeric literal)
• height = 6.2
• Example of Special Literals in Python
• name = None
Control character
● Special characters that are not displayed on the screen, rather, they control the
display of output (among other things).
● Control characters do not have a corresponding keyboard character
● Represented by a combination of characters called an escape sequence
● Escape sequence: begins with an escape character
○ Causes sequence of characters following it to “escape” their normal meaning
=> given by backslash (\)
○ For ex: escape sequence '\n', represents the newline control character
Basic Input, Output, and String Formatting in Python
• >>> name = input("What is your name? ")
• What is your name? Amrita
• >>> name
• ‘Amrita’

• input() always returns a string.


• If you want a numeric type, then you need to convert the string to the appropriate
type with the built-in int(), float(), or complex() function:
Format string data using Python f-strings
• To use formatted string literals, begin a string with f or F before the opening
quotation mark or triple quotation mark.
• Inside this string, you can write a Python expression between { and } characters
that can refer to variables or literal values
Python String split() Method
Split a string into a list of strings after breaking the given string by the specified
separator

You might also like