Python Variables and Operators (PDF)
Document Details
Uploaded by RemarkableDallas
Assiut University
Tags
Summary
This document provides an introduction to Python variables and operators using examples of strings, integers, and floats. It includes practical applications such as printing results and handling user input. Concepts of arithmetic, increment, and assignment operations are also illustrated.
Full Transcript
Section 2 Data types : Variables are element, feature, or factor that is liable to vary or change. هو العنصر او السمه او العامل الذى يمكن ان يتغير او يتنوع Example 5x+3 X is variable Constants are values that will not be changed within the operations. هى قيم مش هتتغير...
Section 2 Data types : Variables are element, feature, or factor that is liable to vary or change. هو العنصر او السمه او العامل الذى يمكن ان يتغير او يتنوع Example 5x+3 X is variable Constants are values that will not be changed within the operations. هى قيم مش هتتغير خﻼل العمليه Example 5X+3 5 ,3 is constant Operators are the used functions that build equations and operations. هى الدوال المستخدمه لبناء معادله او عمليه حسابيه Example 5x+3 + is operators Essential Python Variables 1. Strings A string is traditionally a sequence of characters (The gab is also a character). Example: “Hello”, “It is a true statement”, etc. Less operations are allowed. 2. Integers An integer is a whole number (not a fraction) that can be positive, negative, or zero. Example: 71, 1245, - 80, etc Examples of operations: Add, Subtract, divide, etc. 3. Floats A double type can represent fractional as well as whole values. Example: 10.12, 134e12, etc Examples of operations: Add, Subtract, divide, etc To print / output result on console you can use Print( ) Example : Print(“hello world”) Print (“ welcome to third section”) Click on run The result appear on console Example 1: Make rectangle by using python print ("/ / / / / / / /") print ("/ / / / / / / /") print ("/ / / / / / / /") print ("/ / / / / / / /") Example 2: Make triangular by using python مثلث قائم الزاويه من اليمين print(" *") print(" **") print(" ***") print(" ****") print(" *****") print(" ******") مثلث قائم الزاويه من اليسار print(" *") print(" **") print(" ***") print(" ****") print(" *****") print("******") -------------------------------------------------------------------- مثلث هرمى print(" * ") 1 print(" *** ") 3 print(" ***** ") 5 print(" ******* ") 7 print(" ********* ") 9 print("***********") 11 --------------------------------------------------------- Make square by using python print("* * * * * * *") print("* * * * * * *") print("* * * * * * *") print("* * * * * * *") print("* * * * * * *") print("* * * * * * *") print("* * * * * * *") ------------------------------------------------------- print (" /|") print (" / |") print (" / |") print (" / |") print (" /____|") Input to insert value by user Variable = input(“message to user : ”) EX 1 : EX 2: x=input() x=input(“enter the value of x: ”) print(x) print (x) EX 3: x=input("enter the value of x: \n") print(x) note : \n to take new line EX1: x=1 x+=1 the same to x=x+1 print(x) EX2: x=1 y=2 x=x/y print(x) x=2 Assignment statement x=x+2 Assignment with expression print(x) Print statement y=0.2 print( int (y) ) output =0 y=3.2 print(int (y) ) output =3 y=1.7 print(str(y) ) output=1.7 a = 1.9 b=1 x = int(a) * int(b) print(x) output=1 b = 1.1 c = 0.9 if int(b) == int(c): a=5 print(a) if statement syntax if condition : code if yes EX 1: a=1 b=1 c=2 d=3 if a == b: c=d print(c) output 3 EX 2: a=1 b=2 c=2 d=3 if int(a) == int(b): c=d print(c) output 2 to make a comment you can use ( # ) # hello Working with Strings Result # This is a comment! greeting_1 = "Hello“ # assigning a string value to a variable print(greeting_1) # display the first variable named greeting_1 greeting_2 = ", friend!“ # assigning a string value to another variable print(greeting_2) # display second variable named greeting_2 greeting = greeting_1 + greeting_2 # combining more than a string into 1 print(greeting) # display the combining variable print(len(greeting)) # display the number of characters in the string print(greeting) # display the first character of the string print(greeting) # display character number 10 (last character) print(greeting[-1]) # display the last character print(greeting.replace("friend", " Ahmed")) # replace 'friend' with 'Ahmed' greeting_new_line = greeting_1 + "\n" + greeting_2 # new line print(greeting_new_line) # display the string divided on two lines print(len(greeting_new_line)) # display the number of characters in the string Hello , friend! Hello, friend! 14 ! Hello, Ahmed! Hello , friend! 15