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

What Is Python

Python is an interpreted, object-oriented programming language that is dynamically typed and garbage-collected. It supports procedural, object-oriented, and functional programming styles. This document provides an overview of key Python concepts including data types, variables, operators, conditional statements, strings, lists, and examples of common Python programs. It compares interpreted languages like Python to compiled languages and outlines advantages and disadvantages of each approach.

Uploaded by

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

What Is Python

Python is an interpreted, object-oriented programming language that is dynamically typed and garbage-collected. It supports procedural, object-oriented, and functional programming styles. This document provides an overview of key Python concepts including data types, variables, operators, conditional statements, strings, lists, and examples of common Python programs. It compares interpreted languages like Python to compiled languages and outlines advantages and disadvantages of each approach.

Uploaded by

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

What is Python?

 Python…  …is a general purpose interpreted programming language.  …is a language that
supports multiple approaches to software design, principally structured and object-oriented
programming.  …provides automatic memory management and garbage collection  …is
extensible  …is dynamically typed.
Comparison
Interpreted  Faster development  Easier debugging  Debugging can stop anywhere, swap in
new code, more control over state of program  (almost always) takes less code to get things
done  Slower programs  Sometimes as fast as compiled, rarely faster  Less control over
program behavior
Compiled  Longer development  Edit / compile / test cycle is longer!  Harder to debug 
Usually requires a special compilation  (almost always) takes more code to get things done 
Faster  Compiled code runs directly on CPU  Can communicate directly with hardware 
More control over program behavior

Operators
 Python supports a wide variety of operators which act like functions, i.e. they do something
and return a value:
 Arithmetic: + - * / % **
 Logical: and or not
 Comparison: > < >= <= != ==
 Assignment: =
 Bitwise: & | ~ ^ >> <<
 Identity: is is not
 Membership: in not in
Variables
 Variables are assigned values using the = operator
 In the Python console, typing the name of a variable prints its value
 Not true in a script!
 Variables can be reassigned at any time
 Variable type is not specified
 Types can be changed with a reassignment
 Variables refer to a value stored in memory and are created when first assigned
Variable names:
 Must begin with a letter (a - z, A - B) or underscore _
 Other characters can be letters, numbers or _
 Are case sensitive: capitalization counts!
 Can be any reasonable length
 Assignment can be done en masse: x = y = z = 1
 Multiple assignments can be done on one line: x, y, z = 1, 2.39, 'cat'
Variable Data Types
 Python determines data types for variables based on the context
 The type is identified when the program runs, called dynamic typing
 Compare with compiled languages like C++ or Fortran, where types are identified by the
programmer and by the compiler before the program is run.
 Run-time typing is very convenient and helps with rapid code development…but requires the
programmer to do more code testing for reliability.
Available basic types:
 Numbers: Integers and floating point (64-bit)
 Complex numbers: x = complex(3,1) or x = 3+1j
 Strings, using double or single quotes: "cat" 'dog'
 Boolean: True and False
 Lists, dictionaries, and tuples
 These hold collections of variables
 Specialty types: files, network connections, objects
Strings
 Strings are a basic data type in Python.
 Indicated using pairs of single '' or double "" quotes.
 Multiline strings use a triple set of quotes (single or double) to start and end them.
 Strings have many built-in functions…
 In the Python console, create a string variable called mystr
 type: dir(mystr)
 Try out some functions:
 try: help(mystr.title)
The len() function  The len() function is not a string specific function.  It’ll return the length
of any Python variable that contains some sort of countable thing.  In the case of strings it is
the number of characters in the string.
String operators  Try using the + and += operators with strings in the Python console.  +
concatenates strings.  += appends strings.  Index strings using square brackets, starting at 0.
If / Else  If, elif, and else statements are used to implement conditional program behavior 
Syntax:
if Boolean_value: …some code elif Boolean_value: …some other code else: …more code
 elif and else are not required – used to chain together multiple conditional statements or
provide a default case.
Lists
 A Python list is a general purpose 1-dimensional container for variables.
 i.e. it is a row, column, or vector of things
 Lots of things in Python act like lists or use list-style notation.
 Variables in a list can be of any type at any location, including other lists.
 Lists can change in size: elements can be added or removed
 Lists are not meant for high performance numerical computing!
 Please don’t implement your own linear algebra with Python lists unless it’s for your own
educational interests.
Making a list and checking it twice…
 Make a list with [ ] brackets.
 Append with the append() function
 Create a list with some initial elements
 Create a list with N repeated elements Try these out yourself! Edit the file in Spyder and run it.
Add some print() calls to see the lists.
List functions
 Try dir(list_1)
 Like strings, lists have a number of built-in functions
 Let’s try out a few…
 Also try the len() function to see how many things are in the list: len(list_1)
Accessing List Elements
 Lists are accessed by index.
 All of this applies to accessing strings by index as well!
 Index #’s start at 0.  List: x=['a', 'b', 'c', 'd' ,'e']
 First element: x[0]  Nth element: x[2]
 Last element: x[-1]  Next-to-last: x[-2]
Python Programs
1. Python Program to Add Two Numbers
2. Python Program to Find the Square Root
3. Python Program to Calculate the Area of a Triangle
4. Python Program to Swap Two Variables
5. Python Program to Check if a Number is Positive, Negative or 0
6. Python Program to Check if a Number is Odd or Even
7. Python Program to Check Leap Year
8. Python Program to Find the Factorial of a Number
9. Python Program to Display the multiplication Table
10. Python Program to Print the Fibonacci sequence
11. Python Program to Find the Sum of Natural Numbers
12. Python Program to Convert Decimal to Binary, Octal and Hexadecimal
13. Python Program to Find LCM
14. Python Program to Find the Factors of a Number
15. Python Program to Make a Simple Calculator
16. Python Program to Display Fibonacci Sequence Using Recursion
17. Python Program to Sort Words in Alphabetic Order

You might also like