You are on page 1of 44

Week#2

FUNDAMENTALS OF
PYTHON
Lecture#2
Course Outline/Intro,
Data Types, Functions, Print, Input
Basic Programs.

By: Engr. Muhammad Adnan Malik


Class of BS-CS, NCBA&E, MULTAN.
Data Types & Literals
Literal
• A literal is data whose values are determined by the
literal itself.
• To understand Literal, take following examples:
•A number 2 is a literal as it has literally a value of 2
• But what about c?
•It can be a character or alphabet
•It can be speed of light
• So hence it is not a “Literal”
•But c=2 is literal, as c has assigned value.
Literal
•We can simply simplify this concept by saying that a data
type with assigned value will specify whether a variable is
literal or not.

•In programming, if some value is not assigned to a


variable, it is not a literal, and hence it is “Undefined”
Data Types
There are five basic data types associated with variables, to
declare them as a literals:
• int - integer: a whole number.
• float - floating point value: i.e a number with a fractional
part.
• double - a double-precision floating point value.
• char - a single character.
• void - valueless special purpose type which we will examine
closely in later sections.
Keywords In
Programming
Languages
Keywords
•In programming, a keyword is a word that is reserved by
a program because the word has a special
meaning. Keywords can be commands or parameters.

•Every programming language has a set of keywords that


cannot be used as variable names. Keywords are
sometimes called reserved names .
Keywords
•Some keywords in python language are:
• Input()
•Print()
•Int
•Float
•Str
•Range()
•For
•If
•else
Functions In
Programming
Functions
• A function is a type of procedure or routine that
performs a specific task.
• A function needs Arguments to work.
function(argument)
•For example function sin(x) needs argument x to
peroform operation.
•Similarly y=2x needs argument x to work.
Functions
•Similarly in programming language there are
various functions. Some of them are:
• Print()
• Input()
• Range()
Each above function require an argument to work.
Functions
• In any programming language there are two types
of functions:
•Built-In
•User defined
Programming Time!
Programming
•We will cover following topics for programming
today:
• Assigning values to variables
• Converting data types.
• Printing values.
• Using Input Function.
• Developing some basic Programs.
Assigning Value to
Variable
RULES OF DECLARING VARIABLES
•Following are allowed:
•x=3, y=4, x1=3, x_1=3
•Following are not Allowed:
• x.1 is not valid variable
• Numeric values cannot be written before an
alphabet:
•For example 1x, 3x, 3_x are all WRONG.
•Keywords are not allowed as variable
Assigning Value to Variable
•Default data type after assigning a numeric value
to variable is int.
•But you can convert it into string y using “” or ‘’
• Also you can use keyword str() to convert value
into string.
Assigning Value to Variable
•Also you can use keyword str() to convert value
into string.
•Examples:
•x=3 (x variable, assigned value 3, type-int)
•x=‘pakistan’ (x variable, assigned value string)
•x=“pakistan”(x variable, assigned value string)
•x1=3 (x1 variable, assigned value 3, type-int)
Changing Data Types
(TYPE CASTING)
Changing Data Types
Examples:
• a=3.6
• b=float(a)
• c=int(a)
• d=str(a)
• print(a,b,c,d)
Print() Function
Print() Function
• Name: print()
• Syntax : print(x), print(3) ,print(“name”) etc.
• Example:
1. Print(x=3) (numeric print with variable)
2. Print(3) (numeric print without variable)
3. Print(“I love Pakistan”) (string printing)
4. print(“I”, “love” , “Pakistan”) (string print
with spaces, it is same as 3.)
Formatting with Print()
Function
Formatting with Print() Function
•We can format our ouput text data in different
styles by using following :
• \n (Escape character to move text to new line)
•end=‘’ (To join to lines)
•sep=“” (to sperate text byspecfic charter or
pattern)
Formatting with Print() Function
•Examples: Try following
1. Print(“hi”, “\nhow are you”)

1. print(“Hi”,end=‘’)
2. Print(“how are you”)

1. print(“Hi”,end=‘*’)
2. Print(“how are you”)
Formatting with Print() Function
•Examples: Try following
1. print(“hi”, “how” , “are” , “you” , sep=‘*’)
2. print(“hi”, “how” , “are” , “you” , sep=‘\n’)
Your first programs!
Your first programs
1. x=3
2. print(“value of x is: ”)
3. print(x)

1. x=3
2. print(“value of x is: ” , x)

1. x=int(“3.9”)
2. print(“value of x is: ” , x)
Your first programs
1. x=str(3)
2. print(“value of x is: ”)
3. print(x)

1. x=‘3’
2. print(“value of x is: ” , x)
Your first programs
1. x=3
2. y=4
3. print(“value of x is ” , x, “and y is” , y)

1. x=3
2. y=4
3. print(“value of x is ” , x, “and y is” , y)
Input() function
Input() function
• This function prompts user to enter value
• Remember input() function takes string by
default.
• You need to change data type to take integer
value.
Input() function
1. x=input(“enter value”)
2. print(x)

1. x=int(input(“enter value”))
2. print(x)
Mathematics With
Python
Mathematics With Python
• You can perform all arithmetic operation with
python.
OPERATOR SYMBOL OPERATION
Addition + Adds values
Subtraction - Subtract Values
Division / Divide Values,
result in float.
Remainder % Gives remainder
after division
Division Without // Integer Divisional
Float Operator
Mathematics With Python
Remember:
1. Division operation always return answer in
float
2. Remainder depends whether input values are
float or integer.
Basic Programs
Basic Programs
1. x=3
2. y=4
3. z=x+y
4. print(z)

1. x=“3”
2. y=“4”
3. z=x+y
4. print(z)
Remember!

• Float + integer=Float

• String + float=illegal

• String + String = String


Basic Programs
1. x=4
2. y=5
3. z=x//y
4. print(z)

1. x=4
2. y=5
3. z=x/y
4. print(z)
Concatenation
Concatenation
• The + (plus) sign, when applied to two strings,
becomes a concatenation operator .

• It simply concatenates (glues) two strings into one


Concatenation
1. x= “4”
2. y= “5”
3. z=x+y
4. print(z)

1. x=4
2. y=“5”
3. z=x+y
4. print(z)

You might also like