[go: up one dir, main page]

0% found this document useful (0 votes)
11 views50 pages

Lec3 4

The document provides an overview of Python programming, covering data types such as integers, floats, and complex numbers, as well as string manipulation and user input methods. It includes practical exercises for creating variables, converting types, and using built-in functions. Additionally, it discusses Boolean values, operators, and the differences in user input handling between Python 2.7 and 3.6.
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)
11 views50 pages

Lec3 4

The document provides an overview of Python programming, covering data types such as integers, floats, and complex numbers, as well as string manipulation and user input methods. It includes practical exercises for creating variables, converting types, and using built-in functions. Additionally, it discusses Boolean values, operators, and the differences in user input handling between Python 2.7 and 3.6.
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/ 50

Computer Programming

PYTHON
Done By: Thana Abdulhalim
01
Python Numbers
Int Float Complex
Python Numbers
● Int: Int, or integer, is a whole number,
positive or negative, without
decimals, of unlimited length.

● Float: Float, or "floating point


number" is a number, positive or
negative, containing one or more
decimals.

● Complex: Complex numbers are


written with a "j" as the imaginary
part.
Example
Convert Types
● You can convert from one
type to another with
the int() , float() , complex()
methods
● Note: You cannot convert
complex numbers into
another number type.
Example
Python Random
Number
● Python does not have a random()
function to make a random number, but
Python has a built-in module called
random , that can be used to make
random numbers
Example
01
Practice Time !
Numbers Random
8 Minutes
● Create 3 number variables, int
01 , float , and complex .
● Assign any value to them,
then print them with their
types
● Change int to float variable
02 , and float to int variable.
● Print the types of the new
variables

03 ● Create 2 random numbers


02
Python Strings
Slice Modify Concatenate

Format escape Methods


Python Strings
● Strings in python are surrounded
by either single quotation marks,
or double quotation marks.
● ‘hello’ is same as “hello”.
● You can display a string literal
with the print() function
● You can use 3 double or single
quotation for multi line strings
Example
Strings as an Array
● Like many other popular
programming languages, strings in
Python are arrays of bytes
representing uni-code characters.
● However, Python does not have a
character data type, a single
character is simply a string with a
length of 1.
● Square brackets can be used to
access elements of the string.
Example
String length and
content
● To get the length of a string, use the len()
function .
● To check if a certain phrase or character
is present in a string, we can use the
keyword “in” .
● To check if a certain phrase or character
is NOT present in a string, we can use the
keyword ”not in” .
Example
Example
Strings Slicing
● You can return a range of characters
by using the slice syntax.
● Specify the start index and the end
index, separated by a colon, to
return a part of the string.
● By leaving out the start index, the
range will start at the first character
● By leaving out the end index, the
range will go to the end
● Use negative indexes to start the
slice from the end of the string
Example
String Modifying
methods
● Upper Case : upper()
● Lower Case : lower()
● Remove Whitespace : strip()
● Replace String : replace ()
● Split String : split()
Concatenate Strings
String Format
● As we learned in the Python Variables
chapter, we cannot combine strings and
numbers .
● But we can combine strings and numbers
by using the format() method.
● method takes the passed arguments,
formats them, and places them in the
string where the placeholders {}
String Escape
Characters
● To insert characters that are illegal in a
string, use an escape character.
● An escape character is a backslash \
followed by the character you want to
insert.
● An example of an illegal character is a
double quote inside a string that is
surrounded by double quotes
Example
02
Practice Time !
Strings
10 Minutes
● Create a first_name and last_name
variables , then concatenate them .
01 Add space between them.
● Create your name variable and use
upper python function to it .
● Create x = “welcome to
02 python” .
● Slice x to print only “python”
● Slice x to print everything
except python

03
● Print “this is ‘my’ code”
03
Booleans & Operators
Booleans Operators
Booleans Values
● 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
● When you compare two values, the
expression is evaluated and Python
returns the Boolean answer
● The bool() function allows you to evaluate
any value, and give you True or False
Example
Example
Python Operators
Python divides the operators in the
following groups:
● Arithmetic operators
● Assignment operators
● Comparison operators
● Logical operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
Floor Division (returns the quotient
//
without the remainder)
Modulo (returns the remainder of
%
division)
** Exponentiation
= Assignment
== Equality
!= Not equal
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
and Logical AND
or Logical OR
not Logical NOT
03
Practice Time !
Booleans Operators
5 Minutes
● Check the bool value of
01 “hello” , 33 , {}
● print these operation : x**x ,
y+z

02 ● Return only the int value of


17 / 2
04
User Input
Get data from the user
Python user input
● Python allows for user input.
● That means we are able to ask the
user for input.
● The method is a bit different in
Python 3.6 than Python 2.7.
● Python 3.6 uses the input() function
● Python 2.7 uses the raw_input
function
● Python stops executing when it
comes to the input() function, and
continues when the user has given
some input.
Example
04
Practice Time !
User input
5 Minutes
● Create a program the accept
01 user name , age , phone
number , password

02 ● Print the user input


Weekly Problem

write a program to
accept a string from
user then print the
letters that are in even
index .
Quiz Time
01 02

03 04
Q.01
What are the 3 numbers data
type
Q.02
How to convert type from float to
int
Q.03
What are the logical operators
Q.04
What was the input function in
python 2.7
Thanks!
Do you have any questions?

CREDITS: This presentation template


was created by Slidesgo, including icons
by Flaticon, and infographics & images
by Freepik

You might also like