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

CST445 Module 1 Part 2 Basics

Uploaded by

anjanaks003
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)
12 views

CST445 Module 1 Part 2 Basics

Uploaded by

anjanaks003
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/ 42

1

Python Basics
2
First Program
3
Python Comments

► Comments can be used to explain Python


code.
► Comments can be used to make the code more
readable.
► Comments can be used to prevent execution
when testing code.
4
Values and types

► A value(literal) is one of the fundamental ► >>>type("Hello, World!")


things like a word or a number that a
program manipulates. ► <class 'str'>

► These values belongs to different types ► >>> type(17)


► <class 'int'>
► >>>"Hello, World"
► 'Hello, World'
1,000,000)
ints 1 0 0 #interpreted as a comma ► >>> type(17.5)
ated list of three integers . ► <type 'float'>
5
Literals

► String Literals
► In Python, a string literal is a sequence of
characters enclosed in single or double
quotation marks
6
Python Variables

► Variables are containers for storing data


values.
► Unlike other programming languages,
Python has no command for declaring a
variable.
► A variable is created the moment you first
assign a value to it.
► A variable is a name that refers to a value.
► Assignment statements create new
variables and also give them values to
refer to
7
Python Variables

► Variables do not need to be declared with any ► x = 4 # x is of type int


particular type and can even change type after
► x = "Sally" # x is now of type str
they have been set.
► print(x)
► print(type(x))
► x=1.56
► print(type(x))

Output
Sally
<class 'str'>
<class 'float'>
8
Variable Names

► A variable can have a short name (like x and


y) or a more descriptive name (age, carname,
total_volume).
► A variable name must start with a letter or the
underscore character
► A variable name cannot start with a number
► A variable name can only contain alpha-
numeric characters and underscores (A-z, 0-9,
and _ )
► Variable names are case-sensitive (age, Age
and AGE are three different variables)
9
Assign Value to Multiple Variables

► Python allows you to assign values to


multiple variables in one line:
10
Assign Value to Multiple Variables

► We can assign the same value to multiple


variables in one line:
11
Output Variables

► The Python print statement is often used


to output variables.
► To combine both text and a variable,
Python uses + the character:
12
Output Variables

► For numbers, the ‘+’ character works as a


mathematical operator:

If you try to
combine a string
and a number,
Python will give
you an error:
13
Excercises

► Create a variable named carname and ► Carname=‘Volvo’


assign the value Volvo to it.
► Create a variable named x and assign the ► X=50
value 50 to it
► Display the sum of 5+10 using two ► X=5
variables x&y
► Y=10
► Print(x+y)
► Create a variable called z , assign x+y to
it
14
Python Data Types

► Data types are the classification or


categorization of data items.
► Data types represent a kind of value which
determines what operations can be performed
on that data.
► Variables can store data of different types, and
different types can do different things.
► Python has the following data types built-in by
default, in these categories:
15
Python Data Types

► Python has five standard data


types −
► Numbers
► String
► List
► Tuple
► Dictionary
16
Getting the Data Type

► You can get the data type of any object by


using type() function
17
Data Type

► If you want to specify the data type, you can


use the following constructor functions:
18
Excercises
19
Escape Sequences

► Escape sequences are the way Python


expresses special characters, such as the
tab, the newline, and the backspace
(delete key), as literals.
20
Numeric Data Types and Character Sets

► There are three numeric types in Python: ► Int


► int ► Int, or integer, is a whole number, positive
or negative, without decimals, of
► float unlimited length.
► Complex
► Variables of numeric types are created when
you assign a value to them:
21
Numeric Data Type

► Float ► Float can also be scientific numbers with


an "e" to indicate the power of 10.
► Float, or "floating point number" is a number,
positive or negative, containing one or more
decimals.
22
Numeric Data Type

► Complex
► Complex numbers are written with a "j" as the imaginary part:
23
Type Conversion

► Type Casting:
In typing casting, a data type is converted into
another data type by the programmer using the
casting operator during the program design.
24
Type Conversion

► Type Coercion
► In type conversion, a data type is
automatically converted into another data type
by a compiler at the compiler time.
25
Type Conversion

► You can convert from one type to another with


the int() float() complex() methods
26
Excercise
27
Character Sets

► Some programming languages use different


data types for strings and individual
characters.
► In Python, character literals look just like
string literals and are of the string type.
► All data and instructions in a program are
translated to binary numbers before being run
on a real computer.
► To support this translation, the characters in a
string each map to an integer value.
► This mapping is defined in character sets,
among them the ASCII set and the Unicode
set.
28
Ord() and char()

► Python’s ord and char functions convert


characters to their numeric ASCII codes
and back again, respectively.
29
Strings

► Assigning a string to a variable is done


with the variable name followed by an
equal sign and the string:
30
Python Booleans

► In programming you often need to know


if an expression is True or False
► You can evaluate any expression in
Python, and get one of two answers True
or False
► When you compare two values, the
expression is evaluated and Python
returns the Boolean answer:
31
Keywords

► Python has a set of keywords that are


reserved words that cannot be used as
variable names, function names, or any
other identifiers:
32
Statements in Python

► Instructions written in the source code for ► Multi-Line Statements:


execution are called statements.
► Statements in Python can be extended to one
► There are different types of statements in the or more lines using parentheses (), braces {},
Python programming language like square brackets [], semi-colon (;), continuation
character slash (\).
► Assignment statement,
► Conditional statement,
► Looping statements etc.
► These all help the user to get the required
output.
► For example, n = 50 is an assignment
statement.
Different Forms of Assignment Statements in 33
Python

► 1. Basic form: ► 3. List assignment(same as tuple)


► student = 'Geeks' ► [x, y] = [2, 4]
► print('x = ', x)
► print(student)
► print('y = ', y)
► 2. Tuple assignment:
► 4. Sequence assignment:
► # equivalent to: (x, y) = (50, 100)
► a, b, c = 'HEY'
► x, y = 50, 100 Output
► print('x = ', x) a=H
► print('a = ', a)
b=E
► print('y = ', y) ► print('b = ', b) c=Y
► print('c = ', c)
Different Forms of Assignment Statements in 34
Python

► 5. Multiple- target assignment:


► x = y = 75
► print(x, y)
35
Expressions

► An expression is a combination of values,


variables, operators, and calls to functions.
► Expressions need to be evaluated.
36
Python Operators

► Operators are used to perform operations ► Python divides the operators in the following
on variables and values. groups:
► Arithmetic operators
► Assignment operators
► Comparison operators
► Logical operators
► Identity operators
► Membership operators
► Bitwise operators
37
Arithmetic operators

► Arithmetic operators are used with numeric


values to perform common mathematical
operations:
38
Precedence and associativity

► Associativity is the order in which an


expression is evaluated that has multiple
operators of the same precedence.
► Almost all the operators have left-to-right
associativity.
► Exponentiation and assignment operations are
right associative,so consecutive instances of
these are evaluated from right to left.
► You can use parentheses to change the order
of evaluation.
39
Mixed-Mode Arithmetic and Type
Conversions

► Performing calculations involving both ► In a binary operation on operands of different


integers and floating-point numbers is called numeric types, the less general type (int) is
mixed-mode arithmetic. temporarily and automatically converted to the
more general type (float) before the operation
► For instance, if a circle has radius 3, you
is performed.
compute the area as follows:
► >>> 3.14 * 3 ** 2
► Thus, in the example expression, the value 9 is
converted to 9.0 before the multiplication.
► 28.26
Python Assignment Operators 40

► Assignment operators are used to assign


values to variables:
41
Python Comparison Operators

► Comparison operators are used to compare


two values:
42
Python Logical Operators

► Logical operators are used to combine


conditional statements:

You might also like