Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Introduction To Python

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

Introduction To Python

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

PYTHON

INTRODUCTION TO PYTHON
INTRODUCTION
WHAT IS Python is a high-level, versatile, and widely-
used programming language known for its
simplicity and readability. It was created by

PYTHON Guido van Rossum and first released in 1991.


Python has gained immense popularity due to
its ease of use, extensive standard library, and
a vibrant community of developers.
Features of Python
Python's syntax is designed to be easily readable, using
Readability indentation to define code blocks instead of curly braces or
keywords. This makes it accessible to both beginners and
experienced programmers.

High-level Python is a high-level language, which means it abstracts many low-

Language
level details, making it easier to write and understand code.

Interpreted Python is interpreted, not compiled, which means you can write and
run code directly without the need for a separate compilation step.
Language This makes development faster and more interactive.
Multi- Python supports multiple programming paradigms, including
paradigm procedural, object-oriented, and functional programming, giving
developers flexibility in how they approach problems.

Extensive Python comes with a vast standard library that includes modules for

Standard various tasks, such as file handling, networking, web development,


data manipulation, and more. This "batteries included" philosophy

Library
simplifies development.

Cross- Python is available on multiple platforms (Windows, macOS, Linux)


platform and is compatible with many different systems and hardware.
Large Python has a robust and active community of developers, which
Community means there are plenty of resources, libraries, and third-party
packages available for almost any application or domain.

Python is an open-source language, allowing anyone to use, modify,

Open Source and distribute it freely. This openness has contributed to its
widespread adoption.

Python is used in a wide range of applications, from web


development (with frameworks like Django and Flask) to data
Versatility science and machine learning (with libraries like NumPy, pandas,
and TensorFlow) to scripting and automation
Dynamic Python uses dynamic typing, where variable types are
Typing determined at runtime. This can lead to more flexible and
concise code but requires careful attention to data types.

Community- Python's development is guided by a community-driven process,

driven where enhancements and new features are proposed, discussed,


and implemented through Python Enhancement Proposals (PEPs

Development
Python Installation

Python Installation
Installing python
The official Python website is www.python.org, where downloads,
tutorials, community, etc. may be found.
A convenient way of installing Python together with a large
number of packages (several to be used during the course) is to
install Anaconda (www.anaconda.com/download/)
Choose Python 3 (the stable version is currently 3.11), since it will
be assumed on all slides, assignments, etc.
Find some suitable IDE/working environment, e.g., PyCharm,
PyDev, Jupyter, Emacs
Variables and
Strings
Variables and Numbers
A variable is created when a value is assigned to it
v = 3.6
There are three types of numbers; int, float, and complex:
i = 314
f = 3.14e2
z = 2+3j
The type of a variable can be checked with isinstance(...)
b = isinstance(i,float) # b = False
Strings and Castings
Strings (str) are surrounded by single or double quotes
b = isinstance("I",str) # b = True
Casting using constructor functions; int(...), float(...), str(...)
i = int(3.14) #i=3
f = float(3) # f = 3.0
s = str(3.14) # s = "3.14"
f = float(s) # f = 3.14
Operators
Operators
Arithmetic operators; +, -, *, /, ** (exp.), // (floor div.), %(modulus)
v = 2.0 + 2**3 # v = 10.0
Assignment operators; =, +=, -=, *=, /=
x = 12
x += 2 # x = 14
Comparison operators; ==, !=, >, <, >=, <=
b = (2.0 == 2) # b = True
Logical operators; and, or, not
b = (1+1 == 2 and not(4>5)) # b = True
Identity operators; is, is not
b = (2 is 2.0) # b = False
b = (1+1 is 2) # b = True
Control
structures
Control Structures - If Statements
if statements (with elif and else)
if n>5:
print("more than 5")
elif n == 5: # - elif not required,
print("equal to 5") # and multiple allowed
else: # - else not required
print("less than 5") # and most one allowed
Python is a whitespaces sensitive programming language.
They are used Instead of curly braces/brackets
Loops
Loops - For
Examples
for i in range(3): # Prints 0, 1, 2
print(i)

for i in [1,2,3]: # Prints 1, 2, 3


print(i)

for i in "hello": # Prints h, e, l, l, o


print(i)
Loops - For
Examples
for e in enumerate(["a","b","c"]):
print(e) # Prints (0,a), (1,b), (2,c)
for e in enumerate(["a","b","c"]):
if e[0] % 2 == 0: # Prints a, c
print(e[1])
Loops - For
Examples
for i in [1,2,3]:
if i % 2 == 0:
break
print(i) #Prints 1

for i in [1,2,3]:
if i % 2 == 0:
continue
print(i) #Prints 1, 3
While Loops
Loops - While
Examples with break and continue
i=1
while i < 4: # Prints 1, 2, 3
print(i)
i += 1

i=1
while i < 4: # Prints 1
if i % 2 == 0:
break
print(i)
i += 1
Loops - While
Examples with break and continue
i=1
while i < 4: # Prints 1 and then
if i % 2 == 0:
continue # enters infinite loop
print(i)
i += 1
CONCLUSION
That was simple!
QUESTIONS?
Exercise
Write a Python program that asks the user to input a number. If the number is even, the program should print "The
number is even." If the number is odd, it should print "The number is odd." Ensure that you store the user's input in a
variable and use a conditional control structure to determine whether it's even or odd.
Create a Python program that calculates the factorial of a given positive integer. Ask the user to input a positive integer,
and then use a loop (e.g., a "for" or "while" loop) to calculate and print the factorial of that number. Make sure to use a
variable to keep track of the result.
Write a Python program that generates the Fibonacci sequence up to a specified number of terms. Ask the user to input
the number of terms they want in the sequence. Use a loop and variables to generate and print the Fibonacci sequence.
For example, if the user enters 5, the program should print "0, 1, 1, 2, 3" as the first 5 terms of the Fibonacci sequence.

You might also like